Skip to content

Commit

Permalink
delegating ControllerResource find to model adapter, uses 'get' for D…
Browse files Browse the repository at this point in the history
…ataMapper - closes ryanb#373
  • Loading branch information
ryanb committed May 21, 2011
1 parent c031f82 commit 613ab1c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/cancan/controller_resource.rb
Expand Up @@ -107,11 +107,15 @@ def find_resource
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
Expand Down
5 changes: 5 additions & 0 deletions lib/cancan/model_adapters/abstract_adapter.rb
Expand Up @@ -15,6 +15,11 @@ def self.for_class?(member_class)
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)
Expand Down
4 changes: 4 additions & 0 deletions lib/cancan/model_adapters/data_mapper_adapter.rb
Expand Up @@ -5,6 +5,10 @@ def self.for_class?(model_class)
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
Expand Down
5 changes: 5 additions & 0 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Expand Up @@ -56,6 +56,11 @@
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
Expand Down
5 changes: 5 additions & 0 deletions spec/cancan/model_adapters/data_mapper_adapter_spec.rb
Expand Up @@ -36,6 +36,11 @@ class Comment
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
Expand Down
5 changes: 5 additions & 0 deletions spec/cancan/model_adapters/mongoid_adapter_spec.rb
Expand Up @@ -36,6 +36,11 @@ class MongoidProject
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
Expand Down

0 comments on commit 613ab1c

Please sign in to comment.