# DRY version of RoR book PAYMENT\_TYPES example

**URL:** <https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074>\
**Category:** Rails\
**Created:** [July 12, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074 "2006-07-12T13:40:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 12, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/1 "2006-07-12T13:40:06Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 12, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/2 "2006-07-12T13:44:57Z")

</div>

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](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
[Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS, the Ruby book for  
Rails developers  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
removed\_email\_address@domain.invalid =\> me

---

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 12, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/3 "2006-07-12T14:17:23Z")

</div>

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://somethinglearned.com/articles/2005/10/25/announcement-new-rails-plugin)

[http://svn.protocool.com/rails/plugins/enumerations\_mixin/trunk/README\_ENUMERATIONS](http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/README_ENUMERATIONS)

Matt G.  
[http://mattgriffith.net](http://mattgriffith.net)

---

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 12, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/4 "2006-07-12T19:18:27Z")

</div>

On Jul 12, 2006, at 4:44 AM, removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 13, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/5 "2006-07-13T18:30:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gregd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@gregd](https://www.ruby-forum.com/u/gregd)\
**Post date:** [July 12, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/dry-version-of-ror-book-payment-types-example/98074/6 "2006-07-12T14:30:43Z")

</div>

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