Labels and hints with associations always lookup to association name and not to attribute with _id

This commit is contained in:
Carlos Antonio da Silva 2009-12-11 02:00:37 -02:00
parent 5579a23736
commit 0c5f53fe74
5 changed files with 44 additions and 9 deletions

View File

@ -39,6 +39,11 @@ module SimpleForm
self.class.basename
end
# Find reflection name when available, otherwise use attribute
def reflecion_name_or_attribute
@refletion_name_or_attribute ||= (reflection ? reflection.name : attribute)
end
# Default html options for a component. Passed as a parameter for simple
# form component using component name as follows:
#
@ -85,9 +90,9 @@ module SimpleForm
def translate(default='')
action = template.params[:action] if template.respond_to?(:params)
lookups = []
lookups << :"#{object_name}.#{action}.#{attribute}" if action.present?
lookups << :"#{object_name}.#{attribute}"
lookups << :"#{attribute}"
lookups << :"#{object_name}.#{action}.#{reflecion_name_or_attribute}" if action.present?
lookups << :"#{object_name}.#{reflecion_name_or_attribute}"
lookups << :"#{reflecion_name_or_attribute}"
lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{basename.to_s.pluralize}", :default => lookups)
end

View File

@ -77,7 +77,7 @@ module SimpleForm
# available or just use the attribute itself humanized.
def translate_label
default = if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(attribute.to_s)
object.class.human_attribute_name(reflecion_name_or_attribute.to_s)
else
attribute.to_s.humanize
end

View File

@ -2,9 +2,10 @@ require 'test_helper'
class ErrorTest < ActionView::TestCase
def with_hint_for(object, attribute, type, options={})
def with_hint_for(object, attribute, type, options={}, setup_association=false, &block)
simple_form_for object do |f|
f.attribute = attribute
f.reflection = Association.new(Company, :company, {}) if setup_association
f.input_type = type
f.options = options
@ -66,6 +67,15 @@ class ErrorTest < ActionView::TestCase
end
end
test 'hint should use i18n with lookup for association name' do
store_translations(:en, :simple_form => { :hints => {
:user => { :company => 'My company!' }
} } ) do
with_hint_for @user, :company_id, :string, {}, true
assert_select 'span.hint', /My company!/
end
end
test 'hint should generate properly when object is not present' do
with_hint_for :project, :name, :string, :hint => 'Test without object'
assert_select 'span.hint', 'Test without object'

View File

@ -6,9 +6,10 @@ class LabelTest < ActionView::TestCase
SimpleForm::Components::Label.reset_i18n_cache :translate_required_html
end
def with_label_for(object, attribute, type, options={})
def with_label_for(object, attribute, type, options={}, setup_association=false)
simple_form_for object do |f|
f.attribute = attribute
f.reflection = Association.new(Company, :company, {}) if setup_association
f.input_type = type
f.options = options
@ -39,6 +40,11 @@ class LabelTest < ActionView::TestCase
assert_select 'label[for=user_description]', /User Description!/
end
test 'label should use human attribute name based on association name' do
with_label_for @user, :company, :string, {}, true
assert_select 'label', /Company Human Name!/
end
test 'label should use i18n based on model, action, and attribute to lookup translation' do
store_translations(:en, :simple_form => { :labels => { :user => {
:new => { :description => 'Nova descrição' }
@ -65,6 +71,15 @@ class LabelTest < ActionView::TestCase
end
end
test 'label should use i18n with lookup for association name' do
store_translations(:en, :simple_form => { :labels => {
:user => { :company => 'My company!' }
} } ) do
with_label_for @user, :company_id, :string, {}, true
assert_select 'label[for=user_company_id]', /My company!/
end
end
test 'label should have css class from type' do
with_label_for @user, :name, :string
assert_select 'label.string'

View File

@ -45,9 +45,14 @@ class User < OpenStruct
def self.human_attribute_name(attribute)
case attribute
when 'name' then 'Super User Name!'
when 'description' then 'User Description!'
else attribute.humanize
when 'name'
'Super User Name!'
when 'description'
'User Description!'
when 'company'
'Company Human Name!'
else
attribute.humanize
end
end