I have a view that is accessing attibutes from a model
.<%= @checklist.alt_escape_indicated %>
.<%= @checklist.sign_condition %>
Some of these are going to be booleans that I wanted displayed as ‘Yes” or “No”. I’ve written some code in my model class to do this:
def getter_decorator(attr)
var = read_attribute(attr)
if !!var == var
boolean_as_string(var)
else
var
end
end
def boolean_as_string(bool_type)
if bool_type
"Yes"
else
"No"
end
end
So I can just do:
.<%= @checklist.boolean_as_string(@checklist.sign_condition) %>
for attributes I know are boolean or getter_decorator on everything. My question is. Is there a way I can decorate all of my getters so this function is called when I do
@checklist.sign_condition
I’d recommend making this a helper method since it’s solely for presentation. That’ll also make it easier to localize it in the future when your site takes off internationally…
And then use it in your view:
However, if you REALLY wanted bools to automatically show up as Yes/No, you could do something like:
I wouldn’t recommend that however 😉