added the ability to use i18n to translate collection values

This commit is contained in:
Heinrich Klobuczek 2011-08-21 22:36:00 -07:00
parent 5d4021f6b5
commit 9790f90d30
4 changed files with 36 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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]'

View File

@ -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|