Skip to content
This repository
Browse code

adding any/all support for MetaWhere conditions

  • Loading branch information...
commit 9bee4a8d4b5bd9eb9569b9525f345e1ed511f51f 1 parent eb2826f
Ryan Bates authored
18 lib/cancan/model_adapters/active_record_adapter.rb
@@ -11,7 +11,17 @@ def self.override_condition_matching?(subject, name, value)
11 11
12 12 def self.matches_condition?(subject, name, value)
13 13 subject_value = subject.send(name.column)
14   - case name.method.to_sym
  14 + if name.method.to_s.ends_with? "_any"
  15 + value.any? { |v| meta_where_match? subject_value, name.method.to_s.sub("_any", ""), v }
  16 + elsif name.method.to_s.ends_with? "_all"
  17 + value.all? { |v| meta_where_match? subject_value, name.method.to_s.sub("_all", ""), v }
  18 + else
  19 + meta_where_match? subject_value, name.method, value
  20 + end
  21 + end
  22 +
  23 + def self.meta_where_match?(subject_value, method, value)
  24 + case method.to_sym
15 25 when :eq then subject_value == value
16 26 when :not_eq then subject_value != value
17 27 when :in then value.include?(subject_value)
@@ -20,9 +30,9 @@ def self.matches_condition?(subject, name, value)
20 30 when :lteq then subject_value <= value
21 31 when :gt then subject_value > value
22 32 when :gteq then subject_value >= value
23   - when :matches then subject_value.downcase.include?(value.downcase)
24   - when :does_not_match then !subject_value.downcase.include?(value.downcase)
25   - else raise NotImplemented, "The #{name.method} MetaWhere condition is not supported."
  33 + when :matches then subject_value =~ Regexp.new("^" + Regexp.escape(value).gsub("%", ".*") + "$", true)
  34 + when :does_not_match then !meta_where_match?(subject_value, :matches, value)
  35 + else raise NotImplemented, "The #{method} MetaWhere condition is not supported."
26 36 end
27 37 end
28 38
17 spec/cancan/model_adapters/active_record_adapter_spec.rb
@@ -231,6 +231,10 @@
231 231 article1 = Article.new(:priority => 1, :name => "Hello World")
232 232 adapter.matches_condition?(article1, :priority.eq, 1).should be_true
233 233 adapter.matches_condition?(article1, :priority.eq, 2).should be_false
  234 + adapter.matches_condition?(article1, :priority.eq_any, [1, 2]).should be_true
  235 + adapter.matches_condition?(article1, :priority.eq_any, [2, 3]).should be_false
  236 + adapter.matches_condition?(article1, :priority.eq_all, [1, 1]).should be_true
  237 + adapter.matches_condition?(article1, :priority.eq_all, [1, 2]).should be_false
234 238 adapter.matches_condition?(article1, :priority.ne, 2).should be_true
235 239 adapter.matches_condition?(article1, :priority.ne, 1).should be_false
236 240 adapter.matches_condition?(article1, :priority.in, [1, 2]).should be_true
@@ -245,10 +249,15 @@
245 249 adapter.matches_condition?(article1, :priority.gt, 1).should be_false
246 250 adapter.matches_condition?(article1, :priority.gteq, 1).should be_true
247 251 adapter.matches_condition?(article1, :priority.gteq, 2).should be_false
248   - adapter.matches_condition?(article1, :name.like, "ello worl").should be_true
249   - adapter.matches_condition?(article1, :name.like, "helo").should be_false
250   - adapter.matches_condition?(article1, :name.nlike, "helo").should be_true
251   - adapter.matches_condition?(article1, :name.nlike, "ello worl").should be_false
  252 + adapter.matches_condition?(article1, :name.like, "%ello worl%").should be_true
  253 + adapter.matches_condition?(article1, :name.like, "hello world").should be_true
  254 + adapter.matches_condition?(article1, :name.like, "hello%").should be_true
  255 + adapter.matches_condition?(article1, :name.like, "h%d").should be_true
  256 + adapter.matches_condition?(article1, :name.like, "%helo%").should be_false
  257 + adapter.matches_condition?(article1, :name.like, "hello").should be_false
  258 + adapter.matches_condition?(article1, :name.like, "hello.world").should be_false
  259 + adapter.matches_condition?(article1, :name.nlike, "%helo%").should be_true
  260 + adapter.matches_condition?(article1, :name.nlike, "%ello worl%").should be_false
252 261 end
253 262 end
254 263 end

0 comments on commit 9bee4a8

Please sign in to comment.
Something went wrong with that request. Please try again.