Ensure we always use foreign key name on association.
This commit is contained in:
parent
b8e8687c05
commit
3a83ae81c6
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue