Extracting to_param and find_one functionality out of friendly_id gem
For a recent project, we did our slug generation on our own, but wanted to plug in functionality as easy as the awesome friendly_id gem provides. So I extracted the relevant parts out of it and put it in its own module. In this way we use slugs in our models like this
class Post < ActiveRecord::Base
extend MyApp::Slug
end
and to_param
and find_one
are automatically overwritten, which means Rails generates urls for this models like /posts/my-new-post
. All other models keep there default behaviour. The author of friendly_id, Norman Clarke, calls this "poor man's refinement" and that its exactly what it is :) Read more about it here. I think we will get refinements in Ruby 2.0! Yay!
What I did:
- removed all the config stuff (the name of the attribute is hardcoded
slug
) - removed extension of
Object
and addedis_sluggy?
as method instead - removed all friendly_id addons like history for example, because we handle this on our own