diff --git a/lib/simple_form/hint.rb b/lib/simple_form/hint.rb index f19af066..1810e2e9 100644 --- a/lib/simple_form/hint.rb +++ b/lib/simple_form/hint.rb @@ -4,8 +4,17 @@ module SimpleForm private def generate_hint - return '' unless @options.key?(:hint) - @template.content_tag(:span, @options[:hint], :class => 'hint') + return '' if @options[:hint] == false || (text = hint_text).blank? + @template.content_tag(:span, text, :class => 'hint') + end + + def hint_text + @options[:hint] || translate_hint + end + + def translate_hint + lookups = [:"#{@object_name}.#{@attribute}", :"#{@attribute}", ''] + I18n.t(lookups.shift, :scope => :"simple_form.hints", :default => lookups) end end end diff --git a/lib/simple_form/label.rb b/lib/simple_form/label.rb index 5c7cc88b..45976b50 100644 --- a/lib/simple_form/label.rb +++ b/lib/simple_form/label.rb @@ -21,7 +21,7 @@ module SimpleForm def translate_label default = @object.try(:human_attribute_name, @attribute.to_s) || @attribute.to_s.humanize lookups = [:"#{@object_name}.#{@attribute}", :"#{@attribute}", default] - I18n.t(lookups.shift, :scope => :"views.labels", :default => lookups) + I18n.t(lookups.shift, :scope => :"simple_form.labels", :default => lookups) end def translate_required_string diff --git a/test/hint_test.rb b/test/hint_test.rb index ba71e1ae..a588e692 100644 --- a/test/hint_test.rb +++ b/test/hint_test.rb @@ -2,6 +2,13 @@ require 'test_helper' class ErrorTest < ActionView::TestCase + test 'input should not generate a hint by default' do + simple_form_for @user do |f| + concat f.input :name + end + assert_no_select 'form span.hint' + end + test 'input should allow generating a hint' do simple_form_for @user do |f| concat f.input :name, :hint => 'Use with care...' @@ -9,10 +16,32 @@ class ErrorTest < ActionView::TestCase assert_select 'form span.hint', 'Use with care...' end - test 'input should not generate a hint by default' do - simple_form_for @user do |f| - concat f.input :name + test 'input should use i18n to find hints based on model and attribute' do + store_translations(:en, :simple_form => { :hints => { :user => { :name => + 'Content of this input will be capitalized...' + } } }) do + simple_form_for @user do |f| + concat f.input :name + end + assert_select 'form span.hint', 'Content of this input will be capitalized...' + end + end + + test 'input should use i18n based only on attribute to pick up the label translation' do + store_translations(:en, :simple_form => { :hints => { :name => 'Name hint!' } } ) do + simple_form_for @user do |f| + concat f.input :name + end + assert_select 'form span.hint', 'Name hint!' + end + end + + test 'input should allow disabling a hint that exists in i18n' do + store_translations(:en, :simple_form => { :hints => { :name => 'Name hint!' } } ) do + simple_form_for @user do |f| + concat f.input :name, :hint => false + end + assert_no_select 'form span.hint' end - assert_no_select 'form span.hint' end end diff --git a/test/label_test.rb b/test/label_test.rb index 02c11567..43c10193 100644 --- a/test/label_test.rb +++ b/test/label_test.rb @@ -32,7 +32,7 @@ class LabelTest < ActionView::TestCase end test 'input should use i18n based on model name to pick up label translation' do - store_translations(:en, :views => { :labels => { :super_user => { + store_translations(:en, :simple_form => { :labels => { :super_user => { :description => 'Descrição', :age => 'Idade' } } } ) do @super_user = SuperUser.new @@ -46,7 +46,7 @@ class LabelTest < ActionView::TestCase end test 'input should use i18n based only on attribute to pick up the label translation' do - store_translations(:en, :views => { :labels => { :age => 'Idade' } } ) do + store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do simple_form_for @user do |f| concat f.input :age end