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.
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?
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.
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.
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.