Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
making it easier to test all MetaWhere conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Mar 8, 2011
1 parent ff5aaf5 commit 07088a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/cancan/model_adapters/active_record_adapter.rb
Expand Up @@ -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

Expand Down
25 changes: 16 additions & 9 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Expand Up @@ -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

0 comments on commit 07088a0

Please sign in to comment.