Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Rails] Re: DRY? Converting Boolean to Yes/No


  • From: wycats at gmail.com (Yehuda Katz)
  • Subject: [Rails] Re: DRY? Converting Boolean to Yes/No
  • Date: Mon Jul 17 21:53:08 2006

You could try something like:
def change_address?
  self.change_address ? "Yes" : "No"
end

in the model.

Steve Ross wrote:
> I have a database column given as:
> 
> t.column :change_address, :boolean
> 
> The user interacts with this using a checkbox, but wants confirmations 
> to
> read:
> 
> Change address: Yes
> 
> In an isolated case something like:
> 
> @obj.change_address ? 'Yes' : 'No'
> 
> Would work fine but this is peppered throughout scads of forms and 
> boolean
> fields. The first step I took at DRYing this up was:
> 
> class TrueClass
>   def to_s
>     "Yes"
>   end
> end
> 
> class FalseClass
>   def to_s
>     "No"
>   end
> end
> 
> But this breaks other uses. For example observe_field constructs the
> observers based on the to_s conversion of boolean attributes.
> 
> Any ideas on how to DRY this usage up?
> 
> Thanks
> --
> View this message in context: 
> http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5368812
> Sent from the RubyOnRails Users forum at Nabble.com.


-- 
Posted via http://www.ruby-forum.com/.