Create a gist now

Instantly share code, notes, and snippets.

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
}.merge options
boolean = !!value
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