Inputs lookup i18n to find hints. Update labels scope to simple_form.

This commit is contained in:
Carlos Antonio da Silva 2009-12-08 16:58:38 -02:00
parent 27e3e38c05
commit 4a54e4ecd6
4 changed files with 47 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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