Ruby on Rails Tuesday, June 1, 2010

Matt Royer wrote:
> Colin Law wrote:
>> No, that an instance method of Order so will only work on Order
>> objects. It will work in any view (associated with any controller)
>> but only if you have an Order object.
>
> Oh, okay. Little by little (very little it seems so far) I'm getting the
> hang of this. Man, this stuff is daunting, but cool to work with.
>
> Thanks again Colin.

Personally, I like Colin's approach the best. Yes, while you might want
to add leading zeros in various places the example you showed seems
specific to instances of Order.

> "#{order.id}".leading_zeros + "#{order.name}"

The implementation of this would be best hidden (encapsulated) inside
the Order class. If you need other classes to implement similar behavior
then implement something similar inside those classes as well. In my
opinion this is similar to overriding the to_s method to provide class
specific behavior.

> def order_code
> "#{'%05d' % id} #{name}"
> end
> Then in the view just use
> <%= @order.order_code %>

This seems to be a very clean implementation and would be my
recommendation. Given the simplicity of the "%" formatter operator I see
no need to re-implement it as a helper. Combining it with the addition
of appending the name does make sense to factor into a method of the
Order class.
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment