Skip to content

Commit

Permalink
Add eq_any.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernie Miller committed Sep 29, 2010
1 parent d681655 commit 261d284
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/arel/attributes/attribute.rb
Expand Up @@ -11,6 +11,14 @@ def eq other
Nodes::Equality.new self, other Nodes::Equality.new self, other
end end


def eq_any others
first = Nodes::Equality.new self, others.shift

Nodes::Grouping.new others.inject(first) { |memo,expr|
Nodes::Or.new(memo, Nodes::Equality.new(self, expr))
}
end

def in other def in other
case other case other
when Arel::SelectManager when Arel::SelectManager
Expand Down
16 changes: 16 additions & 0 deletions spec/attributes/attribute_spec.rb
Expand Up @@ -142,6 +142,22 @@ module Attributes
end end
end end


describe '#eq_any' do
it 'should create a Grouping node' do
relation = Table.new(:users)
relation[:id].eq_any([1,2]).should be_kind_of Nodes::Grouping
end

it 'should generate multiple ORs in sql' do
relation = Table.new(:users)
mgr = relation.project relation[:id]
mgr.where relation[:id].eq_any([1,2])
mgr.to_sql.should be_like %{
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 OR "users"."id" = 2)
}
end
end

describe '#in' do describe '#in' do
it 'can be constructed with a list' do it 'can be constructed with a list' do
end end
Expand Down

0 comments on commit 261d284

Please sign in to comment.