Permalink
Browse files

Merge pull request #23 from divoxx/fix_merge_nested_crits_like_elemmatch

Fix merge of nested criterias like
  • Loading branch information...
2 parents 3799f65 + c13ab1f commit 76dc51baea1cd07b2febf7d6407b6b7b2e3e44ba @jnunemaker jnunemaker committed Oct 10, 2012
Showing with 11 additions and 1 deletion.
  1. +5 −1 lib/plucky/criteria_hash.rb
  2. +6 −0 test/plucky/test_criteria_hash.rb
@@ -55,7 +55,11 @@ def merge(other)
if value_is_hash && other_is_hash
value.update(other_value) do |key, old_value, new_value|
- Array(old_value).concat(Array(new_value)).uniq
+ if old_value.is_a?(Hash) && new_value.is_a?(Hash)
+ self.class.new(old_value).merge(self.class.new(new_value)).to_hash
+ else
+ Array(old_value).concat(Array(new_value)).uniq
+ end
end
elsif value_is_hash && !other_is_hash
if modifier_key = value.keys.detect { |k| k.to_s[0, 1] == '$' }
@@ -309,6 +309,12 @@ class CriteriaHashTest < Test::Unit::TestCase
c1.merge(c2).should == CriteriaHash.new('$in' => [1, 2, 3])
end
+ should "be able to merge two modifier hashes with hash values" do
+ c1 = CriteriaHash.new(:arr => {'$elemMatch' => {:foo => 'bar'}})
+ c2 = CriteriaHash.new(:arr => {'$elemMatch' => {:omg => 'ponies'}})
+ c1.merge(c2).should == CriteriaHash.new(:arr => {'$elemMatch' => {:foo => 'bar', :omg => 'ponies'}})
+ end
+
should "merge matching keys with a single modifier" do
c1 = CriteriaHash.new(:foo => {'$in' => [1, 2, 3]})
c2 = CriteriaHash.new(:foo => {'$in' => [1, 4, 5]})

0 comments on commit 76dc51b

Please sign in to comment.