adding more MetaWhere comparison operators

This commit is contained in:
Ryan Bates 2011-03-08 22:21:42 -08:00
parent a49269175e
commit eb2826f135
2 changed files with 29 additions and 4 deletions

View File

@ -11,9 +11,17 @@ module CanCan
def self.matches_condition?(subject, name, value)
subject_value = subject.send(name.column)
case name.method
when "lt" then subject_value < value
when "gt" then subject_value > value
case name.method.to_sym
when :eq then subject_value == value
when :not_eq then subject_value != value
when :in then value.include?(subject_value)
when :not_in then !value.include?(subject_value)
when :lt then subject_value < value
when :lteq then subject_value <= value
when :gt then subject_value > value
when :gteq then subject_value >= value
when :matches then subject_value.downcase.include?(value.downcase)
when :does_not_match then !subject_value.downcase.include?(value.downcase)
else raise NotImplemented, "The #{name.method} MetaWhere condition is not supported."
end
end

View File

@ -19,6 +19,7 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
with_model :article do
table do |t|
t.string "name"
t.boolean "published"
t.boolean "secret"
t.integer "priority"
@ -227,11 +228,27 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
it "should match any MetaWhere condition" do
adapter = CanCan::ModelAdapters::ActiveRecordAdapter
article1 = Article.new(:priority => 1)
article1 = Article.new(:priority => 1, :name => "Hello World")
adapter.matches_condition?(article1, :priority.eq, 1).should be_true
adapter.matches_condition?(article1, :priority.eq, 2).should be_false
adapter.matches_condition?(article1, :priority.ne, 2).should be_true
adapter.matches_condition?(article1, :priority.ne, 1).should be_false
adapter.matches_condition?(article1, :priority.in, [1, 2]).should be_true
adapter.matches_condition?(article1, :priority.in, [2, 3]).should be_false
adapter.matches_condition?(article1, :priority.nin, [2, 3]).should be_true
adapter.matches_condition?(article1, :priority.nin, [1, 2]).should be_false
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.lteq, 1).should be_true
adapter.matches_condition?(article1, :priority.lteq, 0).should be_false
adapter.matches_condition?(article1, :priority.gt, 0).should be_true
adapter.matches_condition?(article1, :priority.gt, 1).should be_false
adapter.matches_condition?(article1, :priority.gteq, 1).should be_true
adapter.matches_condition?(article1, :priority.gteq, 2).should be_false
adapter.matches_condition?(article1, :name.like, "ello worl").should be_true
adapter.matches_condition?(article1, :name.like, "helo").should be_false
adapter.matches_condition?(article1, :name.nlike, "helo").should be_true
adapter.matches_condition?(article1, :name.nlike, "ello worl").should be_false
end
end
end