mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Add :translate setting for include_blank and prompt
This commit is contained in:
parent
779a009027
commit
f4c883e8fb
2 changed files with 87 additions and 0 deletions
|
@ -19,7 +19,13 @@ module SimpleForm
|
|||
|
||||
def input_options
|
||||
options = super
|
||||
|
||||
options[:include_blank] = true unless skip_include_blank?
|
||||
|
||||
[:prompt, :include_blank].each do |key|
|
||||
options[key] = translate(key, true) if options[key] == :translate
|
||||
end
|
||||
|
||||
options
|
||||
end
|
||||
|
||||
|
|
|
@ -96,6 +96,51 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|||
assert_select 'select option[value=]', ''
|
||||
end
|
||||
|
||||
test 'input should translate include blank when set to :translate' do
|
||||
store_translations(:en, :simple_form => { :include_blank => { :user => {
|
||||
:age => 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :include_blank => :translate
|
||||
assert_select 'select option[value=]', 'Rather not say'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate include blank when set to a string' do
|
||||
store_translations(:en, :simple_form => { :include_blank => { :user => {
|
||||
:age => 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :include_blank => 'Young at heart'
|
||||
assert_select 'select option[value=]', 'Young at heart'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate include blank when automatically set' do
|
||||
store_translations(:en, :simple_form => { :include_blank => { :user => {
|
||||
:age => 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30
|
||||
assert_select 'select option[value=]', ''
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate include blank when set to true' do
|
||||
store_translations(:en, :simple_form => { :include_blank => { :user => {
|
||||
:age => 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :include_blank => true
|
||||
assert_select 'select option[value=]', ''
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate include blank when set to false' do
|
||||
store_translations(:en, :simple_form => { :include_blank => { :user => {
|
||||
:age => 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :include_blank => false
|
||||
assert_no_select 'select option[value=]'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not set include blank if otherwise is told' do
|
||||
with_input_for @user, :age, :select, collection: 18..30, include_blank: false
|
||||
assert_no_select 'select option[value=]', ''
|
||||
|
@ -111,6 +156,42 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|||
assert_no_select 'select option[value=]', ''
|
||||
end
|
||||
|
||||
test 'input should translate prompt when set to :translate' do
|
||||
store_translations(:en, :simple_form => { :prompt => { :user => {
|
||||
:age => 'Select age:'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :prompt => :translate
|
||||
assert_select 'select option[value=]', 'Select age:'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate prompt when set to a string' do
|
||||
store_translations(:en, :simple_form => { :prompt => { :user => {
|
||||
:age => 'Select age:'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :prompt => 'Do it:'
|
||||
assert_select 'select option[value=]', 'Do it:'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should not translate prompt when set to false' do
|
||||
store_translations(:en, :simple_form => { :prompt => { :user => {
|
||||
:age => 'Select age:'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :prompt => false
|
||||
assert_no_select 'select option[value=]'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should use Rails prompt translation as a fallback' do
|
||||
store_translations(:en, :helpers => { :select => {
|
||||
:prompt => 'Select value:'
|
||||
} } ) do
|
||||
with_input_for @user, :age, :select, :collection => 18..30, :prompt => :translate
|
||||
assert_select 'select option[value=]', "Select value:"
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should detect label and value on collections' do
|
||||
users = [ setup_new_user(id: 1, name: "Jose"), setup_new_user(id: 2, name: "Carlos") ]
|
||||
with_input_for @user, :description, :select, collection: users
|
||||
|
|
Loading…
Reference in a new issue