adding initial MetaWhere support

This commit is contained in:
Ryan Bates
2011-03-08 10:37:25 -08:00
parent 52435e97d9
commit ff5aaf543b
4 changed files with 47 additions and 10 deletions

View File

@@ -26,6 +26,17 @@ module CanCan
raise NotImplemented, "This model adapter does not support matching on a conditions hash."
end
# Used to determine if this model adapter will override the matching behavior for a specific condition.
# If this returns true then matches_condition? will be called. See Rule#matches_conditions_hash
def self.override_condition_matching?(subject, name, value)
false
end
# Override if override_condition_matching? returns true
def self.matches_condition?(subject, name, value)
raise NotImplemented, "This model adapter does not support matching on a specific condition."
end
def initialize(model_class, rules)
@model_class = model_class
@rules = rules

View File

@@ -5,6 +5,16 @@ module CanCan
model_class <= ActiveRecord::Base
end
def self.override_condition_matching?(subject, name, value)
name.kind_of?(MetaWhere::Column) if defined? MetaWhere
end
def self.matches_condition?(subject, name, value)
case name.method
when "lt" then subject.send(name.column) < value
end
end
# Returns conditions intended to be used inside a database query. Normally you will not call this
# method directly, but instead go through ModelAdditions#accessible_by.
#

View File

@@ -100,17 +100,21 @@ module CanCan
model_adapter(subject).matches_conditions_hash? subject, conditions
else
conditions.all? do |name, value|
attribute = subject.send(name)
if value.kind_of?(Hash)
if attribute.kind_of? Array
attribute.any? { |element| matches_conditions_hash? element, value }
else
matches_conditions_hash? attribute, value
end
elsif value.kind_of?(Array) || value.kind_of?(Range)
value.include? attribute
if model_adapter(subject).override_condition_matching? subject, name, value
model_adapter(subject).matches_condition? subject, name, value
else
attribute == value
attribute = subject.send(name)
if value.kind_of?(Hash)
if attribute.kind_of? Array
attribute.any? { |element| matches_conditions_hash? element, value }
else
matches_conditions_hash? attribute, value
end
elsif value.kind_of?(Array) || value.kind_of?(Range)
value.include? attribute
else
attribute == value
end
end
end
end