Merge pull request #355 from emmanuel/issue/245.

DataMapper adapter improvements
This commit is contained in:
Ryan Bates 2011-05-02 13:52:11 -07:00
commit ff13a82dda
2 changed files with 11 additions and 13 deletions

View File

@ -10,23 +10,22 @@ module CanCan
end end
def self.matches_conditions_hash?(subject, conditions) def self.matches_conditions_hash?(subject, conditions)
subject.class.all(:conditions => conditions).include?(subject) # TODO don't use a database query here for performance and other instances collection = DataMapper::Collection.new(subject.query, [ subject ])
!!collection.first(conditions)
end end
def database_records def database_records
scope = @model_class.all(:conditions => ["0 = 1"]) scope = @model_class.all(:conditions => ["0 = 1"])
conditions.each do |condition| cans, cannots = @rules.partition { |r| r.base_behavior }
scope += @model_class.all(:conditions => condition) return scope if cans.empty?
end # apply unions first, then differences. this mean cannot overrides can
cans.each { |r| scope += @model_class.all(:conditions => r.conditions) }
cannots.each { |r| scope -= @model_class.all(:conditions => r.conditions) }
scope scope
end end
end # class DataMapper
def conditions end # module ModelAdapters
@rules.map(&:conditions) end # module CanCan
end
end
end
end
DataMapper::Model.class_eval do DataMapper::Model.class_eval do
include CanCan::ModelAdditions::ClassMethods include CanCan::ModelAdditions::ClassMethods

View File

@ -65,7 +65,6 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
end end
it "should fetch only the articles that are published and not secret" do it "should fetch only the articles that are published and not secret" do
pending "the `cannot` may require some custom SQL, maybe abstract out from Active Record adapter"
@ability.can :read, Article, :published => true @ability.can :read, Article, :published => true
@ability.cannot :read, Article, :secret => true @ability.cannot :read, Article, :secret => true
article1 = Article.create(:published => true, :secret => false) article1 = Article.create(:published => true, :secret => false)