Fixed load_resource "find_by" in mongoid resources #705

Merged
merged 1 commit into from Feb 22, 2013

Conversation

Projects
None yet
5 participants
Contributor

albertobajo commented Jul 30, 2012

Latest versions of Mongoid supports "find_by" query, but syntax is slightly different than Active Record.

http://mongoid.org/en/mongoid/docs/querying.html#queries

Without this patch I could not use "find_by" option in load_and_authorize_resource filter.

@albertobajo albertobajo Fixed load_resource "find_by" in mongoid resources
Latest versions of Mongoid supports "find_by" query, but syntax
is slightly different than Active Record.
4a5700c

noazark commented Oct 23, 2012

Nice fix

inkstak commented Feb 18, 2013

Thanks for the fix. I use it since months. What about merging ?

Owner

ryanb commented Feb 22, 2013

Would be nice to get a test for this, but merging in since it is working for others. Thanks!

ryanb merged commit ba82241 into ryanb:master Feb 22, 2013

albertobajo deleted the albertobajo:mongoid_find_by branch Feb 22, 2013

@nirvdrum nirvdrum commented on the diff Oct 17, 2013

lib/cancan/controller_resource.rb
@@ -107,6 +107,8 @@ def find_resource
if @options[:find_by]
if resource_base.respond_to? "find_by_#{@options[:find_by]}!"
resource_base.send("find_by_#{@options[:find_by]}!", id_param)
+ elsif resource_base.respond_to? "find_by"
+ resource_base.send("find_by", { @options[:find_by].to_sym => id_param })
@nirvdrum

nirvdrum Oct 17, 2013

Is there any reason you need to do a dynamic call here? If you've already established that it responds to the method, you should be able to just call resource_base.find_by, which would be clearer and faster.

@nirvdrum nirvdrum commented on the diff Oct 17, 2013

lib/cancan/controller_resource.rb
@@ -107,6 +107,8 @@ def find_resource
if @options[:find_by]
if resource_base.respond_to? "find_by_#{@options[:find_by]}!"
resource_base.send("find_by_#{@options[:find_by]}!", id_param)
+ elsif resource_base.respond_to? "find_by"
@nirvdrum

nirvdrum Oct 17, 2013

You should use :find_by here. The symbol will be faster and reduce GC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment