Customizing Model Data -- Inside Model or View or Helper?


#1

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.


#2

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


#3

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


#4

Oops, I meant A (in the Model) is the best answer, not C.


#5

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


#6

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!


#7

Thanks for the replies, fellas. I’ve moved the code into my Model.
Thanks again.


#8

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 L.), we spell it commafied
instead–there is a slightly greater chance of guessing its intended
meaning.

Randy K.

I didn’t have time to write a short letter, so I created a video
instead.–with apologies to Cicero, et.al.


#9

On Sat, Jan 31, 2009 at 10:06 AM, Randy K. removed_email_address@domain.invalid
wrote:

Off the point, but before we allow a word like commified to escape into
the wild (see usage in next post by Rob L.), 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 :slight_smile:


Hassan S. ------------------------ removed_email_address@domain.invalid


#10

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!