Merge branch 'master' of https://github.com/klobuczek/simple_form into klobuczek-master
Conflicts: Gemfile README.md lib/simple_form/inputs/base.rb lib/simple_form/inputs/collection_input.rb test/support/models.rb
This commit is contained in:
commit
b4e297f98c
|
@ -484,6 +484,11 @@ SimpleForm uses all power of I18n API to lookup labels, hints and placeholders.
|
|||
user:
|
||||
username: 'Your username'
|
||||
password: '****'
|
||||
options:
|
||||
user:
|
||||
gender:
|
||||
male: 'Male'
|
||||
female: "Female'
|
||||
```
|
||||
|
||||
And your forms will use this information to render the components for you.
|
||||
|
@ -521,6 +526,10 @@ This way SimpleForm will figure out the right translation for you, based on the
|
|||
defaults:
|
||||
username: 'Your username'
|
||||
password: '****'
|
||||
options:
|
||||
gender:
|
||||
male: 'Male'
|
||||
female: "Female'
|
||||
```
|
||||
|
||||
SimpleForm will always look for a default attribute translation under the "defaults" key if no specific is found inside the model key.Note that this syntax is different from 1.x. To migrate to the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
|
||||
|
|
|
@ -144,6 +144,7 @@ module SimpleForm
|
|||
end
|
||||
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
|
||||
lookups << :"defaults.#{attribute_name}"
|
||||
lookups.map! {|lookup| :"#{lookup}.#{default}"} if namespace == :options
|
||||
lookups << default
|
||||
|
||||
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
|
||||
|
|
|
@ -66,7 +66,10 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def detect_common_display_methods(collection_classes = detect_collection_classes)
|
||||
if collection_classes.include?(Array)
|
||||
if collection_classes == [Symbol]
|
||||
translate_collection
|
||||
{ :label => :first, :value => :last }
|
||||
elsif collection_classes.include?(Array)
|
||||
{ :label => :first, :value => :last }
|
||||
elsif collection_includes_basic_objects?(collection_classes)
|
||||
{ :label => :to_s, :value => :to_s }
|
||||
|
@ -87,6 +90,10 @@ module SimpleForm
|
|||
String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass
|
||||
]).any?
|
||||
end
|
||||
|
||||
def translate_collection
|
||||
@collection = collection.map {|value| [translate(:options, value.to_s), value.to_s]}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -55,6 +55,17 @@ class CollectionInputTest < 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']
|
||||
|
@ -147,6 +158,18 @@ class CollectionInputTest < ActionView::TestCase
|
|||
assert_select 'label.collection_radio', 'Carlos'
|
||||
end
|
||||
|
||||
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']
|
||||
|
|
|
@ -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, :credit_card
|
||||
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender
|
||||
|
||||
def initialize(options={})
|
||||
@new_record = false
|
||||
|
|
Loading…
Reference in New Issue