Using Permalinks in ROR with permalink_fu plugin is pretty straight forward.
In my current project the permalinks don’t have to be :
- foo-bar
- foo-bar-1
- foo-bar-2
They can be:
- foo-bar
- foo-bar
- foo-bar
Its OK for multiple objects to have same permalinks.
Reason: We prefix the object-id to the permalink in the to_param method of the class.
This way, the url generated for each object will anyways be distinct. Say:
- /books/1-foo-bar (assuming object-id = 1)
- /books/2-foo-bar(assuming object-id = 2)
- /books/3-foo-bar(assuming object-id = 3)
As the object_id is responsible for making each URL distinct, we don’t need the permalink also to have a post-fix to make it distinct for each object.
But adding the post-fix to the permalink is the default behavior of the permalink_fu.
Below is a patch to permalink_fu which makes this default behavior mutable. (The following methods are to be replaced/added in RAILS_ROOT/vendor/plugins/permalink_fu/lib/permalink_fu.rb)
Replace the following Methods:
Add Private Method:
Now, after changing the permalink_fu.rb, we have to specify in the model’s has_permalink association the :unique option. If true, it will generate a distinct permalink for each object (bypassing the changes to permalink_fu.rb). Else, will generate a permalink irrespective of the other permalinks. Eg:
The whole patched file is available at : satya – Github
Note: Even after this tweak, the default behavior of permalink_fu remains the same i.e observing the unique constraint. We explicitly have to mention (:unique=>false) in the model for this tweak to work. This way, an existing models using this plugin would not be effected.