friendly_id 1.9.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +81 -0
  3. data/MIT-LICENSE +19 -0
  4. data/Manifest.txt +53 -0
  5. data/README.rdoc +313 -0
  6. data/Rakefile +43 -0
  7. data/coverage/index.html +409 -0
  8. data/coverage/lib-friendly_id-non_sluggable_class_methods_rb.html +646 -0
  9. data/coverage/lib-friendly_id-non_sluggable_instance_methods_rb.html +638 -0
  10. data/coverage/lib-friendly_id-shoulda_macros_rb.html +641 -0
  11. data/coverage/lib-friendly_id-sluggable_class_methods_rb.html +714 -0
  12. data/coverage/lib-friendly_id-sluggable_instance_methods_rb.html +710 -0
  13. data/coverage/lib-friendly_id-string_helpers_rb.html +685 -0
  14. data/coverage/lib-friendly_id_rb.html +665 -0
  15. data/coverage/lib-slug_rb.html +695 -0
  16. data/coverage/rails-init_rb.html +606 -0
  17. data/friendly_id.gemspec +38 -0
  18. data/generators/friendly_id/friendly_id_generator.rb +12 -0
  19. data/generators/friendly_id/templates/create_slugs.rb +18 -0
  20. data/generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb +11 -0
  21. data/generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb +19 -0
  22. data/init.rb +1 -0
  23. data/lib/friendly_id.rb +61 -0
  24. data/lib/friendly_id/non_sluggable_class_methods.rb +41 -0
  25. data/lib/friendly_id/non_sluggable_instance_methods.rb +33 -0
  26. data/lib/friendly_id/shoulda_macros.rb +36 -0
  27. data/lib/friendly_id/slug.rb +90 -0
  28. data/lib/friendly_id/sluggable_class_methods.rb +109 -0
  29. data/lib/friendly_id/sluggable_instance_methods.rb +105 -0
  30. data/lib/friendly_id/version.rb +8 -0
  31. data/lib/tasks/friendly_id.rake +48 -0
  32. data/lib/tasks/friendly_id.rb +1 -0
  33. data/test/database.yml +3 -0
  34. data/test/fixtures/countries.yml +4 -0
  35. data/test/fixtures/country.rb +4 -0
  36. data/test/fixtures/people.yml +7 -0
  37. data/test/fixtures/person.rb +6 -0
  38. data/test/fixtures/post.rb +3 -0
  39. data/test/fixtures/posts.yml +19 -0
  40. data/test/fixtures/slugs.yml +45 -0
  41. data/test/fixtures/user.rb +3 -0
  42. data/test/fixtures/users.yml +7 -0
  43. data/test/non_slugged_test.rb +63 -0
  44. data/test/rails/2.x/app/controllers/application.rb +0 -0
  45. data/test/rails/2.x/config/boot.rb +109 -0
  46. data/test/rails/2.x/config/database.yml +3 -0
  47. data/test/rails/2.x/config/environment.rb +7 -0
  48. data/test/rails/2.x/config/environments/test.rb +6 -0
  49. data/test/rails/2.x/config/routes.rb +0 -0
  50. data/test/schema.rb +38 -0
  51. data/test/scoped_model_test.rb +21 -0
  52. data/test/slug_test.rb +87 -0
  53. data/test/sluggable_test.rb +181 -0
  54. data/test/test_helper.rb +35 -0
  55. metadata +155 -0
  56. metadata.gz.sig +1 -0
