DRY version of RoR book PAYMENT_TYPES example

In the book there is an example how to convert DB payment value to more
readable from. PAYMENT_TYPES array is defined and then in your views you
can use it as Order::PAYMENT_TYPES.

The problem is: how should I convert DB values (for example with type
char(1)) to full string representation. I know that I can add a Hash to
a model: IM = {‘S’ => ‘Skype’, ‘A’ => ‘AIM’}, and in views call it: <%=
User::IM[user.communicator] %>. But I would be more DRY if in views you
could use: <%= user.communicator_to_m %>.

This shouldn’t be difficult to implement:

  • add Hash to a model: FIELDNAME_TYPES = [[‘S’,‘Skype’],[‘A’,‘AIM’]]
  • create additional methods like fieldname_to_m which will get a value
    and convert it to human readable form using model’s array/hash

Before I try to hack RoR I would like to get some feedback to learn if
above approach is good. Maybe the problem is already solved in a
different way or there is a plug-in which implement that idea.

Regards,

Hi –

On Wed, 12 Jul 2006, Grzegorz D. wrote:

This shouldn’t be difficult to implement:

  • add Hash to a model: FIELDNAME_TYPES = [[‘S’,‘Skype’],[‘A’,‘AIM’]]
  • create additional methods like fieldname_to_m which will get a value
    and convert it to human readable form using model’s array/hash

Before I try to hack RoR I would like to get some feedback to learn if
above approach is good. Maybe the problem is already solved in a
different way or there is a plug-in which implement that idea.

I’m not sure why calling a method is less repetitive than grabbing a
hash value (which, after all, is also a method-call). Can you
clarify?

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me

Grzegorz D. wrote:

This shouldn’t be difficult to implement:

  • add Hash to a model: FIELDNAME_TYPES = [[‘S’,‘Skype’],[‘A’,‘AIM’]]
  • create additional methods like fieldname_to_m which will get a value
    and convert it to human readable form using model’s array/hash

Check out the enumerations_mixin. It lets you say something like:

User.gender = Gender[:Male]

And since the data is in the database you can use it in queries to
dereference the values on reports, etc. And you can add additional data
to the model.

http://somethinglearned.com/articles/2005/10/25/announcement-new-rails-plugin

http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/README_ENUMERATIONS

Matt G.

On Jul 12, 2006, at 4:44 AM, [email protected] wrote:

Hash to
a model: IM = {‘S’ => ‘Skype’, ‘A’ => ‘AIM’}, and in views call
it: <%=
User::IM[user.communicator] %>. But I would be more DRY if in
views you
could use: <%= user.communicator_to_m %>.

I’m not sure why calling a method is less repetitive than grabbing a
hash value (which, after all, is also a method-call). Can you
clarify?

Because grabbing the hash value violates OO encapsulation by exposing
the internal data structure of the object.


– Tom M.

I’m not sure why calling a method is less repetitive than grabbing a
hash value (which, after all, is also a method-call). Can you
clarify?

What about something like this:

class ActiveRecord::Base
def lm(arg)
self.class.const_get(arg.to_s.upcase + “_HASH”)[self.send(arg)]
end
end

and in the model then

class Order < ActiveRecord::Base

PAY_TYPE_HASH = { “ch” => “Check”,
“cc” => “Credit card”,
“po” => “Purchase order” }

end

and finally in the view

<%= @order.lm(:pay_type) %>

This is much simpler than adding a new method for every attribute. Any
other ideas?

I’m not sure why calling a method is less repetitive than grabbing a
hash value (which, after all, is also a method-call). Can you
clarify?

For example:

<%= Order::PAYMENT_TYPES[order.pay_type] %> …
<%= Order::DELIVERY_TYPES[order.delivary] %> …
<%= Order::CREDIT_CARD_TYPES[order.credit_card_type] %>

vs

<%= order.pay_type_to_m %> …
<%= order.delivary_to_m %> …
<%= order.credit_card_type_to_m %>

You don’t have to type model and hash names. I think this is less
repetitive in terms of code to write in views, not in terms of code
which is executed underneath. That’s all.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs