aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-04-13 12:22:19 +0100
committerJon Leighton <j@jonathanleighton.com>2012-04-13 13:17:43 +0100
commit55ee6ed7dd22a17499b68bee786da191ce68682c (patch)
treeaa4c5ff7fe16bd2665367356b085a022ae53b313 /activerecord
parentf52253cbebf0124bb74925e91bcca75325eaa502 (diff)
downloadrails-55ee6ed7dd22a17499b68bee786da191ce68682c.tar.gz
rails-55ee6ed7dd22a17499b68bee786da191ce68682c.tar.bz2
rails-55ee6ed7dd22a17499b68bee786da191ce68682c.zip
Add Relation#merge!
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb21
-rw-r--r--activerecord/test/cases/relation_test.rb5
2 files changed, 17 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 40af665769..a53ee01afa 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -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:
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index 31236cae15..c1063698bd 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -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