-
-
Save hron84/1649915 to your computer and use it in GitHub Desktop.
Helper method to format a boolean value. See http://juliankniephoff.wordpress.com/2011/04/09/formatting-boolean-values-in-rails for some documentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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