From 2a2d04ddb752134e6ea93051d55df11ddf43b28e Mon Sep 17 00:00:00 2001 From: Andy Nicholson Date: Fri, 6 Oct 2017 10:53:24 +1100 Subject: [PATCH] Only call `where` on associations when they respond to it. Replaces #1351, as this includes tests. :pear: @christian314159 --- lib/simple_form/form_builder.rb | 2 +- test/form_builder/association_test.rb | 9 +++++++++ test/support/models.rb | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index c151e9ed..dfb1ce5b 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -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 diff --git a/test/form_builder/association_test.rb b/test/form_builder/association_test.rb index d8a9b6f8..f23e88e9 100644 --- a/test/form_builder/association_test.rb +++ b/test/form_builder/association_test.rb @@ -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' diff --git a/test/support/models.rb b/test/support/models.rb index 50df3bda..2a09025a 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -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