Skip to content

Commit

Permalink
Refactor association method and move the reflection variable to insid…
Browse files Browse the repository at this point in the history
…e of input base, where it really belongs to
  • Loading branch information
carlosantoniodasilva committed Dec 8, 2010
1 parent 9eef9d1 commit dc46c17
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
24 changes: 12 additions & 12 deletions lib/simple_form/form_builder.rb
@@ -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
Expand Down Expand Up @@ -79,8 +79,8 @@ class FormBuilder < ActionView::Helpers::FormBuilder
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
#
def input(attribute_name, options={}, &block)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)

if block_given?
SimpleForm::Inputs::BlockInput.new(self, attribute_name, column, input_type, options, &block).render
Expand Down Expand Up @@ -123,28 +123,28 @@ def association(association, options={}, &block)

raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object

reflection = find_association_reflection(association)
raise "Association #{association.inspect} not found" unless reflection

This comment has been minimized.

Copy link
@nashby

nashby Sep 13, 2012

Collaborator

@carlosantoniodasilva looks like it's more ArgumentError then RuntimeError, wdyt?

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Sep 13, 2012

Collaborator

👍

This comment has been minimized.

Copy link
@carlosantoniodasilva

carlosantoniodasilva Sep 13, 2012

Author Member

👍


options[:as] ||= :select
@reflection = find_association_reflection(association)
raise "Association #{association.inspect} not found" unless @reflection
options[:collection] ||= reflection.klass.all(reflection.options.slice(:conditions, :order))

case @reflection.macro
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
end

options[:collection] ||= @reflection.klass.all(@reflection.options.slice(:conditions, :order))
:"#{reflection.name.to_s.singularize}_ids"
end

input(attribute, options).tap { @reflection = nil }
input(attribute, options.merge(:reflection => reflection))
end

# Creates a button:
Expand Down
6 changes: 4 additions & 2 deletions lib/simple_form/inputs/base.rb
Expand Up @@ -15,15 +15,17 @@ class Base
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?
Expand Down
2 changes: 1 addition & 1 deletion test/components/error_test.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/components/hint_test.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/components/label_test.rb
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/test_helper.rb
Expand Up @@ -27,10 +27,6 @@
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
Expand Down

0 comments on commit dc46c17

Please sign in to comment.