Rename attribute to attribute_name to allow f.attribute calls (for show_for compatibility).
This commit is contained in:
parent
c56feeb136
commit
28bc70e8f4
|
@ -8,7 +8,7 @@ module SimpleForm
|
|||
# The Base component is a raw component with some helpers and a default behavior
|
||||
# of prepending the content available in the method content.
|
||||
class Base
|
||||
delegate :template, :object, :object_name, :attribute, :column,
|
||||
delegate :template, :object, :object_name, :attribute_name, :column,
|
||||
:reflection, :input_type, :options, :to => :@builder
|
||||
|
||||
# When action is create or update, we still should use new and edit
|
||||
|
@ -46,8 +46,8 @@ module SimpleForm
|
|||
end
|
||||
|
||||
# Find reflection name when available, otherwise use attribute
|
||||
def reflecion_name_or_attribute
|
||||
@refletion_name_or_attribute ||= (reflection ? reflection.name : attribute)
|
||||
def reflection_or_attribute_name
|
||||
reflection ? reflection.name : attribute_name
|
||||
end
|
||||
|
||||
# Default html options for a component. Passed as a parameter for simple
|
||||
|
@ -96,9 +96,9 @@ module SimpleForm
|
|||
# Take a look at our locale example file.
|
||||
def translate(default='')
|
||||
lookups = []
|
||||
lookups << :"#{object_name}.#{lookup_action}.#{reflecion_name_or_attribute}"
|
||||
lookups << :"#{object_name}.#{reflecion_name_or_attribute}"
|
||||
lookups << :"#{reflecion_name_or_attribute}"
|
||||
lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}"
|
||||
lookups << :"#{object_name}.#{reflection_or_attribute_name}"
|
||||
lookups << :"#{reflection_or_attribute_name}"
|
||||
lookups << default
|
||||
I18n.t(lookups.shift, :scope => :"simple_form.#{basename.to_s.pluralize}", :default => lookups)
|
||||
end
|
||||
|
|
|
@ -13,7 +13,7 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def errors_on_attribute
|
||||
Array(object.errors[attribute])
|
||||
Array(object.errors[attribute_name])
|
||||
end
|
||||
|
||||
def errors_on_association
|
||||
|
|
|
@ -48,7 +48,7 @@ module SimpleForm
|
|||
mapping = self.class.mappings[input_type]
|
||||
raise "Invalid input type #{input_type.inspect}" unless mapping
|
||||
|
||||
args = [ attribute ]
|
||||
args = [ attribute_name ]
|
||||
apply_with_priority_behavior(args) if mapping.with_priority
|
||||
apply_collection_behavior(args) if mapping.collection
|
||||
apply_options_behavior(args) if mapping.options
|
||||
|
|
|
@ -35,19 +35,19 @@ module SimpleForm
|
|||
def content
|
||||
html_options = component_html_options
|
||||
html_options[:for] = options[:input_html][:id] if options.key?(:input_html)
|
||||
@builder.label(attribute_name, text, html_options)
|
||||
@builder.label(label_target, text, html_options)
|
||||
end
|
||||
|
||||
# Map attribute to specific name when dealing with date/time/timestamp,
|
||||
# ensuring label will always be "clickable". For better accessibility.
|
||||
def attribute_name
|
||||
def label_target
|
||||
case input_type
|
||||
when :date, :datetime
|
||||
"#{attribute}_1i"
|
||||
"#{attribute_name}_1i"
|
||||
when :time
|
||||
"#{attribute}_4i"
|
||||
"#{attribute_name}_4i"
|
||||
else
|
||||
attribute
|
||||
attribute_name
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -77,9 +77,9 @@ 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(reflecion_name_or_attribute.to_s)
|
||||
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
|
||||
else
|
||||
attribute.to_s.humanize
|
||||
attribute_name.to_s.humanize
|
||||
end
|
||||
|
||||
translate(default)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module SimpleForm
|
||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
attr_reader :template, :object_name, :object, :attribute, :column,
|
||||
attr_reader :template, :object_name, :object, :attribute_name, :column,
|
||||
:reflection, :input_type, :options
|
||||
|
||||
TERMINATOR = lambda { "" }
|
||||
|
@ -70,8 +70,8 @@ module SimpleForm
|
|||
# Some inputs, as :time_zone and :country accepts a :priority option. If none is
|
||||
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
|
||||
#
|
||||
def input(attribute, options={})
|
||||
define_simple_form_attributes(attribute, options)
|
||||
def input(attribute_name, options={})
|
||||
define_simple_form_attributes(attribute_name, options)
|
||||
|
||||
component = TERMINATOR
|
||||
SimpleForm.components.reverse.each do |klass|
|
||||
|
@ -80,6 +80,7 @@ module SimpleForm
|
|||
end
|
||||
component.call
|
||||
end
|
||||
alias :attribute :input
|
||||
|
||||
# Helper for dealing with association selects/radios, generating the
|
||||
# collection automatically. It's just a wrapper to input, so all options
|
||||
|
@ -112,11 +113,11 @@ module SimpleForm
|
|||
# f.association :company, :scope => [ :public, :not_broken ]
|
||||
# # Same as doing Company.public.not_broken.all
|
||||
#
|
||||
def association(attribute, options={})
|
||||
def association(association, options={})
|
||||
raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object
|
||||
|
||||
options[:as] ||= :select
|
||||
@reflection = find_association_reflection(attribute)
|
||||
@reflection = find_association_reflection(association)
|
||||
raise "Association #{attribute.inspect} not found" unless @reflection
|
||||
|
||||
case @reflection.macro
|
||||
|
@ -203,8 +204,8 @@ module SimpleForm
|
|||
# f.error :name
|
||||
# f.error :name, :id => "cool_error"
|
||||
#
|
||||
def error(attribute, options={})
|
||||
define_simple_form_attributes(attribute, :error_html => options)
|
||||
def error(attribute_name, options={})
|
||||
define_simple_form_attributes(attribute_name, :error_html => options)
|
||||
SimpleForm::Components::Error.new(self, TERMINATOR).call
|
||||
end
|
||||
|
||||
|
@ -218,9 +219,9 @@ module SimpleForm
|
|||
# f.hint :name, :id => "cool_hint"
|
||||
# f.hint "Don't forget to accept this"
|
||||
#
|
||||
def hint(attribute, options={})
|
||||
attribute, options[:hint] = nil, attribute if attribute.is_a?(String)
|
||||
define_simple_form_attributes(attribute, :hint => options.delete(:hint), :hint_html => options)
|
||||
def hint(attribute_name, options={})
|
||||
attribute_name, options[:hint] = nil, attribute_name if attribute_name.is_a?(String)
|
||||
define_simple_form_attributes(attribute_name, :hint => options.delete(:hint), :hint_html => options)
|
||||
SimpleForm::Components::Hint.new(self, TERMINATOR).call
|
||||
end
|
||||
|
||||
|
@ -237,10 +238,10 @@ module SimpleForm
|
|||
# f.label :name, :required => false
|
||||
# f.label :name, :id => "cool_label"
|
||||
#
|
||||
def label(attribute, *args)
|
||||
def label(attribute_name, *args)
|
||||
return super if args.first.is_a?(String)
|
||||
options = args.extract_options!
|
||||
define_simple_form_attributes(attribute, :label => options.delete(:label),
|
||||
define_simple_form_attributes(attribute_name, :label => options.delete(:label),
|
||||
:label_html => options, :required => options.delete(:required))
|
||||
SimpleForm::Components::Label.new(self, TERMINATOR).call
|
||||
end
|
||||
|
@ -248,10 +249,10 @@ module SimpleForm
|
|||
private
|
||||
|
||||
# Setup default simple form attributes.
|
||||
def define_simple_form_attributes(attribute, options) #:nodoc:
|
||||
def define_simple_form_attributes(attribute_name, options) #:nodoc:
|
||||
@options = options
|
||||
|
||||
if @attribute = attribute
|
||||
if @attribute_name = attribute_name
|
||||
@column = find_attribute_column
|
||||
@input_type = default_input_type
|
||||
end
|
||||
|
@ -270,7 +271,7 @@ module SimpleForm
|
|||
when :timestamp
|
||||
:datetime
|
||||
when :string, nil
|
||||
match = case @attribute.to_s
|
||||
match = case @attribute_name.to_s
|
||||
when /password/ then :password
|
||||
when /time_zone/ then :time_zone
|
||||
when /country/ then :country
|
||||
|
@ -284,18 +285,18 @@ module SimpleForm
|
|||
|
||||
# Checks if attribute is a file_method.
|
||||
def file_method? #:nodoc:
|
||||
file = @object.send(@attribute) if @object.respond_to?(@attribute)
|
||||
file = @object.send(@attribute_name) if @object.respond_to?(@attribute_name)
|
||||
:file if file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) }
|
||||
end
|
||||
|
||||
# Finds the database column for the given attribute
|
||||
def find_attribute_column #:nodoc:
|
||||
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
|
||||
@object.column_for_attribute(@attribute_name) if @object.respond_to?(:column_for_attribute)
|
||||
end
|
||||
|
||||
# Find association related to attribute
|
||||
def find_association_reflection(attribute) #:nodoc:
|
||||
@object.class.reflect_on_association(attribute) if @object.class.respond_to?(:reflect_on_association)
|
||||
def find_association_reflection(association) #:nodoc:
|
||||
@object.class.reflect_on_association(association) if @object.class.respond_to?(:reflect_on_association)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,12 +2,12 @@ require 'test_helper'
|
|||
|
||||
class ErrorTest < ActionView::TestCase
|
||||
|
||||
def with_error_for(object, attribute, type, options={}, &block)
|
||||
def with_error_for(object, attribute_name, type, options={}, &block)
|
||||
simple_form_for object do |f|
|
||||
f.attribute = attribute
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
f.attribute_name = attribute_name
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
error = SimpleForm::Components::Error.new(f, SimpleForm::FormBuilder::TERMINATOR)
|
||||
concat(error.call)
|
||||
|
|
|
@ -2,12 +2,12 @@ require 'test_helper'
|
|||
|
||||
class HintTest < ActionView::TestCase
|
||||
|
||||
def with_hint_for(object, attribute, type, options={}, &block)
|
||||
def with_hint_for(object, attribute_name, type, options={}, &block)
|
||||
simple_form_for object do |f|
|
||||
f.attribute = attribute
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
f.attribute_name = attribute_name
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
hint = SimpleForm::Components::Hint.new(f, SimpleForm::FormBuilder::TERMINATOR)
|
||||
concat(hint.call)
|
||||
|
|
|
@ -6,12 +6,12 @@ class InputTest < ActionView::TestCase
|
|||
SimpleForm::Components::Input.reset_i18n_cache :boolean_collection
|
||||
end
|
||||
|
||||
def with_input_for(object, attribute, type, options={})
|
||||
def with_input_for(object, attribute_name, type, options={})
|
||||
simple_form_for object do |f|
|
||||
f.attribute = attribute
|
||||
f.column = object.column_for_attribute(attribute) if object.respond_to?(:column_for_attribute)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
f.attribute_name = attribute_name
|
||||
f.column = object.column_for_attribute(attribute_name) if object.respond_to?(:column_for_attribute)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
input = SimpleForm::Components::Input.new(f, SimpleForm::FormBuilder::TERMINATOR)
|
||||
concat(input.call)
|
||||
|
|
|
@ -6,12 +6,12 @@ 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_name, type, options={})
|
||||
simple_form_for object do |f|
|
||||
f.attribute = attribute
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
f.attribute_name = attribute_name
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
label = SimpleForm::Components::Label.new(f, SimpleForm::FormBuilder::TERMINATOR)
|
||||
concat(label.call)
|
||||
|
|
|
@ -19,7 +19,7 @@ $:.unshift "#{File.dirname(__FILE__)}/support/country_select/lib"
|
|||
require 'country_select'
|
||||
|
||||
class SimpleForm::FormBuilder
|
||||
attr_accessor :attribute, :column, :reflection, :input_type, :options
|
||||
attr_accessor :attribute_name, :column, :reflection, :input_type, :options
|
||||
end
|
||||
|
||||
class ActionView::TestCase
|
||||
|
|
Loading…
Reference in New Issue