making it easier to test all MetaWhere conditions

This commit is contained in:
Ryan Bates 2011-03-08 10:52:49 -08:00
parent ff5aaf543b
commit 07088a0cdc
2 changed files with 19 additions and 10 deletions

View File

@ -10,8 +10,10 @@ module CanCan
end end
def self.matches_condition?(subject, name, value) def self.matches_condition?(subject, name, value)
subject_value = subject.send(name.column)
case name.method 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
end end

View File

@ -201,8 +201,7 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.model_adapter(Article, :read).joins.should == [:project] @ability.model_adapter(Article, :read).joins.should == [:project]
end end
describe "with MetaWhere" do it "should restrict articles given a MetaWhere condition" do
it "should read articles where priority is less than 2" do
@ability.can :read, Article, :priority.lt => 2 @ability.can :read, Article, :priority.lt => 2
article1 = Article.create!(:priority => 1) article1 = Article.create!(:priority => 1)
article2 = Article.create!(:priority => 3) article2 = Article.create!(:priority => 3)
@ -210,6 +209,14 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.should be_able_to(:read, article1) @ability.should be_able_to(:read, article1)
@ability.should_not be_able_to(:read, article2) @ability.should_not be_able_to(:read, article2)
end 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 end
end end