mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Cache model name lookup as it is specific to the form builder.
This commit is contained in:
parent
40f81c2da1
commit
01deec9303
2 changed files with 36 additions and 30 deletions
|
@ -2,6 +2,12 @@ module SimpleForm
|
|||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
attr_reader :template, :object_name, :object, :wrapper
|
||||
|
||||
# When action is create or update, we still should use new and edit
|
||||
ACTIONS = {
|
||||
:create => :new,
|
||||
:update => :edit
|
||||
}
|
||||
|
||||
extend MapType
|
||||
include SimpleForm::Inputs
|
||||
|
||||
|
@ -298,6 +304,34 @@ module SimpleForm
|
|||
SimpleForm::ErrorNotification.new(self, options).render
|
||||
end
|
||||
|
||||
# Extract the model names from the object_name mess, ignoring numeric and
|
||||
# explicit child indexes.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# route[blocks_attributes][0][blocks_learning_object_attributes][1][foo_attributes]
|
||||
# ["route", "blocks", "blocks_learning_object", "foo"]
|
||||
#
|
||||
def lookup_model_names
|
||||
@lookup_model_names ||= begin
|
||||
child_index = options[:child_index]
|
||||
names = object_name.to_s.scan(/([a-zA-Z_]+)/).flatten
|
||||
names.delete(child_index) if child_index
|
||||
names.each { |name| name.gsub!('_attributes', '') }
|
||||
names.freeze
|
||||
end
|
||||
end
|
||||
|
||||
# The action to be used in lookup.
|
||||
def lookup_action
|
||||
@lookup_action ||= begin
|
||||
action = template.controller.action_name
|
||||
return unless action
|
||||
action = action.to_sym
|
||||
ACTIONS[action] || action
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Find an input based on the attribute name.
|
||||
|
|
|
@ -3,12 +3,6 @@ module SimpleForm
|
|||
class Base
|
||||
extend I18nCache
|
||||
|
||||
# When action is create or update, we still should use new and edit
|
||||
ACTIONS = {
|
||||
:create => :new,
|
||||
:update => :edit
|
||||
}
|
||||
|
||||
include SimpleForm::Helpers::Required
|
||||
include SimpleForm::Helpers::Disabled
|
||||
include SimpleForm::Helpers::Validators
|
||||
|
@ -32,7 +26,7 @@ module SimpleForm
|
|||
attr_reader :attribute_name, :column, :input_type, :reflection,
|
||||
:options, :input_html_options
|
||||
|
||||
delegate :template, :object, :object_name, :to => :@builder
|
||||
delegate :template, :object, :object_name, :lookup_model_names, :lookup_action, :to => :@builder
|
||||
|
||||
def initialize(builder, attribute_name, column, input_type, options = {})
|
||||
@builder = builder
|
||||
|
@ -123,7 +117,7 @@ module SimpleForm
|
|||
def translate(namespace, default='')
|
||||
return nil unless SimpleForm.translate
|
||||
|
||||
model_names = lookup_model_names
|
||||
model_names = lookup_model_names.dup
|
||||
lookups = []
|
||||
|
||||
while !model_names.empty?
|
||||
|
@ -139,28 +133,6 @@ module SimpleForm
|
|||
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
|
||||
end
|
||||
|
||||
# Extract the model names from the object_name mess, ignoring numeric and
|
||||
# explicit child indexes.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# route[blocks_attributes][0][blocks_learning_object_attributes][1][foo_attributes]
|
||||
# ["route", "blocks", "blocks_learning_object", "foo"]
|
||||
#
|
||||
def lookup_model_names
|
||||
child_index = @builder.options[:child_index]
|
||||
names = object_name.to_s.scan(/([a-zA-Z_]+)/).flatten
|
||||
names.delete(child_index) if child_index
|
||||
names.each { |name| name.gsub!('_attributes', '') }
|
||||
end
|
||||
|
||||
# The action to be used in lookup.
|
||||
def lookup_action
|
||||
action = template.controller.action_name
|
||||
return unless action
|
||||
action = action.to_sym
|
||||
ACTIONS[action] || action
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue