From 5e1e6e182b3332221309e1b36796c342eb477fe4 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 21 Dec 2010 14:01:28 -0800 Subject: [PATCH] allow query.conditions to be called multiple times without losing conditions --- lib/cancan/query.rb | 4 ++-- spec/cancan/query_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/cancan/query.rb b/lib/cancan/query.rb index 67aadd4..595abff 100644 --- a/lib/cancan/query.rb +++ b/lib/cancan/query.rb @@ -26,10 +26,10 @@ module CanCan def conditions if @rules.size == 1 && @rules.first.base_behavior # Return the conditions directly if there's just one definition - @rules.first.tableized_conditions + @rules.first.tableized_conditions.dup else @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 diff --git a/spec/cancan/query_spec.rb b/spec/cancan/query_spec.rb index 989059d..1b4ac39 100644 --- a/spec/cancan/query_spec.rb +++ b/spec/cancan/query_spec.rb @@ -104,4 +104,13 @@ describe CanCan::Query do @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) 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