mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Refactor association method and move the reflection variable to inside of input base, where it really belongs to
This commit is contained in:
parent
9eef9d1cb1
commit
dc46c17c22
6 changed files with 20 additions and 22 deletions
|
@ -1,6 +1,6 @@
|
|||
module SimpleForm
|
||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
attr_reader :template, :object_name, :object, :reflection, :options
|
||||
attr_reader :template, :object_name, :object
|
||||
|
||||
extend MapType
|
||||
include SimpleForm::Inputs
|
||||
|
@ -123,28 +123,28 @@ module SimpleForm
|
|||
|
||||
raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object
|
||||
|
||||
options[:as] ||= :select
|
||||
@reflection = find_association_reflection(association)
|
||||
raise "Association #{association.inspect} not found" unless @reflection
|
||||
reflection = find_association_reflection(association)
|
||||
raise "Association #{association.inspect} not found" unless reflection
|
||||
|
||||
case @reflection.macro
|
||||
options[:as] ||= :select
|
||||
options[:collection] ||= reflection.klass.all(reflection.options.slice(:conditions, :order))
|
||||
|
||||
attribute = case reflection.macro
|
||||
when :belongs_to
|
||||
attribute = @reflection.options[:foreign_key] || :"#{@reflection.name}_id"
|
||||
reflection.options[:foreign_key] || :"#{reflection.name}_id"
|
||||
when :has_one
|
||||
raise ":has_one association are not supported by f.association"
|
||||
else
|
||||
attribute = :"#{@reflection.name.to_s.singularize}_ids"
|
||||
|
||||
if options[:as] == :select
|
||||
html_options = options[:input_html] ||= {}
|
||||
html_options[:size] ||= 5
|
||||
html_options[:multiple] = true unless html_options.key?(:multiple)
|
||||
end
|
||||
|
||||
:"#{reflection.name.to_s.singularize}_ids"
|
||||
end
|
||||
|
||||
options[:collection] ||= @reflection.klass.all(@reflection.options.slice(:conditions, :order))
|
||||
|
||||
input(attribute, options).tap { @reflection = nil }
|
||||
input(attribute, options.merge(:reflection => reflection))
|
||||
end
|
||||
|
||||
# Creates a button:
|
||||
|
|
|
@ -15,15 +15,17 @@ module SimpleForm
|
|||
include SimpleForm::Components::Placeholders
|
||||
include SimpleForm::Components::Wrapper
|
||||
|
||||
attr_reader :attribute_name, :column, :input_type, :options, :input_html_options
|
||||
attr_reader :attribute_name, :column, :input_type, :reflection,
|
||||
:options, :input_html_options
|
||||
|
||||
delegate :template, :object, :object_name, :reflection, :to => :@builder
|
||||
delegate :template, :object, :object_name, :to => :@builder
|
||||
|
||||
def initialize(builder, attribute_name, column, input_type, options = {})
|
||||
@builder = builder
|
||||
@attribute_name = attribute_name
|
||||
@column = column
|
||||
@input_type = input_type
|
||||
@reflection = options.delete(:reflection)
|
||||
@options = options
|
||||
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
|
||||
o[:required] = true if attribute_required?
|
||||
|
|
|
@ -4,7 +4,7 @@ class ErrorTest < ActionView::TestCase
|
|||
|
||||
def with_error_for(object, attribute_name, type, options={}, &block)
|
||||
with_concat_form_for(object) do |f|
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).error.to_s
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ class HintTest < ActionView::TestCase
|
|||
|
||||
def with_hint_for(object, attribute_name, type, options={}, &block)
|
||||
with_concat_form_for(object) do |f|
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).hint.to_s
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ class LabelTest < ActionView::TestCase
|
|||
|
||||
def with_label_for(object, attribute_name, type, options={})
|
||||
with_concat_form_for(object) do |f|
|
||||
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,10 +27,6 @@ else
|
|||
raise "Could not find country_select plugin in test/support. Please execute git submodule update --init."
|
||||
end
|
||||
|
||||
class SimpleForm::FormBuilder
|
||||
attr_accessor :reflection, :options
|
||||
end
|
||||
|
||||
class ActionView::TestCase
|
||||
include MiscHelpers
|
||||
include SimpleForm::ActionViewExtensions::FormHelper
|
||||
|
|
Loading…
Reference in a new issue