1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add Relation#merge!

This commit is contained in:
Jon Leighton 2012-04-13 12:22:19 +01:00
parent f52253cbeb
commit 55ee6ed7dd
2 changed files with 17 additions and 9 deletions

View file

@ -4,20 +4,23 @@ require 'active_record/relation/merger'
module ActiveRecord
module SpawnMethods
def merge(other)
if other
case other
when Array
to_a & other
when Hash
Relation::HashMerger.new(clone, other).merge
else
Relation::Merger.new(clone, other).merge
end
if other.is_a?(Array)
to_a & other
elsif other
clone.merge!(other)
else
self
end
end
def merge!(other)
if other.is_a?(Hash)
Relation::HashMerger.new(self, other).merge
else
Relation::Merger.new(self, other).merge
end
end
# Removes from the query the condition(s) specified in +skips+.
#
# Example:

View file

@ -206,5 +206,10 @@ module ActiveRecord
assert relation.create_with!(foo: 'bar').equal?(relation)
assert_equal({foo: 'bar'}, relation.create_with_value)
end
test 'merge!' do
assert relation.merge!(where: ['foo']).equal?(relation)
assert_equal ['foo'], relation.where_values
end
end
end