1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Only call where on associations when they respond to it.

Replaces #1351, as this includes tests.

🍐 @christian314159
This commit is contained in:
Andy Nicholson 2017-10-06 10:53:24 +11:00
parent 46da2d1e60
commit 2a2d04ddb7
3 changed files with 26 additions and 2 deletions

View file

@ -467,7 +467,7 @@ module SimpleForm
conditions = reflection.options[:conditions]
conditions = object.instance_exec(&conditions) if conditions.respond_to?(:call)
relation = relation.where(conditions)
relation = relation.where(conditions) if relation.respond_to?(:where)
relation = relation.order(order) if relation.respond_to?(:order)
end

View file

@ -154,6 +154,15 @@ class AssociationTest < ActionView::TestCase
end
end
test 'builder does not call where if the given association does not respond to it' do
with_association_for @user, :friends
assert_select 'form select.select#user_friend_ids'
assert_select 'form select[multiple=multiple]'
assert_select 'form select option[value="1"]', 'Friend 1'
assert_select 'form select option[value="2"]', 'Friend 2'
assert_select 'form select option[value="3"]', 'Friend 3'
end
test 'builder does not call order if the given association does not respond to it' do
with_association_for @user, :pictures
assert_select 'form select.select#user_picture_ids'

View file

@ -56,6 +56,19 @@ Company = Struct.new(:id, :name) do
end
end
Friend = Struct.new(:id, :name) do
extend ActiveModel::Naming
include ActiveModel::Conversion
def self.all
(1..3).map { |i| new(i, "#{name} #{i}") }
end
def persisted?
true
end
end
class Tag < Company; end
TagGroup = Struct.new(:id, :name, :tags)
@ -70,7 +83,7 @@ class User
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
:extra_special_company_id, :pictures, :picture_ids, :special_pictures,
:special_picture_ids, :uuid
:special_picture_ids, :uuid, :friends, :friend_ids
def self.build(extra_attributes = {})
attributes = {
@ -201,6 +214,8 @@ class User
Association.new(Picture, association, :has_many, nil, {})
when :special_pictures
Association.new(Picture, association, :has_many, proc { where(name: self.name) }, {})
when :friends
Association.new(Friend, association, :has_many, nil, {})
end
end