Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

FriendlyId 5 Support #892

Closed
wants to merge 1 commit into from

6 participants

Alfred Rowe Jared Beck tmedford Andrew Culver Sami Haahtinen fryyyy
Alfred Rowe

Quick Fix until a better solution is provided

Jared Beck

You should probably rescue NoMethodError instead of a blanket rescue.

Also, does cancan claim to support friendlyid? I'm not sure cancan will want to include your self-confessed "nasty hack" just to support another library, even one as popular as friendly_id.

You might consider writing an adapter gem, eg. cancan-friendly_id, which would re-open CanCan::ModelAdapters::AbstractAdapter and overwrite find.

Honestly, cancan is so busy trying to get support for strong_parameters out the door that it doesn't seem to be accepting new features anyway. So, an adapter gem is probably a better way to get your idea published.

tmedford

This needed in my application. Thanks

Andrew Culver

According to the friendly_id documentation, you can re-enable the overloading of "find" like so:

friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')

I was able to use this to restore compatibility between the two libraries without modifying cancan.

Alfred Rowe

Super! Thanks for that info, I will definitely use that technique.

Alfred Rowe

@jaredbeck sorry for the very late reply. You are right, there's a better way however via @andrewculver comment. I will definitely take your advise next time I contribute, I honestly understand you.

Cheers!

Jared Beck

Glad to hear you guys found a workaround. Please close this issue. Thanks!

Alfred Rowe

Yep! Closing curtains. Cheers!

Alfred Rowe nukturnal closed this
Sami Haahtinen

Dropping a note here, even if the issue has already been closed. You can also use

load_and_authorize_resource find_by: :slug

That will work without overriding the finder.

Alfred Rowe
fryyyy

load_and_authorize_resource find_by: :slug
unfortunately doesnt seem to work with rails 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 28, 2013
  1. Alfred Rowe
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 0 deletions.
  1. +6 −0 lib/cancan/model_adapters/abstract_adapter.rb
6 lib/cancan/model_adapters/abstract_adapter.rb
View
@@ -17,6 +17,12 @@ def self.for_class?(member_class)
# Override if you need custom find behavior
def self.find(model_class, id)
+
+ # Nasty hack to properly work with FriendlyId 5 due to refactoring
+ # FriendlyId 5 does not overwrite the find() method on ActiveRecord anymore
+ # Hence friendly urls are not properly parsed
+ model_class.friendly.find(id)
+ rescue
model_class.find(id)
end
Something went wrong with that request. Please try again.