diff --git a/lib/simple_form/components/base.rb b/lib/simple_form/components/base.rb index 91f7d62e..6b067c28 100644 --- a/lib/simple_form/components/base.rb +++ b/lib/simple_form/components/base.rb @@ -11,6 +11,12 @@ module SimpleForm delegate :template, :object, :object_name, :attribute, :column, :reflection, :input_type, :options, :to => :@builder + # When action is create or update, we still should use new and edit + ACTIONS = { + :create => :new, + :update => :edit + } + def self.basename @basename ||= name.split("::").last.underscore.to_sym end @@ -88,14 +94,19 @@ module SimpleForm # # Take a look at our locale example file. def translate(default='') - action = template.params[:action] if template.respond_to?(:params) - lookups = [] - lookups << :"#{object_name}.#{action}.#{reflecion_name_or_attribute}" if action.present? + lookups = [] + lookups << :"#{object_name}.#{lookup_action}.#{reflecion_name_or_attribute}" 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 + + # The action to be used in lookup. + def lookup_action + action = template.controller.action_name.to_sym + ACTIONS[action] || action + end end end end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 290f52b8..eb92c416 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -46,6 +46,18 @@ class LabelTest < ActionView::TestCase end test 'label should use i18n based on model, action, and attribute to lookup translation' do + @controller.action_name = "new" + store_translations(:en, :simple_form => { :labels => { :user => { + :new => { :description => 'Nova descrição' } + } } } ) do + params.merge!(:action => 'new') + with_label_for @user, :description, :text + assert_select 'label[for=user_description]', /Nova descrição/ + end + end + + test 'label should fallback to new when action is create' do + @controller.action_name = "create" store_translations(:en, :simple_form => { :labels => { :user => { :new => { :description => 'Nova descrição' } } } } ) do diff --git a/test/support/mock_controller.rb b/test/support/mock_controller.rb index 030eb203..af71e915 100644 --- a/test/support/mock_controller.rb +++ b/test/support/mock_controller.rb @@ -1,4 +1,9 @@ class MockController + attr_accessor :action_name + + def action_name + @action_name || "edit" + end def url_for(*args) "http://example.com"