allow query.conditions to be called multiple times without losing conditions

This commit is contained in:
Ryan Bates 2010-12-21 14:01:28 -08:00
parent 9b8e84944c
commit 5e1e6e182b
2 changed files with 11 additions and 2 deletions

View File

@ -26,10 +26,10 @@ module CanCan
def conditions def conditions
if @rules.size == 1 && @rules.first.base_behavior if @rules.size == 1 && @rules.first.base_behavior
# Return the conditions directly if there's just one definition # Return the conditions directly if there's just one definition
@rules.first.tableized_conditions @rules.first.tableized_conditions.dup
else else
@rules.reverse.inject(false_sql) do |sql, rule| @rules.reverse.inject(false_sql) do |sql, rule|
merge_conditions(sql, rule.tableized_conditions, rule.base_behavior) merge_conditions(sql, rule.tableized_conditions.dup, rule.base_behavior)
end end
end end
end end

View File

@ -104,4 +104,13 @@ describe CanCan::Query do
@ability.can :read, Project, :project => { :foo => {:bar => true}, :bar => {:zip => :zap} } @ability.can :read, Project, :project => { :foo => {:bar => true}, :bar => {:zip => :zap} }
@ability.query(:read, Project).joins.inspect.should orderlessly_match([{:project => [:bar, :foo]}, {:company => [:bar]}].inspect) @ability.query(:read, Project).joins.inspect.should orderlessly_match([{:project => [:bar, :foo]}, {:company => [:bar]}].inspect)
end end
it "should not forget conditions when calling with SQL string" do
@ability.can :read, Project, :foo => 1
@ability.can :read, Project, ['bar = ?', 1]
query = @ability.query(:read, Project)
2.times do
query.conditions.should == "(bar = 1) OR (foo=1)"
end
end
end end