data.tar.gz.sig ADDED
Binary file
data/History.txt ADDED
@@ -0,0 +1,81 @@
1
+ == RELEASE DATE COMING SOON
2
+
3
+ * FriendlyId 2.0 Released
4
+ * Support for scoped slugs (Norman Clarke)
5
+ * Support for UTF-8 friendly_ids (Norman Clarke)
6
+ * Can now be installed via Ruby Gems, or as a Rails plugin (Norman Clarke)
7
+ * Shoulda macro ({Josh Nichols}[http://github.com/technicalpickles])
8
+ * Improved handling of non-unique slugs (Norman Clarke and Adrian Mugnolo)
9
+ * Various small bugfixes, cleanups and refactorings (Norman Clarke and Emilio Tagua)
10
+
11
+ == 2008-12-01
12
+
13
+ * Fixed bug that may return invalid records having similar id/names and using MySQL. (Emilio Tagua)
14
+ * Fixed slug generation to increment only numeric extension without modifying the name on duplicated slugs. (Emilio Tagua)
15
+
16
+ == 2008-10-31
17
+
18
+ * Fixed compatibility with Rails 2.0.x. (Norman Clarke)
19
+ * friendly_id::make_slugs update records in chunks of 1000 to avoid running out of memory with large datasets. (Tim Kadom)
20
+ * Fixed logic error with slug name collisions. Thanks to Tim Kadom for reporting this bug.
21
+
22
+ == 2008-10-22
23
+
24
+ * Reverted use of UTF8Handler - was causing errors for some people (Bence Nagy)
25
+ * Corrected find in case if a friendly_id begins with number (Bence Nagy)
26
+ * Added ability to reserve words from slugs (Adam Cigánek)
27
+
28
+ == 2008-10-09
29
+
30
+ * Moved "require"" for iconv to init.rb (Florian Aßmann)
31
+ * Removed "require" for Unicode, use Rails' handler instead (Florian Aßmann)
32
+ * Replaced some magic numbers with constants (Florian Aßmann)
33
+ * Don't overwrite find, alias_method_chain find_one and find_some instead (Florian Aßmann)
34
+ * Slugs behave more like ids now (Florian Aßmann)
35
+ * Can find by mixture of ids and slugs (Florian Aßmann)
36
+ * Reformatted code and comments (Florian Aßmann)
37
+ * Added support for Edge Rails' Inflector::parameterize (Norman Clarke)
38
+
39
+ == 2008-08-25
40
+
41
+ * Moved strip_diacritics into Slug for easier reuse/better organization.
42
+ * Put class methods inside class << self block. (Norman Clarke)
43
+
44
+ * Small change to allow friendly_id to work better with STI. (David Ramalho)
45
+
46
+ == 2008-07-14
47
+
48
+ * Improved slug generation for friendly id's with apostrophes. (alistairholt)
49
+ * Added support for namespaced models in Rakefile. (David Ramalho)
50
+
51
+ == 2008-06-23
52
+
53
+ * Cached most recent slug to improve performance (Emilio Tagua).
54
+
55
+ == 2008-06-10
56
+
57
+ * Added ability to find friendly_ids by array (Emilio Tagua)
58
+
59
+ == 2008-05-15
60
+
61
+ * Made friendly_id raise an error if slug method returns a blank value.
62
+
63
+ == 2008-05-12
64
+
65
+ * Added experimental Github gemspec.
66
+
67
+ == 2008-04-18
68
+
69
+ * Improved slug name collision avoidance.
70
+
71
+ == 2008-03-13
72
+
73
+ * Added :dependent => :destroy to slug relation, as suggested by Emilio Tagua.
74
+ * Fixed error when renaming a slugged item back to a previously used name.
75
+ * Incorporated documentation changes suggested by Jesse Crouch and Chris Nolan.
76
+
77
+ == 2008-02-07
78
+
79
+ * Applied patches from blog commenter "suntzu" to fix problem with model values were being overwritten.
80
+ * Applied patch from Dan Blue to make friendly_id no longer ignore options on ActiveRecordBase#find.
81
+ * Added call to options.assert_valid_keys in has_friendly_id. Thanks to W. Andrew Loe III for pointing out that this was missing.
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Norman Clarke and Adrian Mugnolo.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,53 @@
1
+ History.txt
2
+ MIT-LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ coverage/index.html
7
+ coverage/lib-friendly_id-non_sluggable_class_methods_rb.html
8
+ coverage/lib-friendly_id-non_sluggable_instance_methods_rb.html
9
+ coverage/lib-friendly_id-shoulda_macros_rb.html
10
+ coverage/lib-friendly_id-sluggable_class_methods_rb.html
11
+ coverage/lib-friendly_id-sluggable_instance_methods_rb.html
12
+ coverage/lib-friendly_id-string_helpers_rb.html
13
+ coverage/lib-friendly_id_rb.html
14
+ coverage/lib-slug_rb.html
15
+ coverage/rails-init_rb.html
16
+ friendly_id.gemspec
17
+ generators/friendly_id/friendly_id_generator.rb
18
+ generators/friendly_id/templates/create_slugs.rb
19
+ generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb
20
+ generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb
21
+ init.rb
22
+ lib/friendly_id.rb
23
+ lib/friendly_id/non_sluggable_class_methods.rb
24
+ lib/friendly_id/non_sluggable_instance_methods.rb
25
+ lib/friendly_id/shoulda_macros.rb
26
+ lib/friendly_id/slug.rb
27
+ lib/friendly_id/sluggable_class_methods.rb
28
+ lib/friendly_id/sluggable_instance_methods.rb
29
+ lib/friendly_id/version.rb
30
+ lib/tasks/friendly_id.rake
31
+ lib/tasks/friendly_id.rb
32
+ test/database.yml
33
+ test/fixtures/countries.yml
34
+ test/fixtures/country.rb
35
+ test/fixtures/people.yml
36
+ test/fixtures/person.rb
37
+ test/fixtures/post.rb
38
+ test/fixtures/posts.yml
39
+ test/fixtures/slugs.yml
40
+ test/fixtures/user.rb
41
+ test/fixtures/users.yml
42
+ test/non_slugged_test.rb
43
+ test/rails/2.x/app/controllers/application.rb
44
+ test/rails/2.x/config/boot.rb
45
+ test/rails/2.x/config/database.yml
46
+ test/rails/2.x/config/environment.rb
47
+ test/rails/2.x/config/environments/test.rb
48
+ test/rails/2.x/config/routes.rb
49
+ test/schema.rb
50
+ test/scoped_model_test.rb
51
+ test/slug_test.rb
52
+ test/sluggable_test.rb
53
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,313 @@
1
+ = FriendlyId
2
+
3
+ FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
4
+ Ruby on Rails. It allows you to create pretty URL's and work with
5
+ human-friendly strings as if they were numeric ids for ActiveRecord models.
6
+
7
+ Using FriendlyId, it's easy to make your application use URL's like:
8
+
9
+ http://example.com/states/washington
10
+
11
+ instead of:
12
+
13
+ http://example.com/states/4323454
14
+
15
+ Want to find out more? Read on. The {most recent version of the FriendlyId
16
+ RDocs}[http://friendly-id.rubyforge.org] can always be found on
17
+ Rubyforge[http://www.rubyforge.org].
18
+
19
+ === Why?
20
+
21
+ * Text-based id's look better
22
+ * They make URL's easier to remember.
23
+ * They give no hint about the number of records in your database.
24
+ * They are better for search engine optimization.
25
+
26
+ === But...
27
+
28
+ * They can change, breaking your URL's and your SEO.
29
+ * It can be tricky to ensure they're always unique.
30
+ * They can become a pain to manage in large Rails applications.
31
+ * They can conflict with your application's namespace.
32
+
33
+ FriendlyId tries to offer you the all the advantages, and avoid or soften the
34
+ potential impact of the disadvantages.
35
+
36
+ == Typical Uses
37
+
38
+ === User names ("non-slugged" models)
39
+
40
+ Usually users have unique user names stored in a column with a unique
41
+ constraint or index. In this case, all you need to do is add this to your
42
+ model:
43
+
44
+ has_friendly_id :login
45
+
46
+ and you can then write code like this:
47
+
48
+ @member = Member.find("joe") # the old Member.find(1) still works, too.
49
+ @member.to_param # returns "joe"
50
+ redirect_to @member # The URL would be /members/joe
51
+
52
+ === Blog posts ("slugged" models)
53
+
54
+ Blog posts generally have titles which are distinctive but not necessarily
55
+ unique. In this and similar cases, FriendlyId provides a Slug model separate
56
+ from your Post model. The Slug model handles duplicate friendly_ids, as well
57
+ as versioning.
58
+
59
+ Your model code would look something like this:
60
+
61
+ has_friendly_id :title, :use_slug => true
62
+
63
+ and you can then write code like this:
64
+
65
+ @post = Post.find("new-version-released") # Post.find(1) still works, too
66
+ @post.to_param # returns "new-version-released"
67
+ redirect_to @post # The URL would be /posts/new-version-released
68
+
69
+ Now in your controllers, if you want to prevent people from accessing your
70
+ models by numeric id, you can detect whether they were found by the
71
+ friendly_id:
72
+
73
+ @post = Post.find(params[:id])
74
+ raise "some error" if !@post.found_using_friendly_id?
75
+
76
+ or, you can 301 redirect if the model was found by the numeric id if you don't
77
+ care about numeric access, but want the SEO value of the friendly_id:
78
+
79
+ @post = Post.find(params[:id])
80
+ redirect_to @post, :status => 301 if @post.has_better_id?
81
+
82
+ The "has_better_id?" method returns true if the model was found with the
83
+ numeric id, or with an outdated slug.
84
+
85
+ == Extra Features
86
+
87
+ === Slug Versioning
88
+
89
+ FriendlyId will record changes to slugs so that you can tell when the model is
90
+ found with an older slug, or by the numeric id. This can be useful if you want
91
+ to do a 301 redirect to your updated URL.
92
+
93
+ class PostsController < ApplicationController
94
+
95
+ before_filter ensure_current_post_url, :only => :show
96
+
97
+ ...
98
+
99
+ def ensure_current_post_url
100
+ redirect_to @post, :status => :moved_permanently if @post.has_better_id?
101
+ end
102
+
103
+ end
104
+
105
+ This is particularly useful when implementing FrindlyId on an existing
106
+ website that already has many URL's with the old numeric id listed on search
107
+ engines. When the search engine spiders crawl your site, they will
108
+ eventually pick up the new, more SEO-friendly URL's.
109
+
110
+ === Non-unique Slug Names
111
+
112
+ FriendlyId will append a arbitrary number to the end of the id to keep it
113
+ unique if necessary:
114
+
115
+ /posts/new-version-released
116
+ /posts/new-version-released--2
117
+ /posts/new-version-released--3
118
+ ...
119
+ etc.
120
+
121
+ Note that the number is preceeded by two dashes to distinguish it from the
122
+ rest of the slug. This is important to enable having slugs like:
123
+
124
+ /cars/peugeot-206
125
+ /cars/peugeot-206--2
126
+
127
+ === Reserved Names
128
+
129
+ You can mark off some strings as reserved so that, for example, you don't end
130
+ up with this problem:
131
+
132
+ /users/joe-schmoe # A user chose "joe schmoe" as his user name - no worries.
133
+ /users/new # A user chose "new" as his user name, and now no one can sign up.
134
+
135
+ Here's how to do it:
136
+
137
+ class Restaurant < ActiveRecord::Base
138
+ belongs_to :city
139
+ has_friendly_id :name, :use_slug => true, :reserved => ["new", "index"]
140
+ end
141
+
142
+
143
+ === Scoped Slugs
144
+
145
+ FriendlyId can generate unique slugs within a given scope. For example:
146
+
147
+ class Restaurant < ActiveRecord::Base
148
+ belongs_to :city
149
+ has_friendly_id :name, :use_slug => true, :scope => :city
150
+ end
151
+
152
+ class City < ActiveRecord::Base
153
+ has_many :restaurants
154
+ has_friendly_id :name, :use_slug => true
155
+ end
156
+
157
+ http://example.org/cities/seattle/restaurants/joes-diner
158
+ http://example.org/cities/chicago/restaurants/joes-diner
159
+
160
+ Restaurant.find("joes-diner", :scope => "seattle") # returns 1 record
161
+ Restaurant.find("joes-diner", :scope => "chicago") # returns 1 record
162
+ Restaurant.find("joes-diner") # returns both records
163
+
164
+
165
+ The value for the :scope key in your model can be a custom method you define,
166
+ or the name of a relation. If it's the name of a relation, then the scope's
167
+ text value will be the result of calling <code>to_param</code> on the related
168
+ model record. In the example above, the city model also uses FriendlyId and so
169
+ its <code>to_param</code> method returns its friendly_id: chicago or seattle.
170
+
171
+ This feature is new in FriendlyId 2 and should be considered of experimental
172
+ quality. Please don't use this for code that needs to run on the Space
173
+ Shuttle.
174
+
175
+ === Text Normalization
176
+
177
+ FriendlyId's slugging can strip diacritics from Western European characters,
178
+ so that you can have ASCII-only URL's; for example, conveting "ñøîéçü" to
179
+ "noiecu."
180
+
181
+ has_friendly_id :title, :use_slug => true, :strip_diacritics => true
182
+
183
+ If you are not using slugs, you'll have to do this manually for whatever value
184
+ you're using as the friendly_id.
185
+
186
+ === Diacritic-sensitive normalization
187
+
188
+ FriendlyId can also normalize slug text while preserving accented characters, if
189
+ you prefer to leave them in your URL's:
190
+
191
+ has_friendly_id :title, :use_slug => true
192
+ ...
193
+ @post = Post.create(:title => "¡Feliz Año!")
194
+ @post.friendly_id # "feliz-año"
195
+
196
+ === Unicode URL's
197
+
198
+ FriendlyId can generate slugs in any language that can be written with
199
+ Unicode. It does its best to strip away punctuation regardless of the language
200
+ being used. Since the authors only speak English, Spanish, Portuguese and
201
+ German, this has not been extensively tested with anything like Chinese,
202
+ Russian, Greek, etc, but it "should work." If you're a speaker of a language
203
+ that uses a non-Roman writing system, your feedback would be most welcome.
204
+
205
+ has_friendly_id :title, :use_slug => true
206
+ ...
207
+ @post = Post.create(:title => "友好编号在中国")
208
+ @post.friendly_id # "友好编号在中国"
209
+ @post2 = Post.create(:title => "友好编号在中国")
210
+ @post2.friendly_id # "友好编号在中国--2"
211
+
212
+ == Getting it
213
+
214
+ FriendlyId is installed as a Ruby Gem:
215
+
216
+ gem install friendly-id
217
+
218
+ Alternatively, you can install it as a Rails plugin, though this is
219
+ discouraged:
220
+
221
+ ./script/plugin install git://github.com/norman/friendly_id.git
222
+
223
+ == Setting it up
224
+
225
+ FriendlyId currently works with Rails 2.0.0 and higher. Here's how to set it up.
226
+
227
+ 1) Install the Gem:
228
+
229
+ sudo gem install friendly-id
230
+ cd my_app
231
+ script/generate friendly_id
232
+ rake db:migrate
233
+
234
+ 2) Load FriendlyId in your app:
235
+
236
+ # Rails 2.1 and higher; add this to the gem section of environment.rb:
237
+ config.gem "friendly_id"
238
+
239
+ # Rails 2.0; this goes at the bottom of environment.rb
240
+ require 'friendly_id'
241
+
242
+ 3) Add some code to your models:
243
+
244
+ class Post < ActiveRecord::Base
245
+ has_friendly_id :title, :use_slug => true
246
+ end
247
+
248
+ 4) If you are using slugs, you can use a Rake task to generate slugs for your
249
+ existing records:
250
+
251
+ friendly_id:make_slugs MODEL=MyModelName
252
+
253
+ If you eventually want to expire old slugs every so often, or perhaps every
254
+ day via cron, you can do:
255
+
256
+ rake:friendly_id:remove_old_slugs
257
+
258
+ The default is to remove dead slugs older than 45 days, but is configurable:
259
+
260
+ rake:friendly_id:remove_old_slugs MODEL=MyModelName DAYS=60
261
+
262
+ == Upgrading from an older version
263
+
264
+ If you installed an older version of FriendlyId and want to upgrade to 2.0,
265
+ follow these steps:
266
+
267
+ ==== Install the friendly-id Gem:
268
+
269
+ sudo gem install friendly-id
270
+
271
+ ==== Add FriendlyId to environment.rb:
272
+
273
+ ===== For Rails 2.1 and higher:
274
+
275
+ config.gem "friendly_id", :version => ">= 2.0.0"
276
+
277
+ ===== For Rails 2.0:
278
+
279
+ Add this to the bottom of environment.rb:
280
+
281
+ require 'friendly_id'
282
+
283
+ ==== Remove the older version of FriendlyId:
284
+
285
+ git rm -rf vendor/plugins/friendly_id
286
+ svn delete vendor/plugins/friendly_id
287
+ # or whatever
288
+
289
+ ==== Generate the upgrade migration and run it
290
+
291
+ ./script generate friendly_id_20_upgrade
292
+ rake db:migrate
293
+
294
+ That's it!
295
+
296
+
297
+ == Hacking FriendlyId:
298
+
299
+ FriendlyId is {hosted on Github}[git://github.com/norman/friendly_id.git], and
300
+ we love pull requests. :-)
301
+
302
+ == Bugs:
303
+
304
+ Please report them on Lighthouse[http://randomba.lighthouseapp.com/projects/14675-friendly_id].
305
+
306
+
307
+ == Credits:
308
+
309
+ FriendlyId was created by {Norman Clarke}[mailto:norman@randomba.org],
310
+ {Adrian Mugnolo}[mailto:adrian@randomba.org], and {Emilio Tagua}[mailto:miloops@gmail.com].
311
+
312
+ Copyright (c) 2008 Norman Clarke, Adrian Mugnolo and Emilio Tagua, released
313
+ under the MIT license.