From 9790f90d30e07a81501f1d3fc75afb3356254a55 Mon Sep 17 00:00:00 2001 From: Heinrich Klobuczek Date: Sun, 21 Aug 2011 22:36:00 -0700 Subject: [PATCH] added the ability to use i18n to translate collection values --- lib/simple_form/inputs/base.rb | 1 + lib/simple_form/inputs/collection_input.rb | 11 +++++++++- test/inputs_test.rb | 25 +++++++++++++++++++++- test/support/models.rb | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 9a3cbf0b..713bf886 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -174,6 +174,7 @@ module SimpleForm lookups << :"#{joined_model_names}.#{reflection_or_attribute_name}" end lookups << :"#{reflection_or_attribute_name}" + lookups.map! {|lookup| :"#{lookup}.#{default}"} if namespace == :options lookups << default I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index ef8e51e4..cfdd24cc 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -55,7 +55,12 @@ module SimpleForm value ||= common_method_for[:value] end - [label, value] + if SimpleForm.translate && [label, value] == [:to_s, :to_s] + translate_collection + [:first, :last] + else + [label, value] + end end def detect_common_display_methods @@ -82,6 +87,10 @@ module SimpleForm String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass ]).any? end + + def translate_collection + collection.map! {|value| [translate(:options, value.to_s), value.to_s]} + end end end end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index bd25c043..fa64b923 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -611,6 +611,17 @@ class InputTest < ActionView::TestCase assert_select 'select option', 'Carlos' end + test 'input should allow translating collection for select types' do + store_translations(:en, :simple_form => { :options => { + :gender => { :male => 'Male', :female => 'Female'} + } } ) do + with_input_for @user, :gender, :select, :collection => [:male, :female] + assert_select 'select.select#user_gender' + assert_select 'select option', 'Male' + assert_select 'select option', 'Female' + end + end + test 'input should mark the selected value by default' do @user.name = "Carlos" with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos'] @@ -687,7 +698,19 @@ class InputTest < ActionView::TestCase assert_select 'label.collection_radio', 'Carlos' end - test 'input should mark the current radio value by default' do + test 'input should allow translation of collection for radio types' do + store_translations(:en, :simple_form => { :options => { :user => { + :gender => { :male => 'Male', :female => 'Female'} + } } }) do + with_input_for @user, :gender, :radio, :collection => [:male, :female] + assert_select 'input[type=radio][value=male]' + assert_select 'input[type=radio][value=female]' + assert_select 'label.collection_radio', 'Male' + assert_select 'label.collection_radio', 'Female' + end + end + + test 'input should mark the current radio value by default' do @user.name = "Carlos" with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos'] assert_select 'input[type=radio][value=Carlos][checked=checked]' diff --git a/test/support/models.rb b/test/support/models.rb index a38d21b9..feb142b5 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -42,7 +42,7 @@ class User :description, :created_at, :updated_at, :credit_limit, :password, :url, :delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids, :avatar, :home_picture, :email, :status, :residence_country, :phone_number, - :post_count, :lock_version, :amount, :attempts, :action + :post_count, :lock_version, :amount, :attempts, :action, :gender def initialize(options={}) options.each do |key, value|