From f4c883e8fb4ab4a25c1d850dd658e19778651473 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 6 Feb 2013 10:42:20 +0000 Subject: [PATCH 01/10] Add :translate setting for include_blank and prompt --- lib/simple_form/inputs/collection_input.rb | 6 ++ test/inputs/collection_select_input_test.rb | 81 +++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 020b7e13..e40b211b 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -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 diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index c102696e..f10c595c 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -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 From d502ecf14b0f1d0c1e9e5eef8b25da95ca335e20 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 6 Feb 2013 11:06:38 +0000 Subject: [PATCH 02/10] Allow i18n key for include_blank and prompt --- lib/simple_form/inputs/collection_input.rb | 12 +++++++++++- test/inputs/collection_select_input_test.rb | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index e40b211b..91c3d73e 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -23,7 +23,7 @@ module SimpleForm options[:include_blank] = true unless skip_include_blank? [:prompt, :include_blank].each do |key| - options[key] = translate(key, true) if options[key] == :translate + translate_option options, key end options @@ -101,6 +101,16 @@ module SimpleForm true end end + + def translate_option(options, key) + return unless options[key].is_a? Symbol + + options[key] = if options[key] == :translate + translate(key, true) + else + I18n.t(options[key], scope: :"simple_form.#{key}") + end + end end end end diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index f10c595c..9b3395d0 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -105,6 +105,16 @@ class CollectionSelectInputTest < ActionView::TestCase end end + test 'input should translate include blank with a particular key' do + store_translations(:en, :simple_form => { :include_blank => { + :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_blank => { :user => { :age => 'Rather not say' @@ -165,6 +175,16 @@ class CollectionSelectInputTest < ActionView::TestCase end end + test 'input should translate prompt with a particular key' do + store_translations(:en, :simple_form => { :prompt => { + :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 => { :prompt => { :user => { :age => 'Select age:' From a7c3d520727eeb6a23007b8bb4edfc6b44e8e3f8 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 6 Feb 2013 11:21:53 +0000 Subject: [PATCH 03/10] Allow :t as a shorthand for :translate --- lib/simple_form/inputs/collection_input.rb | 3 ++- test/inputs/collection_select_input_test.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 91c3d73e..975bee1a 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -105,7 +105,8 @@ module SimpleForm def translate_option(options, key) return unless options[key].is_a? Symbol - options[key] = if options[key] == :translate + options[key] = case options[key] + when :translate, :t translate(key, true) else I18n.t(options[key], scope: :"simple_form.#{key}") diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index 9b3395d0..252b2fd0 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -105,6 +105,15 @@ class CollectionSelectInputTest < ActionView::TestCase end end + test 'input should translate include blank when set to :t' 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 => :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_blank => { :age => 'Rather not say', @@ -175,6 +184,15 @@ class CollectionSelectInputTest < ActionView::TestCase end end + test 'input should translate prompt when set to :t' do + store_translations(:en, :simple_form => { :prompt => { :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 => { :prompt => { :age => 'Select age:', From ef81ffe7135b1c01956f68cb234be1999f24df15 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 6 Feb 2013 11:36:30 +0000 Subject: [PATCH 04/10] Pluralize namespace for consistency --- lib/simple_form/inputs/collection_input.rb | 6 ++++-- test/inputs/collection_select_input_test.rb | 24 ++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 975bee1a..1ac15de1 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -105,11 +105,13 @@ module SimpleForm 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(key, true) + translate(namespace, true) else - I18n.t(options[key], scope: :"simple_form.#{key}") + I18n.t(options[key], scope: :"simple_form.#{namespace}") end end end diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index 252b2fd0..3c736a64 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -97,7 +97,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate include blank when set to :translate' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + 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 @@ -106,7 +106,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate include blank when set to :t' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + 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 @@ -115,7 +115,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate include blank with a particular key' do - store_translations(:en, :simple_form => { :include_blank => { + store_translations(:en, :simple_form => { :include_blanks => { :age => 'Rather not say', :user => { :age => 'Should be overridden' } } } ) do @@ -125,7 +125,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate include blank when set to a string' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + 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' @@ -134,7 +134,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate include blank when automatically set' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + store_translations(:en, :simple_form => { :include_blanks => { :user => { :age => 'Rather not say' } } } ) do with_input_for @user, :age, :select, :collection => 18..30 @@ -143,7 +143,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate include blank when set to true' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + 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 @@ -152,7 +152,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate include blank when set to false' do - store_translations(:en, :simple_form => { :include_blank => { :user => { + 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 @@ -176,7 +176,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate prompt when set to :translate' do - store_translations(:en, :simple_form => { :prompt => { :user => { + store_translations(:en, :simple_form => { :prompts => { :user => { :age => 'Select age:' } } } ) do with_input_for @user, :age, :select, :collection => 18..30, :prompt => :translate @@ -185,7 +185,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate prompt when set to :t' do - store_translations(:en, :simple_form => { :prompt => { :user => { + store_translations(:en, :simple_form => { :prompts => { :user => { :age => 'Select age:' } } } ) do with_input_for @user, :age, :select, :collection => 18..30, :prompt => :t @@ -194,7 +194,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate prompt with a particular key' do - store_translations(:en, :simple_form => { :prompt => { + store_translations(:en, :simple_form => { :prompts => { :age => 'Select age:', :user => { :age => 'Should be overridden' } } } ) do @@ -204,7 +204,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate prompt when set to a string' do - store_translations(:en, :simple_form => { :prompt => { :user => { + store_translations(:en, :simple_form => { :prompts => { :user => { :age => 'Select age:' } } } ) do with_input_for @user, :age, :select, :collection => 18..30, :prompt => 'Do it:' @@ -213,7 +213,7 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should not translate prompt when set to false' do - store_translations(:en, :simple_form => { :prompt => { :user => { + store_translations(:en, :simple_form => { :prompts => { :user => { :age => 'Select age:' } } } ) do with_input_for @user, :age, :select, :collection => 18..30, :prompt => false From 58ce72b99fe88bffcc914f9d6350e22fa707b092 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 6 Feb 2013 11:40:27 +0000 Subject: [PATCH 05/10] 1.9 hash syntax --- test/inputs/collection_select_input_test.rb | 82 ++++++++++----------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index 3c736a64..497ffeec 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -97,65 +97,65 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate include blank when set to :translate' do - store_translations(:en, :simple_form => { :include_blanks => { :user => { - :age => 'Rather not say' + 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 + 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' + 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 + 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' } + 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 + 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' + 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' + 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' + store_translations(:en, simple_form: { include_blanks: { user: { + age: 'Rather not say' } } } ) do - with_input_for @user, :age, :select, :collection => 18..30 + 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' + 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 + 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' + 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 + with_input_for @user, :age, :select, collection: 18..30, include_blank: false assert_no_select 'select option[value=]' end end @@ -176,56 +176,56 @@ class CollectionSelectInputTest < ActionView::TestCase end test 'input should translate prompt when set to :translate' do - store_translations(:en, :simple_form => { :prompts => { :user => { - :age => 'Select age:' + store_translations(:en, simple_form: { prompts: { user: { + age: 'Select age:' } } } ) do - with_input_for @user, :age, :select, :collection => 18..30, :prompt => :translate + 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:' + store_translations(:en, simple_form: { prompts: { user: { + age: 'Select age:' } } } ) do - with_input_for @user, :age, :select, :collection => 18..30, :prompt => :t + 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' } + 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 + 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:' + store_translations(:en, simple_form: { prompts: { user: { + age: 'Select age:' } } } ) do - with_input_for @user, :age, :select, :collection => 18..30, :prompt => 'Do it:' + 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:' + store_translations(:en, simple_form: { prompts: { user: { + age: 'Select age:' } } } ) do - with_input_for @user, :age, :select, :collection => 18..30, :prompt => false + 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:' + store_translations(:en, helpers: { select: { + prompt: 'Select value:' } } ) do - with_input_for @user, :age, :select, :collection => 18..30, :prompt => :translate + with_input_for @user, :age, :select, collection: 18..30, prompt: :translate assert_select 'select option[value=]', "Select value:" end end From 5ce1d04362c92d4e86cb99d1ba31aea739c0b6d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Apr 2014 15:22:09 -0300 Subject: [PATCH 06/10] Only translate prompts and include_blank if value is :translate --- lib/simple_form/inputs/collection_input.rb | 12 ++----- test/inputs/collection_select_input_test.rb | 40 ++++++--------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 57958997..e3ec5058 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -107,18 +107,12 @@ module SimpleForm end def translate_option(options, key) - return unless options[key].is_a? Symbol + if options[key] == :translate + namespace = key.to_s.pluralize - 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}") + options[key] = translate(namespace, true) end end end end end - diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index b8e41dd5..343046ec 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -105,27 +105,17 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should translate include blank when set to :translate' do store_translations(:en, simple_form: { include_blanks: { user: { age: 'Rather not say' - } } } ) do + } } }) 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: { + test 'input should translate include blank with a default' do + store_translations(:en, simple_form: { include_blanks: { defaults: { age: 'Rather not say', - user: { age: 'Should be overridden' } - } } ) do - with_input_for @user, :age, :select, collection: 18..30, include_blank: :age + } } }) do + with_input_for @user, :age, :select, collection: 18..30, include_blank: :translate assert_select 'select option[value=]', 'Rather not say' end end @@ -184,27 +174,17 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should translate prompt when set to :translate' do store_translations(:en, simple_form: { prompts: { user: { age: 'Select age:' - } } } ) do + } } }) 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: { + test 'input should translate prompt with a default' do + store_translations(:en, simple_form: { prompts: { defaults: { age: 'Select age:', - user: { age: 'Should be overridden' } - } } ) do - with_input_for @user, :age, :select, collection: 18..30, prompt: :age + } } }) do + with_input_for @user, :age, :select, collection: 18..30, prompt: :translate assert_select 'select option[value=]', 'Select age:' end end From d103329708075d38a2792d4ddc20a51908c6ef02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Apr 2014 15:22:45 -0300 Subject: [PATCH 07/10] :lipstick: --- test/inputs/collection_select_input_test.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/inputs/collection_select_input_test.rb b/test/inputs/collection_select_input_test.rb index 343046ec..90c33222 100644 --- a/test/inputs/collection_select_input_test.rb +++ b/test/inputs/collection_select_input_test.rb @@ -31,7 +31,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should do automatic collection translation for select types using defaults key' do store_translations(:en, simple_form: { options: { defaults: { gender: { male: 'Male', female: 'Female'} - } } } ) do + } } }) do with_input_for @user, :gender, :select, collection: [:male, :female] assert_select 'select.select#user_gender' assert_select 'select option', 'Male' @@ -42,7 +42,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should do automatic collection translation for select types using specific object key' do store_translations(:en, simple_form: { options: { user: { gender: { male: 'Male', female: 'Female'} - } } } ) do + } } }) do with_input_for @user, :gender, :select, collection: [:male, :female] assert_select 'select.select#user_gender' assert_select 'select option', 'Male' @@ -123,7 +123,7 @@ class CollectionSelectInputTest < ActionView::TestCase 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 + } } }) do with_input_for @user, :age, :select, collection: 18..30, include_blank: 'Young at heart' assert_select 'select option[value=]', 'Young at heart' end @@ -132,7 +132,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should not translate include blank when automatically set' do store_translations(:en, simple_form: { include_blanks: { user: { age: 'Rather not say' - } } } ) do + } } }) do with_input_for @user, :age, :select, collection: 18..30 assert_select 'select option[value=]', '' end @@ -141,7 +141,7 @@ class CollectionSelectInputTest < ActionView::TestCase 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 + } } }) do with_input_for @user, :age, :select, collection: 18..30, include_blank: true assert_select 'select option[value=]', '' end @@ -150,7 +150,7 @@ class CollectionSelectInputTest < ActionView::TestCase 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 + } } }) do with_input_for @user, :age, :select, collection: 18..30, include_blank: false assert_no_select 'select option[value=]' end @@ -158,7 +158,7 @@ class CollectionSelectInputTest < ActionView::TestCase 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=]', '' + assert_no_select 'select option[value=]' end test 'input should not set include blank if prompt is given' do @@ -192,7 +192,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should not translate prompt when set to a string' do store_translations(:en, simple_form: { prompts: { user: { age: 'Select age:' - } } } ) do + } } }) do with_input_for @user, :age, :select, collection: 18..30, prompt: 'Do it:' assert_select 'select option[value=]', 'Do it:' end @@ -201,7 +201,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should not translate prompt when set to false' do store_translations(:en, simple_form: { prompts: { user: { age: 'Select age:' - } } } ) do + } } }) do with_input_for @user, :age, :select, collection: 18..30, prompt: false assert_no_select 'select option[value=]' end @@ -210,7 +210,7 @@ class CollectionSelectInputTest < ActionView::TestCase test 'input should use Rails prompt translation as a fallback' do store_translations(:en, helpers: { select: { prompt: 'Select value:' - } } ) do + } }) do with_input_for @user, :age, :select, collection: 18..30, prompt: :translate assert_select 'select option[value=]', "Select value:" end From 08bf6c860e5509e0e91fb388f341ec7d95357308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Apr 2014 15:27:45 -0300 Subject: [PATCH 08/10] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4db83a17..e4444b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * Put a whitespace before the `inline_label` options of boolean input if it is present. * Add support to configure the `label_text` proc at the wrapper level. [@NOX73](https://github.com/NOX73) * `label_text` proc now receive three arguments (label, request, and if the label was explicit). [@timscott](https://github.com/timscott) + * Add I18n support to `:include_blank` and `:prompt` when `:translate` is used as value. [@haines](https://github.com/plataformatec/simple_form/pull/616) ### bug fix * Collection input that uses automatic collection translation properly sets checked values. From 61d5a42b1949a814853130d9502eb0461e50ab53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Apr 2014 15:27:52 -0300 Subject: [PATCH 09/10] Document new I18n keys in the generated config --- .../templates/config/locales/simple_form.en.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/generators/simple_form/templates/config/locales/simple_form.en.yml b/lib/generators/simple_form/templates/config/locales/simple_form.en.yml index 9810cea1..23743833 100644 --- a/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +++ b/lib/generators/simple_form/templates/config/locales/simple_form.en.yml @@ -10,7 +10,7 @@ en: # html: '*' error_notification: default_message: "Please review the problems below:" - # Labels and hints examples + # Examples # labels: # defaults: # password: 'Password' @@ -23,3 +23,9 @@ en: # defaults: # username: 'User name to sign in.' # password: 'No special characters, please.' + # include_blanks: + # defaults: + # age: 'Rather not say' + # prompts: + # defaults: + # age: 'Select your age' From 91476fa57d59df1e808df957a3b7c39043fd8c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Apr 2014 15:32:48 -0300 Subject: [PATCH 10/10] Document include_blank and prompt I18n on README --- README.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ded0ab9a..f817fbca 100644 --- a/README.md +++ b/README.md @@ -611,7 +611,7 @@ end ## I18n -**Simple Form** uses all power of I18n API to lookup labels, hints and placeholders. To customize your +**Simple Form** uses all power of I18n API to lookup labels, hints, prompts and placeholders. To customize your forms you can create a locale file like this: ```yaml @@ -629,12 +629,18 @@ en: user: username: 'Your username' password: '****' + include_blanks: + user: + age: 'Rather not say' + prompts: + gender: + age: 'Select your gender' ``` And your forms will use this information to render the components for you. -**Simple Form** also lets you be more specific, separating lookups through actions for labels, hints and -placeholders. Let's say you want a different label for new and edit actions, the locale file would +**Simple Form** also lets you be more specific, separating lookups through actions. +Let's say you want a different label for new and edit actions, the locale file would be something like: ```yaml @@ -673,13 +679,19 @@ en: ``` **Simple Form** 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}". +specific is found inside the model key. In addition, **Simple Form** will fallback to default `human_attribute_name` from Rails when no other translation is found for labels. Finally, you can also overwrite any label, hint or placeholder inside your view, just by passing the option manually. This way the I18n lookup will be skipped. +For `:prompt` and `:include_blank` the I18n lookup is optional and to enable it is necessary to pass +`:translate` as value. + +```ruby +f.input :gender, prompt: :translate +``` + **Simple Form** also has support for translating options in collection helpers. For instance, given a User with a `:gender` attribute, you might want to create a select box showing translated labels that would post either `male` or `female` as value. With **Simple Form** you could create an input