From 55ee6ed7dd22a17499b68bee786da191ce68682c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 13 Apr 2012 12:22:19 +0100 Subject: Add Relation#merge! --- .../lib/active_record/relation/spawn_methods.rb | 21 ++++++++++++--------- activerecord/test/cases/relation_test.rb | 5 +++++ 2 files changed, 17 insertions(+), 9 deletions(-) (limited to 'activerecord') 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 -- cgit v1.2.3