diff --git a/lib/dm-core/query.rb b/lib/dm-core/query.rb index 0c8039fa..418675d5 100644 --- a/lib/dm-core/query.rb +++ b/lib/dm-core/query.rb @@ -1356,7 +1356,18 @@ def set_operation(operation, other) # # @api private def other_conditions(other, operation) - query_conditions(self).send(operation, query_conditions(other)) + self_conditions = query_conditions(self) + + unless self_conditions.kind_of?(Conditions::Operation) + operation_slug = case operation + when :intersection, :difference then :and + when :union then :or + end + + self_conditions = Conditions::Operation.new(operation_slug, self_conditions) + end + + self_conditions.send(operation, query_conditions(other)) end # Extract conditions from a Query diff --git a/spec/semipublic/query_spec.rb b/spec/semipublic/query_spec.rb index 2308ad14..747d2685 100644 --- a/spec/semipublic/query_spec.rb +++ b/spec/semipublic/query_spec.rb @@ -1960,7 +1960,15 @@ class ::Other end end - subject { @query.send(method, @other) } + subject do + result = @query.send(method, @other) + + if @another + result = result.send(method, @another) + end + + result + end describe 'with equivalent query' do before { @other = @query.dup } @@ -1995,8 +2003,9 @@ class ::Other describe 'with self matching everything' do before do - @query = DataMapper::Query.new(@repository, @model) - @other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb') + @query = DataMapper::Query.new(@repository, @model) + @other = DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb') + @another = DataMapper::Query.new(@repository, @model, :citizenship => 'US') end it { should be_kind_of(DataMapper::Query) } @@ -2005,10 +2014,10 @@ class ::Other it { should_not equal(@other) } + it { should_not equal(@another) } + it 'should factor out the operation matching everything' do - pending 'TODO: compress Query#conditions for proper comparison' do - should == DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb') - end + should == DataMapper::Query.new(@repository, @model, :name => 'Dan Kubb', :citizenship => 'US') end end