From d6851debd44bcd7851a346a08085eeb228095e9e Mon Sep 17 00:00:00 2001 From: Emmanuel Gomez Date: Fri, 29 Apr 2011 00:46:38 -0700 Subject: [PATCH] Fix pending spec for DataMapper adapter. --- .../model_adapters/data_mapper_adapter.rb | 19 ++++++++----------- .../data_mapper_adapter_spec.rb | 1 - 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/cancan/model_adapters/data_mapper_adapter.rb b/lib/cancan/model_adapters/data_mapper_adapter.rb index 38ba023..9060b97 100644 --- a/lib/cancan/model_adapters/data_mapper_adapter.rb +++ b/lib/cancan/model_adapters/data_mapper_adapter.rb @@ -15,19 +15,16 @@ module CanCan end def database_records - scope = @model_class.all(:conditions => ["0=1"]) - conditions.each do |condition| - scope += @model_class.all(:conditions => condition) - end + scope = @model_class.all(:conditions => ["0 = 1"]) + cans, cannots = @rules.partition { |r| r.base_behavior } + # 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 end - - def conditions - @rules.map(&:conditions) - end - end - end -end + end # class DataMapper + end # module ModelAdapters +end # module CanCan DataMapper::Model.class_eval do include CanCan::ModelAdditions::ClassMethods diff --git a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb index 924b65f..e2eb1f8 100644 --- a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb +++ b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb @@ -65,7 +65,6 @@ if ENV["MODEL_ADAPTER"] == "data_mapper" end 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.cannot :read, Article, :secret => true article1 = Article.create(:published => true, :secret => false)