Permalink
Browse files

adding initial MetaWhere support

  • Loading branch information...
1 parent 52435e9 commit ff5aaf543b7e491fbe930d04709f0ebd7b5a8476 @ryanb committed Mar 8, 2011
@@ -26,6 +26,17 @@ def self.matches_conditions_hash?(subject, conditions)
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
@@ -5,6 +5,16 @@ def self.for_class?(model_class)
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
@@ -100,17 +100,21 @@ def matches_conditions_hash?(subject, conditions = @conditions)
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 }
+ if model_adapter(subject).override_condition_matching? subject, name, value
+ model_adapter(subject).matches_condition? subject, name, value
+ else
+ 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
- matches_conditions_hash? attribute, value
+ attribute == value
end
- elsif value.kind_of?(Array) || value.kind_of?(Range)
- value.include? attribute
- else
- attribute == value
end
end
end
@@ -21,6 +21,7 @@
table do |t|
t.boolean "published"
t.boolean "secret"
+ t.integer "priority"
t.integer "category_id"
end
model do
@@ -199,5 +200,16 @@
@ability.can :read, Article, :project => { :admin => true }
@ability.model_adapter(Article, :read).joins.should == [:project]
end
+
+ describe "with MetaWhere" do
+ it "should read articles where priority is less than 2" do
+ @ability.can :read, Article, :priority.lt => 2
+ article1 = Article.create!(:priority => 1)
+ article2 = Article.create!(:priority => 3)
+ Article.accessible_by(@ability).should == [article1]
+ @ability.should be_able_to(:read, article1)
+ @ability.should_not be_able_to(:read, article2)
+ end
+ end
end
end

0 comments on commit ff5aaf5

Please sign in to comment.