Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Convert comparison to operation when setting a query operation
Browse files Browse the repository at this point in the history
This fixes the problem where building a query using intersection
starting with a query that matches everything ends up with an exception.

In example:

  users = User.all
  users &= User.all(:name => 'foo')
  users &= User.all(:email => /bar')
  • Loading branch information
solnic committed Oct 24, 2010
1 parent 2e19ca1 commit 189a649
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/dm-core/query.rb
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions spec/semipublic/query_spec.rb
Expand Up @@ -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 }
Expand Down Expand Up @@ -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) }
Expand All @@ -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

Expand Down

0 comments on commit 189a649

Please sign in to comment.