diff --git a/cancan.gemspec b/cancan.gemspec index 492f7d6..4d052e8 100644 --- a/cancan.gemspec +++ b/cancan.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.require_path = "lib" s.add_development_dependency 'rspec', '~> 2.1.0' - s.add_development_dependency 'rails', '~> 3.0.0' + s.add_development_dependency 'rails', '~> 3.0.7' s.add_development_dependency 'rr', '~> 0.10.11' # 1.0.0 has respond_to? issues: http://github.com/btakita/rr/issues/issue/43 s.add_development_dependency 'supermodel', '~> 0.1.4' diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 5faf640..10cf4b6 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -100,7 +100,15 @@ module CanCan if @options[:singleton] && parent_resource.respond_to?(name) parent_resource.send(name) else - @options[:find_by] ? resource_base.send("find_by_#{@options[:find_by]}!", id_param) : resource_base.find(id_param) + if @options[:find_by] + if resource_base.respond_to? "find_by_#{@options[:find_by]}!" + resource_base.send("find_by_#{@options[:find_by]}!", id_param) + else + resource_base.send(@options[:find_by], id_param) + end + else + resource_base.find(id_param) + end end end diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 3e96506..7033c04 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -336,6 +336,14 @@ describe CanCan::ControllerResource do @controller.instance_variable_get(:@project).should == project end + it "should allow full find method to be passed into find_by option" do + project = Project.create!(:name => "foo") + @params.merge!(:action => "show", :id => "foo") + resource = CanCan::ControllerResource.new(@controller, :find_by => :find_by_name) + resource.load_resource + @controller.instance_variable_get(:@project).should == project + end + it "should raise ImplementationRemoved when adding :name option" do lambda { CanCan::ControllerResource.new(@controller, :name => :foo) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f682c30..4deccd1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,4 +30,12 @@ end class Project < SuperModel::Base belongs_to :category attr_accessor :category # why doesn't SuperModel do this automatically? + + def self.respond_to?(method, include_private = false) + if method.to_s == "find_by_name!" # hack to simulate ActiveRecord + true + else + super + end + end end