From 0c5f53fe742d9b33ec0d2abb2311582a410dc3d1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 11 Dec 2009 02:00:37 -0200 Subject: [PATCH] Labels and hints with associations always lookup to association name and not to attribute with _id --- lib/simple_form/components/base.rb | 11 ++++++++--- lib/simple_form/components/label.rb | 2 +- test/components/hint_test.rb | 12 +++++++++++- test/components/label_test.rb | 17 ++++++++++++++++- test/support/models.rb | 11 ++++++++--- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/simple_form/components/base.rb b/lib/simple_form/components/base.rb index 01fded76..91f7d62e 100644 --- a/lib/simple_form/components/base.rb +++ b/lib/simple_form/components/base.rb @@ -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 diff --git a/lib/simple_form/components/label.rb b/lib/simple_form/components/label.rb index 931a48ac..908a6fe2 100644 --- a/lib/simple_form/components/label.rb +++ b/lib/simple_form/components/label.rb @@ -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 diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index 9cc862a6..5d11c2da 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -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' diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 14d5f45b..290f52b8 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -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' diff --git a/test/support/models.rb b/test/support/models.rb index c4c974e7..89af9bb0 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -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