From 3a83ae81c66f2951f11ef0ed520e4f185a4c1f4a Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 11 Dec 2009 00:34:26 -0200 Subject: [PATCH] Ensure we always use foreign key name on association. --- lib/simple_form/form_builder.rb | 8 +++++--- test/form_builder_test.rb | 8 ++++---- test/support/models.rb | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 1f8ca256..95422b97 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -95,10 +95,12 @@ module SimpleForm def association(attribute, options={}) raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object - options[:collection] ||= begin - association = find_association(attribute) - raise "Association not found #{attribute.inspect}" unless association + association = find_association(attribute) + raise "Association not found #{attribute.inspect}" unless association + attribute = association.options[:foreign_key] || :"#{association.name}_id" + + options[:collection] ||= begin find_options = { :conditions => options.delete(:conditions), :order => options.delete(:order) } association.klass.all(find_options) diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 9984700b..e721a4f5 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -346,7 +346,7 @@ class FormBuilderTest < ActionView::TestCase test 'builder should allow creating an association input generating collection' do with_association_for @user, :company - assert_select 'form select.select#user_company' + assert_select 'form select.select#user_company_id' assert_select 'form select option[value=1]', 'Company 1' assert_select 'form select option[value=2]', 'Company 2' assert_select 'form select option[value=3]', 'Company 3' @@ -354,7 +354,7 @@ class FormBuilderTest < ActionView::TestCase test 'builder should allow passing conditions to find collection' do with_association_for @user, :company, :conditions => { :id => 1 } - assert_select 'form select.select#user_company' + assert_select 'form select.select#user_company_id' assert_select 'form select option[value=1]' assert_no_select 'form select option[value=2]' assert_no_select 'form select option[value=3]' @@ -362,7 +362,7 @@ class FormBuilderTest < ActionView::TestCase test 'builder should allow passing order to find collection' do with_association_for @user, :company, :order => 'name' - assert_select 'form select.select#user_company' + assert_select 'form select.select#user_company_id' assert_no_select 'form select option[value=1]' assert_no_select 'form select option[value=2]' assert_select 'form select option[value=3]' @@ -372,7 +372,7 @@ class FormBuilderTest < ActionView::TestCase with_association_for @user, :company, :collection => [Company.new(999, 'Teste')], :options => { :include_blank => false } - assert_select 'form select.select#user_company' + assert_select 'form select.select#user_company_id' assert_no_select 'form select option[value=1]' assert_select 'form select option[value=999]', 'Teste' assert_select 'form select option', :count => 1 diff --git a/test/support/models.rb b/test/support/models.rb index 1e35f0aa..070112f0 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -1,7 +1,7 @@ require 'ostruct' Column = Struct.new(:name, :type, :limit) -Association = Struct.new(:klass) +Association = Struct.new(:klass, :name, :options) class Company < Struct.new(:id, :name) def self.all(options={}) @@ -56,7 +56,7 @@ class User < OpenStruct end def self.reflect_on_association(association) - Association.new(Company) if association == :company + Association.new(Company, association, {}) if association == :company end def errors