Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Commit

Permalink
Convert ActiveRecord::Relations into their ARel AST in Function args. F…
Browse files Browse the repository at this point in the history
…ixes #17
  • Loading branch information
Ernie Miller committed Jun 15, 2011
1 parent 3ce3f73 commit 1ce3601
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/squeel/visitors/predicate_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def visit_Squeel_Nodes_Function(o, parent)
case arg
when Nodes::Function
accept(arg, parent)
when ActiveRecord::Relation
arg.arel.ast
when Nodes::KeyPath
can_accept?(arg.endpoint) ? accept(arg, parent) : contextualize(traverse(arg, parent))[arg.endpoint.to_sym]
when Symbol, Nodes::Stub
Expand Down
7 changes: 7 additions & 0 deletions spec/squeel/visitors/predicate_visitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ module Visitors
predicate.to_sql.should be_like '"people"."id" IN (SELECT "people"."id" FROM "people" ORDER BY "people"."id" DESC LIMIT 3)'
end

it 'converts ActiveRecord::Relation values in function arguments to their ARel AST' do
predicate = @v.accept(dsl{exists(Person.where{name == 'Aric Smith'})})
predicate.should be_a Arel::Nodes::NamedFunction
predicate.expressions.first.should be_a Arel::Nodes::SelectStatement
predicate.to_sql.should be_like "exists(SELECT \"people\".* FROM \"people\" WHERE \"people\".\"name\" = 'Aric Smith')"
end

it "doesn't try to sanitize_sql an array of strings in the value of a Predicate" do
predicate = @v.accept(dsl{name >> ['Aric Smith', 'Gladyce Kulas']})
predicate.should be_a Arel::Nodes::In
Expand Down

0 comments on commit 1ce3601

Please sign in to comment.