Skip to content

Instantly share code, notes, and snippets.

@hron84
Forked from JulianKniephoff/application_helper.rb
Created January 20, 2012 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hron84/1649915 to your computer and use it in GitHub Desktop.
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.
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