Make :create and :update map to :new and :edit.

This commit is contained in:
José Valim 2009-12-11 18:32:57 -02:00
parent 6eb04705c1
commit 9add5b8c10
3 changed files with 31 additions and 3 deletions

View File

@ -11,6 +11,12 @@ module SimpleForm
delegate :template, :object, :object_name, :attribute, :column, delegate :template, :object, :object_name, :attribute, :column,
:reflection, :input_type, :options, :to => :@builder :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 def self.basename
@basename ||= name.split("::").last.underscore.to_sym @basename ||= name.split("::").last.underscore.to_sym
end end
@ -88,14 +94,19 @@ module SimpleForm
# #
# Take a look at our locale example file. # Take a look at our locale example file.
def translate(default='') def translate(default='')
action = template.params[:action] if template.respond_to?(:params) lookups = []
lookups = [] lookups << :"#{object_name}.#{lookup_action}.#{reflecion_name_or_attribute}"
lookups << :"#{object_name}.#{action}.#{reflecion_name_or_attribute}" if action.present?
lookups << :"#{object_name}.#{reflecion_name_or_attribute}" lookups << :"#{object_name}.#{reflecion_name_or_attribute}"
lookups << :"#{reflecion_name_or_attribute}" lookups << :"#{reflecion_name_or_attribute}"
lookups << default lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{basename.to_s.pluralize}", :default => lookups) I18n.t(lookups.shift, :scope => :"simple_form.#{basename.to_s.pluralize}", :default => lookups)
end 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 end
end end

View File

@ -46,6 +46,18 @@ class LabelTest < ActionView::TestCase
end end
test 'label should use i18n based on model, action, and attribute to lookup translation' do 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 => { store_translations(:en, :simple_form => { :labels => { :user => {
:new => { :description => 'Nova descrição' } :new => { :description => 'Nova descrição' }
} } } ) do } } } ) do

View File

@ -1,4 +1,9 @@
class MockController class MockController
attr_accessor :action_name
def action_name
@action_name || "edit"
end
def url_for(*args) def url_for(*args)
"http://example.com" "http://example.com"