Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper method to format a boolean value. See http://juliankniephoff.wordpress.com/2011/04/09/formatting-boolean-values-in-rails for some documentation.
module ApplicationHelper
def b(value, options = {})
options = {
:true => :positive,
:false => :negative,
:scope => [:boolean],
:locale => I18n.locale, # <- It makes easy to avoid conditional run
}.merge options
# avoid passing nil, or some desctructive value
# what can be easily happened
boolean = !!(value || false)
key = boolean.to_s.to_sym
t(options[key], :scope => options[:scope], :locale => options[:locale])
end
end
require 'spec_helper'
describe ApplicationHelper do
context 'method b' do
it 'translates booleans correclty' do
I18n.backend.store_translations :en, :boolean => { :positive => 'yes', :negative => 'no' }
I18n.locale = :en
helper.b(true).should eql 'yes'
helper.b(false).should eql 'no'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.