thoughtbot / friendly-identifier

A fork of http://code.google.com/p/friendly-identifier/

This URL has Read+Write access

cpytel (author)
Mon Apr 13 08:12:43 -0700 2009
commit  9ad4b37d3fe074bc8b2791b6ce0c0608d5ce8e8b
tree    279ccca7b3eeda03c951cb87f4c1ef5167dd4a67
parent  a7994221a585de989911b1b7cff1c057b8b00740
friendly-identifier / README
100644 65 lines (34 sloc) 2.43 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
=== Philosophy
 
It's nice to have human-readable identifiers in your URLs, but can be a bit of a pain to get just right. Overriding to_param to spit out some string after the id is a great technique in a pinch, but I felt like it was high time to come up with something a bit more comprehensive.
 
Friendly Identifier uses a source column (such as a name or title) to generate a more compact "friendly identifier". It overrides to_param to use that identifier, and also overrides ActiveRecord::Base#find to search using that identifier.
 
Ultimately, it's designed to boil down to a single line in your model that Just Works(tm).
 
=== Usage
 
friendly_identifier(source_column, options)
 
class Foo < ActiveRecord::Base
friendly_identifier :name
end
 
class Bar < ActiveRecord::Base
friendly_identifier :title, :scope => :category_id
end
 
class Baz < ActiveRecord::Base
friendly_identifier :title, :identifier_column => :url_slug
end
 
 
=== Options
 
* :keep_updated - Change the identifier whenever the field it is based on is changed. Defaults to true, but set to false if you need your identifiers to be customizable or URLs to remain unchanged after creation.
 
* :scope - Passed on to validates_uniqueness_of :friendly_identifier.
 
* :identifier_column - Pass in the name of an existing column you're already using and would like to reuse for the same sort functionality.
 
* Formatting callback: You can override "self.format(str)" in your class to provide your own identifier-formatting method.
 
 
=== Requirements
 
Your models simply need a string column named "friendly_identifier".
 
 
==== Caveats
 
Beware these possible side effects:
 
* In many cases, you don't want your URLs to change if you rename the name or title of your object. Use the :keep_updated => false option to handle this, and let the UI handle changing/updating your
 
* Can be a bit unpredictable with really complex associations (let me know if you have any trouble)
 
* Does validate presence, which effectively requires that your source column be present, so you might as well add a check for that.
 
 
==== Coming Soon(ish)
 
* Generator to create a migration for your models
 
 
==== Feedback Welcome!
 
Feel free to get in touch via email if you have problems, suggestions for improvement, or even just want to show me a site that you used this plugin on.
 
Nick Zadrozny<br/>
nick@zadrozny.com (email/jabber/gtalk)<br/>
http://beyondthepath.com/