Commit b77bb0fa authored by Alessandro Rodi's avatar Alessandro Rodi
Browse files

restore original find_by behaviour

parent 8a496277
......@@ -13,11 +13,16 @@ module CanCan
end
def find_resource_using_find_by
if resource_base.respond_to? 'find_by'
resource_base.send('find_by', @options[:find_by].to_sym => id_param)
else
resource_base.send(@options[:find_by], id_param)
end
find_by_dynamic_finder || find_by_find_by_finder || resource_base.send(@options[:find_by], id_param)
end
def find_by_dynamic_finder
method_name = "find_by_#{@options[:find_by]}!"
resource_base.send(method_name, id_param) if resource_base.respond_to? method_name
end
def find_by_find_by_finder
resource_base.find_by(@options[:find_by].to_sym => id_param) if resource_base.respond_to? :find_by
end
def id_param
......
......@@ -499,6 +499,15 @@ describe CanCan::ControllerResource do
expect(controller.instance_variable_get(:@model)).to eq(model)
end
it 'loads resource using dynamic finder method' do
model = Model.new
allow(Model).to receive(:find_by_name!).with('foo') { model }
params.merge!(action: 'show', id: 'foo')
resource = CanCan::ControllerResource.new(controller, find_by: :name)
resource.load_resource
expect(controller.instance_variable_get(:@model)).to eq(model)
end
it 'allows full find method to be passed into find_by option' do
model = Model.new
allow(Model).to receive(:find_by_name).with('foo') { model }
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment