Make `AR::SpawnMethods#merge!` to check an arg is a Proc

From Ruby ( 2.3.0dev trunk 52520), `Hash#to_proc` is defined
(fbe967ec02),
and many tests have been failed with
`ArgumentError: wrong number of arguments (given 0, expected 1)`.
Because we call `Hash#to_proc` with no args in `#merge!`.

This commit changes order of conditionals to not call `Hash#to_proc`.

(cherry picked from commit a98475c2df)
This commit is contained in:
yui-knk 2015-11-11 15:15:26 +09:00 committed by Andrew White
parent 4696e0d439
commit 32ca78a670
2 changed files with 14 additions and 3 deletions

View File

@ -12,6 +12,7 @@ module ActiveRecord
# Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an <tt>ActiveRecord::Relation</tt>.
# Returns an array representing the intersection of the resulting records with <tt>other</tt>, if <tt>other</tt> is an array.
#
# Post.where(published: true).joins(:comments).merge( Comment.where(spam: false) )
# # Performs a single join query with both where conditions.
#
@ -37,11 +38,14 @@ module ActiveRecord
end
def merge!(other) # :nodoc:
if !other.is_a?(Relation) && other.respond_to?(:to_proc)
if other.is_a?(Hash)
Relation::HashMerger.new(self, other).merge
elsif other.is_a?(Relation)
Relation::Merger.new(self, other).merge
elsif other.respond_to?(:to_proc)
instance_exec(&other)
else
klass = other.is_a?(Hash) ? Relation::HashMerger : Relation::Merger
klass.new(self, other).merge
raise ArgumentError, "#{other.inspect} is not an ActiveRecord::Relation"
end
end

View File

@ -235,5 +235,12 @@ module ActiveRecord
posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)
assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
end
def test_merge_raises_with_invalid_argument
assert_raises ArgumentError do
relation = Relation.new(FakeKlass, :b)
relation.merge(true)
end
end
end
end