module Plucky
  class CriteriaMerger
    def self.merge(hash, other)
      hash.dup.tap do |target|
        other.keys.each do |key|
          if target[key].is_a?(Hash)
            if other[key].is_a?(Hash)
              target[key] = merge(target[key], other[key])
            else
              target[key]['$in'].concat(Array(other[key])).uniq!
            end
            next
          end

          target.update(other) { |key, *values| values.flatten.uniq }
        end
      end
    end
  end
end
module Plucky
  class CriteriaMerger
    def self.merge(hash, other)
      hash.dup.tap do |target|
        other.keys.each do |key|
          if target[key].is_a?(Hash)
            if other[key].is_a?(Hash)
              target[key] = merge(target[key], other[key])
            else
              target[key]['$in'].concat(Array(other[key])).uniq!
            end
          else
            if target.key?(key) && other.key?(key)
              target[key] = Array(target[key]).concat(Array(other[key]))
            else
              target[key] = other[key]
            end
          end
        end
      end
    end
  end
end
Fix for single values that were being returned as arrays incorrectly.
Fix for single values that were being returned as arrays incorrectly.