Merge pull request #616 from haines/include_blank_i18n
Add i18n for include_blank
This commit is contained in:
commit
0b4d83082a
|
@ -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|
|
||||
translate_option options, key
|
||||
end
|
||||
|
||||
options
|
||||
end
|
||||
|
||||
|
@ -99,6 +105,19 @@ module SimpleForm
|
|||
true
|
||||
end
|
||||
end
|
||||
|
||||
def translate_option(options, key)
|
||||
return unless options[key].is_a? Symbol
|
||||
|
||||
namespace = key.to_s.pluralize
|
||||
|
||||
options[key] = case options[key]
|
||||
when :translate, :t
|
||||
translate(namespace, true)
|
||||
else
|
||||
I18n.t(options[key], scope: :"simple_form.#{namespace}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -102,6 +102,70 @@ 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_blanks: { 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 translate include blank when set to :t' do
|
||||
store_translations(:en, simple_form: { include_blanks: { user: {
|
||||
age: 'Rather not say'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, collection: 18..30, include_blank: :t
|
||||
assert_select 'select option[value=]', 'Rather not say'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should translate include blank with a particular key' do
|
||||
store_translations(:en, simple_form: { include_blanks: {
|
||||
age: 'Rather not say',
|
||||
user: { age: 'Should be overridden' }
|
||||
} } ) do
|
||||
with_input_for @user, :age, :select, collection: 18..30, include_blank: :age
|
||||
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_blanks: { 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_blanks: { 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_blanks: { 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_blanks: { 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=]', ''
|
||||
|
@ -117,6 +181,61 @@ 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: { prompts: { 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 translate prompt when set to :t' do
|
||||
store_translations(:en, simple_form: { prompts: { user: {
|
||||
age: 'Select age:'
|
||||
} } } ) do
|
||||
with_input_for @user, :age, :select, collection: 18..30, prompt: :t
|
||||
assert_select 'select option[value=]', 'Select age:'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input should translate prompt with a particular key' do
|
||||
store_translations(:en, simple_form: { prompts: {
|
||||
age: 'Select age:',
|
||||
user: { age: 'Should be overridden' }
|
||||
} } ) do
|
||||
with_input_for @user, :age, :select, collection: 18..30, prompt: :age
|
||||
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: { prompts: { 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: { prompts: { 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 = [User.build(id: 1, name: "Jose"), User.build(id: 2, name: "Carlos")]
|
||||
with_input_for @user, :description, :select, collection: users
|
||||
|
|
Loading…
Reference in New Issue