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:
parent
46da2d1e60
commit
2a2d04ddb7
3 changed files with 26 additions and 2 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue