From 07088a0cdc019eb3a1297675a40f9b855bf98eb0 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 8 Mar 2011 10:52:49 -0800 Subject: [PATCH] making it easier to test all MetaWhere conditions --- .../model_adapters/active_record_adapter.rb | 4 ++- .../active_record_adapter_spec.rb | 25 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index f175237f..d02c25d3 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -10,8 +10,10 @@ def self.override_condition_matching?(subject, name, value) end def self.matches_condition?(subject, name, value) + subject_value = subject.send(name.column) case name.method - when "lt" then subject.send(name.column) < value + when "lt" then subject_value < value + when "gt" then subject_value > value end end diff --git a/spec/cancan/model_adapters/active_record_adapter_spec.rb b/spec/cancan/model_adapters/active_record_adapter_spec.rb index 6fa42e98..dcc9bd5e 100644 --- a/spec/cancan/model_adapters/active_record_adapter_spec.rb +++ b/spec/cancan/model_adapters/active_record_adapter_spec.rb @@ -201,15 +201,22 @@ @ability.model_adapter(Article, :read).joins.should == [:project] end - describe "with MetaWhere" do - it "should read articles where priority is less than 2" do - @ability.can :read, Article, :priority.lt => 2 - article1 = Article.create!(:priority => 1) - article2 = Article.create!(:priority => 3) - Article.accessible_by(@ability).should == [article1] - @ability.should be_able_to(:read, article1) - @ability.should_not be_able_to(:read, article2) - end + it "should restrict articles given a MetaWhere condition" do + @ability.can :read, Article, :priority.lt => 2 + article1 = Article.create!(:priority => 1) + article2 = Article.create!(:priority => 3) + Article.accessible_by(@ability).should == [article1] + @ability.should be_able_to(:read, article1) + @ability.should_not be_able_to(:read, article2) + end + + it "should match any MetaWhere condition" do + adapter = CanCan::ModelAdapters::ActiveRecordAdapter + article1 = Article.new(:priority => 1) + adapter.matches_condition?(article1, :priority.lt, 2).should be_true + adapter.matches_condition?(article1, :priority.lt, 1).should be_false + adapter.matches_condition?(article1, :priority.gt, 0).should be_true + adapter.matches_condition?(article1, :priority.gt, 1).should be_false end end end