Permalink
Browse files

use I18n to translate default boolean options

  • Loading branch information...
1 parent ec83447 commit d140a9bd4353e60ad8fcb022e3bfb5088d177d41 @nashby committed Mar 4, 2012
Showing with 17 additions and 5 deletions.
  1. +6 −5 lib/simple_form/inputs/collection_input.rb
  2. +11 −0 test/inputs/collection_select_input_test.rb
@@ -5,10 +5,11 @@ class CollectionInput < Base
# collection is given. Always fallback to this boolean collection.
# Texts can be translated using i18n in "simple_form.yes" and
# "simple_form.no" keys. See the example locale file.
- def self.boolean_collection
- i18n_cache :boolean_collection do
- [ [I18n.t(:"simple_form.yes", :default => 'Yes'), true],
- [I18n.t(:"simple_form.no", :default => 'No'), false] ]
+ def boolean_collection
+ self.class.i18n_cache :boolean_collection do
+ namespace = :"simple_form.options.defaults"
+ [ [I18n.t(:"simple_form.yes", :default => [:"#{namespace}.#{attribute_name}.yes", 'Yes']), true],
+ [I18n.t(:"simple_form.no", :default => [:"#{namespace}.#{attribute_name}.no", 'No']), false] ]
end
end
@@ -27,7 +28,7 @@ def input_options
def collection
@collection ||= begin
- collection = options.delete(:collection) || self.class.boolean_collection
+ collection = options.delete(:collection) || boolean_collection
collection.respond_to?(:call) ? collection.call : collection.to_a
end
end
@@ -21,6 +21,17 @@ class CollectionSelectInputTest < ActionView::TestCase
end
end
+ test 'input as select should use i18n to translate select boolean options for given attribute' do
+ store_translations(:en, :simple_form => { :options => { :defaults => {
+ :active => { :yes => "I think so", :no => "I don't think so"}
+ } } } ) do
+ with_input_for @user, :active, :select
+ assert_select 'select.select#user_active'
+ assert_select 'select option[value=true]', 'I think so'
+ assert_select 'select option[value=false]', "I don't think so"
+ end
+ end
+
test 'input should allow overriding collection for select types' do
with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
assert_select 'select.select#user_name'

0 comments on commit d140a9b

Please sign in to comment.