DRY? Converting Boolean to Yes/No

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.

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

in the model.

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

I created a ‘prettifier’ plugin that extended TrueClass and FalseClass
with a ‘prettier’ method that gave me a Yes/No response. More useful if
you need the Yes/No to be available across the board. Yehuda’s response
should be sufficient if you just need it for one column.

Yes, this handles the individual case in questions, but I have dozens of
models and many of them have one or more boolean attributes. The larger
question, then, is how to generalize the pattern?

Thanks

wycats wrote:

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


View this message in context:
http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369236
Sent from the RubyOnRails Users forum at Nabble.com.

Where is the plugin or could you post relevant code?

Philip S. wrote:

I created a ‘prettifier’ plugin that extended TrueClass and FalseClass
with a ‘prettier’ method that gave me a Yes/No response.

View this message in context:
http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356
Sent from the RubyOnRails Users forum at Nabble.com.

./script/generate plugin pretty_boolean

And then modify/create the following two files:

<RAILS_APP>/vendor/plugins/pretty_boolean/init.rb

Include hook code here

require ‘pretty_boolean’

End init.rb

<RAILS_APP>/vendor/plugins/pretty_boolean/lib/pretty_boolean.rb

PrettyBoolean

module PrettyBoolean
def prettier
self ? “yes” : “no”
end
end

class FalseClass
include PrettyBoolean
end

class TrueClass
include PrettyBoolean
end

End PrettyBoolean

and, as long as the plugin is working, you can take any boolean you want
and go
some_boolean.prettier

and it’ll return “yes” or “no”

Steve R. wrote:

Where is the plugin or could you post relevant code?

Philip S. wrote:

I created a ‘prettifier’ plugin that extended TrueClass and FalseClass
with a ‘prettier’ method that gave me a Yes/No response.

View this message in context:
http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356
Sent from the RubyOnRails Users forum at Nabble.com.

I think that we’re posting in between eachother here… I just posted
the relevant code :smiley:

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