Forum: Ruby on Rails Customizing Model Data -- Inside Model or View or Helper?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
18607dc7098d879b13f5ed22a70c4302?d=identicon&s=25 Eric Milford (ericindc)
on 2009-01-31 12:52
(Received via mailing list)
Say I have to Models, Project and Creator, related to each other such
that @project.creator is valid.  Creator has two fields, first_name
and last_name, for the creator's name.  Assuming that I want a quick
way to display the creator's name as "last_name, first_name" inside
the view, which is the best approach (efficiency, the Rails way, etc.)
and why?

Here are the approached I've come up with...

A) Create a method in the Creator class called commified_name and use
@project.creator.commified_name in the view.  This seems like a good
option.  It's DRY and fits the rest of the mold for @project.xyz in
the view, but I think following MVC the model is not meant for
formatting, right?

def commified_name
    [self.last_name, self.first_name].join(', ')
end

B) Do the join inside the view.  Probably the worst of the options
since it allows for variations in how the name is displayed.

C) Create a method in the Creator Helper called commified_name and use
commified_name in the view.  I believe this is the best answer since
formatting of information that goes in the View belongs in a Helper,
right?  I just find that A feels more right to me.

def commified_name
    [@project.creator.last_name, @project.creator.first_name].join(',
') if @project
end

If C (or A) is the best answer, is this a good way to accomplish what
I'm after.  Thanks in advance.
05d8aae3736c731bcfc2c2a5c2a5f316?d=identicon&s=25 Rob Lacey (braindeaf)
on 2009-01-31 13:09
(Received via mailing list)
I would always go for A

def commified_name
  "#{self.firstname} #{self.last_name}"
end

It keeps the formatting in one place, no need for an additional helper
method or indeed a helper class. I'd only really use a helper method to
encapsulate repeated code in views. Just suppose you wanted to see the
commified name in the console or use it to perform some kind of
validation, etc.

RobL
18607dc7098d879b13f5ed22a70c4302?d=identicon&s=25 Eric Milford (ericindc)
on 2009-01-31 15:43
(Received via mailing list)
Thanks for the reply.

"I'd only really use a helper method to encapsulate repeated code in
views."

But isn't that the case here?  I repeatedly use the commified version
of the name inside of my views.  So why wouldn't C be the best
solution? Wouldn't using A break that convention?

I also use the commified version of the name inside of a
collection_select statement, so that the displayed names in the
dropdown menu are "last_name, first_name".  The only way I've found to
do that is to have a method inside of my Project model that returns
the commified name, even though the collection_select is inside of my
Project View.

So I guess given that, C is the best solution to avoid repetition
(commified_name in Model and Helper)?

Thanks
18607dc7098d879b13f5ed22a70c4302?d=identicon&s=25 Eric Milford (ericindc)
on 2009-01-31 15:47
(Received via mailing list)
Oops, I meant A (in the Model) is the best answer, not C.
05d8aae3736c731bcfc2c2a5c2a5f316?d=identicon&s=25 Rob Lacey (braindeaf)
on 2009-01-31 16:18
(Received via mailing list)
Hi there,

Ok that statement was probably a bit ambiguous. I'd use a view helper to
encapsulate something that is purely view based. I'd say that
commified_name isn't confined to the view, its legitimately an attribute
of the model - model attributes aren't confined to database attributes.
I don't think that would break convention at all. The convention is
there to make things easier to manage, separating that logic into a view
helper would make it, in my eyes less easy to manage.

Generally I would go with whatever solution feels more natural, and
easier to manage for you. Don't get bogged down in convention if it
doesn't work for you or feels wrong.

RobL
http://www.robl.me
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-31 16:56
(Received via mailing list)
> Generally I would go with whatever solution feels more natural, and
> easier to manage for you. Don't get bogged down in convention if it
> doesn't work for you or feels wrong.

I prefer to say that like: Put the code where it's easiest to test.

Models are easier to test than controllers, so put the most business
logic
there. Views are hardest to test, so put lots of untestable art there!
05d8aae3736c731bcfc2c2a5c2a5f316?d=identicon&s=25 Rob Lacey (braindeaf)
on 2009-01-31 17:34
(Received via mailing list)
Agreed its easier to test models, or at least I find it easier. I'd say
that its a fortunate side effect though, rather than a reason to test in
a certain way. Although I guess if it really is too hard to test any
part of your app then you're probably trying to solve the problem in the
wrong way. I never test my views to be honest, it seems that its going
just a step too far and it tends to be fairly fragile at that.

RobL


 > I prefer to say that like: Put the code where it's easiest to test.
 > Models are easier to test than controllers, so put the most business
logic there. Views are hardest to test, so put lots of untestable art
there!
18607dc7098d879b13f5ed22a70c4302?d=identicon&s=25 Eric Milford (ericindc)
on 2009-01-31 18:22
(Received via mailing list)
Thanks for the replies, fellas.  I've moved the code into my Model.
Thanks again.
F5b3c1ebfb2e9fc5f67bb48b119f6054?d=identicon&s=25 Randy Kramer (Guest)
on 2009-01-31 19:10
(Received via mailing list)
On Friday 30 January 2009 09:31 pm, ericindc wrote:
> commified_name

Off the point, but before we allow a word like commified to escape into
the wild (see usage in next post by Rob Lacey), we spell it commafied
instead--there is a slightly greater chance of guessing its intended
meaning.

Randy Kramer
--
I didn't have time to write a short letter, so I created a video
instead.--with apologies to Cicero, et.al.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-01-31 19:15
(Received via mailing list)
On Sat, Jan 31, 2009 at 10:06 AM, Randy Kramer <rhkramer@gmail.com>
wrote:

> Off the point, but before we allow a word like commified to escape into
> the wild (see usage in next post by Rob Lacey), we spell it commafied
> instead--there is a slightly greater chance of guessing its intended
> meaning.

And even greater:  `acts_like_last_name_comma_first_name`  :-)

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
This topic is locked and can not be replied to.