From 613ab1c1abb1338d873eb3ffccb9df355685a2c1 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Sat, 21 May 2011 13:57:17 -0700 Subject: [PATCH] delegating ControllerResource find to model adapter, uses 'get' for DataMapper - closes #373 --- lib/cancan/controller_resource.rb | 6 +++++- lib/cancan/model_adapters/abstract_adapter.rb | 5 +++++ lib/cancan/model_adapters/data_mapper_adapter.rb | 4 ++++ spec/cancan/model_adapters/active_record_adapter_spec.rb | 5 +++++ spec/cancan/model_adapters/data_mapper_adapter_spec.rb | 5 +++++ spec/cancan/model_adapters/mongoid_adapter_spec.rb | 5 +++++ 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 10cf4b6..6c41efd 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -107,11 +107,15 @@ module CanCan resource_base.send(@options[:find_by], id_param) end else - resource_base.find(id_param) + adapter.find(resource_base, id_param) end end end + def adapter + ModelAdapters::AbstractAdapter.adapter_class(resource_class) + end + def authorization_action parent? ? :show : @params[:action].to_sym end diff --git a/lib/cancan/model_adapters/abstract_adapter.rb b/lib/cancan/model_adapters/abstract_adapter.rb index 2b7b873..e644713 100644 --- a/lib/cancan/model_adapters/abstract_adapter.rb +++ b/lib/cancan/model_adapters/abstract_adapter.rb @@ -15,6 +15,11 @@ module CanCan false # override in subclass end + # Override if you need custom find behavior + def self.find(model_class, id) + model_class.find(id) + end + # Used to determine if this model adapter will override the matching behavior for a hash of conditions. # If this returns true then matches_conditions_hash? will be called. See Rule#matches_conditions_hash def self.override_conditions_hash_matching?(subject, conditions) diff --git a/lib/cancan/model_adapters/data_mapper_adapter.rb b/lib/cancan/model_adapters/data_mapper_adapter.rb index 88058db..c36c51a 100644 --- a/lib/cancan/model_adapters/data_mapper_adapter.rb +++ b/lib/cancan/model_adapters/data_mapper_adapter.rb @@ -5,6 +5,10 @@ module CanCan model_class <= DataMapper::Resource end + def self.find(model_class, id) + model_class.get(id) + end + def self.override_conditions_hash_matching?(subject, conditions) conditions.any? { |k,v| !k.kind_of?(Symbol) } end diff --git a/spec/cancan/model_adapters/active_record_adapter_spec.rb b/spec/cancan/model_adapters/active_record_adapter_spec.rb index fc8c8a4..cf4155b 100644 --- a/spec/cancan/model_adapters/active_record_adapter_spec.rb +++ b/spec/cancan/model_adapters/active_record_adapter_spec.rb @@ -56,6 +56,11 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record" CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::ActiveRecordAdapter end + it "should find record" do + article = Article.create! + CanCan::ModelAdapters::ActiveRecordAdapter.find(Article, article.id).should == article + end + it "should not fetch any records when no abilities are defined" do Article.create! Article.accessible_by(@ability).should be_empty diff --git a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb index e2eb1f8..6c6c7f1 100644 --- a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb +++ b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb @@ -36,6 +36,11 @@ if ENV["MODEL_ADAPTER"] == "data_mapper" CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::DataMapperAdapter end + it "should find record" do + article = Article.create + CanCan::ModelAdapters::DataMapperAdapter.find(Article, article.id).should == article + end + it "should not fetch any records when no abilities are defined" do Article.create Article.accessible_by(@ability).should be_empty diff --git a/spec/cancan/model_adapters/mongoid_adapter_spec.rb b/spec/cancan/model_adapters/mongoid_adapter_spec.rb index 1ce0466..fc53d23 100644 --- a/spec/cancan/model_adapters/mongoid_adapter_spec.rb +++ b/spec/cancan/model_adapters/mongoid_adapter_spec.rb @@ -36,6 +36,11 @@ if ENV["MODEL_ADAPTER"] == "mongoid" CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject).should == CanCan::ModelAdapters::MongoidAdapter end + it "should find record" do + project = MongoidProject.create + CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id).should == project + end + it "should compare properties on mongoid documents with the conditions hash" do model = MongoidProject.new @ability.can :read, MongoidProject, :id => model.id