Created
April 9, 2011 14:41
-
-
Save JulianKniephoff/911442 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 hidden or 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 | |
}.merge options | |
boolean = !!value | |
key = boolean.to_s.to_sym | |
t(options[key], :scope => options[:scope], :locale => options[:locale]) | |
end | |
end |
This file contains hidden or 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