Permalink
Browse files

adding more MetaWhere comparison operators

  • Loading branch information...
1 parent a492691 commit eb2826f13596433554eaf71e7e56b98cc4a518c5 @ryanb committed Mar 9, 2011
@@ -11,9 +11,17 @@ def self.override_condition_matching?(subject, name, value)
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
@@ -19,6 +19,7 @@
with_model :article do
table do |t|
+ t.string "name"
t.boolean "published"
t.boolean "secret"
t.integer "priority"
@@ -227,11 +228,27 @@
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

0 comments on commit eb2826f

Please sign in to comment.