friendly_id 5.0.0.alpha.1 → 5.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44f9c1a8ef2afb5bda90485d0df78b67da83e73d
4
- data.tar.gz: 722909dc863dd55275aa403ee4321c99af1ea118
3
+ metadata.gz: 3a7d80631aa03de549650e0cc84c04108204af5c
4
+ data.tar.gz: c868670182414c4846e8c4a94aa34d680f78c1a7
5
5
  SHA512:
6
- metadata.gz: d0949ad9c3d10238bbc4f6f350bd4d8b7101485679f3d55ef78c902b1a0f280d738b89f5753bd467d593ab8ec91ca7bc412323083114140f1fea5a061c27755f
7
- data.tar.gz: d561df0002650904d1b087185f9d5a94ad6e191ab88925fe9e0974894affaafa55e92db2774cc3fe364ead7fef85dd6cade495f7f47feeeda54efe5b04b2681e
6
+ metadata.gz: 203a56c05ef6d18ab3ba8b9de406749a55689d25fb3776b744986c753bcd0a174ffe8f364521f5cca9255b7f196feea76f5fd94bde48c63d8bc4fb2e9a3f8ce9
7
+ data.tar.gz: 195f31cb8264650bb0b9922dc436575fa3ba33c4c0ecb8f4f2efb2c62033131f11e93d9ba9fff3c710b67bcd0dd96c5601e06a6b5cb9678e72b8abab6b985d2a
data/.yardopts CHANGED
@@ -1,4 +1,5 @@
1
- --files=WhatsNew.md,Changelog.md,Guide.rdoc
1
+ --files=Changelog.md,Guide.md
2
2
  --private
3
3
  --protected
4
4
  --exclude lib/friendly_id/migration
5
+ --markup=markdown
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,36 @@
1
+ # FriendlyId
2
+
3
+ ## Getting Help
4
+
5
+ Before opening a new issue, please search the existing issues to see if your
6
+ question has been answered before.
7
+
8
+ Please also take a look at the extensive documentation, especially the Guide.
9
+
10
+ ## FAQ
11
+
12
+ Here are the answers to some common questions.
13
+
14
+ ### I'm using Rails 4...
15
+
16
+ Then you **must** use FriendlyId 5, which is currently available in the master
17
+ branch of the repository, and will be released to Rubygems soon.
18
+
19
+ ### How do I set up my routes for FriendlyId?
20
+
21
+ Exactly like in any other Rails app. FriendlyId doesn't change any routing
22
+ functionality, it only changes the ids that are shown in your routes.
23
+
24
+ TIP: Make your app work the way you want to with numeric id's. Then add
25
+ FriendlyId later.
26
+
27
+ ### I'm seeing `undefined method `friendly_id'`
28
+
29
+ You probably just added the friendly_id gem. Run `bundle` and restart your
30
+ Rails app.
31
+
32
+ ### I'm seeing `uninitialized constant FriendlyId`
33
+
34
+ You probably just added the friendly_id gem. Run `bundle` and restart your
35
+ Rails app.
36
+
data/Changelog.md CHANGED
@@ -3,16 +3,23 @@
3
3
  We would like to think our many {file:Contributors contributors} for
4
4
  suggestions, ideas and improvements to FriendlyId.
5
5
 
6
- * Table of Contents
7
- {:toc}
6
+ ## 5.0.0.beta1 (2013-08-10)
8
7
 
9
- ## 4.1.0.beta.1 (2013-01-09)
8
+ * Support for Rails 4.
9
+ * Made the :scoped and :history modules compatible with each other (Andre Duffeck).
10
+ * Removed class-level finders in favor of `friendly` scope (Norman Clarke).
11
+ * Implemented "candidates" support (Norman Clarke).
12
+ * Slug "sequences" are now GUIDs rather than numbers (Norman Clarke).
13
+ * `find` no longer falls back to super unless id is fully numeric string (Norman Clarke).
14
+ * Default sequence separator is now '-' rather than '--'.
15
+ * Support for Globalize has been removed until Globalize supports Rails 4.
16
+ * Removed upport for Ruby < 1.9.3 and Rails < 4.0.
10
17
 
11
- Made the :scoped and :history modules compatible with each other (Andre Duffeck)
12
18
 
13
- ## 4.0.10 (NOT RELEASED YET)
19
+ ## 4.0.10 (2013-08-10)
14
20
 
15
- * Fixed table prefixes/suffixes being ignored (Jesse Farless)
21
+ * Fixed table prefixes/suffixes being ignored (Jesse Farless).
22
+ * Fixed sequence generation for slugs containing numbers (Adam Carroll).
16
23
 
17
24
  ## 4.0.9 (2012-10-31)
18
25
 
@@ -38,7 +45,7 @@ Made the :scoped and :history modules compatible with each other (Andre Duffeck)
38
45
 
39
46
  ## 4.0.7 (2012-06-06)
40
47
 
41
- * to_param just calls super when no friendly_id is present, to keep the model's
48
+ * to_param just calls super when no friendly id is present, to keep the model's
42
49
  default behavior. (Andrew White)
43
50
 
44
51
  * FriendlyId can now properly sequence slugs that end in numbers even when a
@@ -1,7 +1,6 @@
1
- #encoding: utf-8
2
1
 
3
2
 
4
- == About FriendlyId
3
+ ## About FriendlyId
5
4
 
6
5
  FriendlyId is an add-on to Ruby's Active Record that allows you to replace ids
7
6
  in your URLs with strings:
@@ -15,28 +14,27 @@ in your URLs with strings:
15
14
  It requires few changes to your application code and offers flexibility,
16
15
  performance and a well-documented codebase.
17
16
 
18
- === Core Concepts
17
+ ### Core Concepts
19
18
 
20
- ==== Slugs
19
+ #### Slugs
21
20
 
22
- The concept of "slugs[http://en.wikipedia.org/wiki/Slug_(web_publishing)]" is at
23
- the heart of FriendlyId.
21
+ The concept of *slugs* is at the heart of FriendlyId.
24
22
 
25
23
  A slug is the part of a URL which identifies a page using human-readable
26
24
  keywords, rather than an opaque identifier such as a numeric id. This can make
27
25
  your application more friendly both for users and search engine.
28
26
 
29
- ==== Finders: Slugs Act Like Numeric IDs
27
+ #### Finders: Slugs Act Like Numeric IDs
30
28
 
31
29
  To the extent possible, FriendlyId lets you treat text-based identifiers like
32
30
  normal IDs. This means that you can perform finds with slugs just like you do
33
31
  with numeric ids:
34
32
 
35
33
  Person.find(82542335)
36
- Person.find("joe")
34
+ Person.friendly.find("joe")
37
35
 
38
36
 
39
- == Setting Up FriendlyId in Your Model
37
+ ## Setting Up FriendlyId in Your Model
40
38
 
41
39
  To use FriendlyId in your ActiveRecord models, you must first either extend or
42
40
  include the FriendlyId module (it makes no difference), then invoke the
@@ -53,7 +51,7 @@ addons it should use. See the documentation for this method for a list of all
53
51
  available addons, or skim through the rest of the docs to get a high-level
54
52
  overview.
55
53
 
56
- === The Default Setup: Simple Models
54
+ ### The Default Setup: Simple Models
57
55
 
58
56
  The simplest way to use FriendlyId is with a model that has a uniquely indexed
59
57
  column with no spaces or special characters, and that is seldom or never
@@ -65,9 +63,9 @@ updated. The most common example of this is a user name:
65
63
  validates_format_of :login, :with => /\A[a-z0-9]+\z/i
66
64
  end
67
65
 
68
- @user = User.find "joe" # the old User.find(1) still works, too
69
- @user.to_param # returns "joe"
70
- redirect_to @user # the URL will be /users/joe
66
+ @user = User.friendly.find "joe" # the old User.find(1) still works, too
67
+ @user.to_param # returns "joe"
68
+ redirect_to @user # the URL will be /users/joe
71
69
 
72
70
  In this case, FriendlyId assumes you want to use the column as-is; it will never
73
71
  modify the value of the column, and your application should ensure that the
@@ -78,7 +76,7 @@ value is unique and admissible in a URL:
78
76
  friendly_id :name
79
77
  end
80
78
 
81
- @city.find "Viña del Mar"
79
+ @city.friendly.find "Viña del Mar"
82
80
  redirect_to @city # the URL will be /cities/Viña%20del%20Mar
83
81
 
84
82
  Writing the code to process an arbitrary string into a good identifier for use
@@ -86,7 +84,7 @@ in a URL can be repetitive and surprisingly tricky, so for this reason it's
86
84
  often better and easier to use {FriendlyId::Slugged slugs}.
87
85
 
88
86
 
89
- == Slugged Models
87
+ ## Slugged Models
90
88
 
91
89
  FriendlyId can use a separate column to store slugs for models which require
92
90
  some text processing.
@@ -111,13 +109,13 @@ To activate the slugging functionality, use the {FriendlyId::Slugged} module.
111
109
 
112
110
  FriendlyId will generate slugs from a method or column that you specify, and
113
111
  store them in a field in your model. By default, this field must be named
114
- +:slug+, though you may change this using the
112
+ `:slug`, though you may change this using the
115
113
  {FriendlyId::Slugged::Configuration#slug_column slug_column} configuration
116
114
  option. You should add an index to this column, and in most cases, make it
117
115
  unique. You may also wish to constrain it to NOT NULL, but this depends on your
118
116
  app's behavior and requirements.
119
117
 
120
- === Example Setup
118
+ ### Example Setup
121
119
 
122
120
  # your model
123
121
  class Post < ActiveRecord::Base
@@ -143,119 +141,144 @@ app's behavior and requirements.
143
141
  end
144
142
  end
145
143
 
146
- === Working With Slugs
144
+ ### Working With Slugs
147
145
 
148
- ==== Formatting
146
+ #### Formatting
149
147
 
150
148
  By default, FriendlyId uses Active Support's
151
- paramaterize[http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize]
149
+ [paramaterize](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize)
152
150
  method to create slugs. This method will intelligently replace spaces with
153
151
  dashes, and Unicode Latin characters with ASCII approximations:
154
152
 
155
- movie = Movie.create! :title => "Der Preis fürs Überleben"
156
- movie.slug #=> "der-preis-furs-uberleben"
153
+ movie = Movie.create! :title => "Der Preis fürs Überleben"
154
+ movie.slug #=> "der-preis-furs-uberleben"
157
155
 
158
- ==== Uniqueness
156
+ #### Column or Method?
159
157
 
160
- When you try to insert a record that would generate a duplicate friendly id,
161
- FriendlyId will append a sequence to the generated slug to ensure uniqueness:
158
+ FriendlyId always uses a method as the basis of the slug text - not a column. It
159
+ first glance, this may sound confusing, but remember that Active Record provides
160
+ methods for each column in a model's associated table, and that's what
161
+ FriendlyId uses.
162
162
 
163
- car = Car.create :title => "Peugot 206"
164
- car2 = Car.create :title => "Peugot 206"
163
+ Here's an example of a class that uses a custom method to generate the slug:
165
164
 
166
- car.friendly_id #=> "peugot-206"
167
- car2.friendly_id #=> "peugot-206-f9f3789a-daec-4156-af1d-fab81aa16ee5"
165
+ class Person < ActiveRecord::Base
166
+ friendly_id :name_and_location
167
+ def name_and_location
168
+ "#{name} from #{location}"
169
+ end
170
+ end
168
171
 
169
- ==== Sequence Separator - The Two Dashes
172
+ bob = Person.create! :name => "Bob Smith", :location => "New York City"
173
+ bob.friendly_id #=> "bob-smith-from-new-york-city"
170
174
 
171
- By default, FriendlyId uses a dash to separate the slug from a sequence.
175
+ FriendlyId refers to this internally as the "base" method.
172
176
 
173
- You can change this with the {FriendlyId::Slugged::Configuration#sequence_separator
174
- sequence_separator} configuration option.
177
+ #### Uniqueness
175
178
 
176
- ==== Column or Method?
179
+ When you try to insert a record that would generate a duplicate friendly id,
180
+ FriendlyId will append a UUID to the generated slug to ensure uniqueness:
177
181
 
178
- FriendlyId always uses a method as the basis of the slug text - not a column. It
179
- first glance, this may sound confusing, but remember that Active Record provides
180
- methods for each column in a model's associated table, and that's what
181
- FriendlyId uses.
182
+ car = Car.create :title => "Peugot 206"
183
+ car2 = Car.create :title => "Peugot 206"
182
184
 
183
- Here's an example of a class that uses a custom method to generate the slug:
185
+ car.friendly_id #=> "peugot-206"
186
+ car2.friendly_id #=> "peugot-206-f9f3789a-daec-4156-af1d-fab81aa16ee5"
184
187
 
185
- class Person < ActiveRecord::Base
186
- friendly_id :name_and_location
187
- def name_and_location
188
- "#{name} from #{location}"
188
+ Previous versions of FriendlyId appended a numeric sequence a to make slugs
189
+ unique, but this was removed to simplify using FriendlyId in concurrent code.
190
+
191
+ #### Candidates
192
+
193
+ Since UUIDs are ugly, FriendlyId provides a "slug candidates" functionality to
194
+ let you specify alternate slugs to use in the event the one you want to use is
195
+ already taken. For example:
196
+
197
+ class Restaurant < ActiveRecord::Base
198
+ extend FriendlyId
199
+ friendly_id :slug_candidates, use: :slugged
200
+
201
+ # Try building a slug based on the following fields in
202
+ # increasing order of specificity.
203
+ def slug_candidates
204
+ [
205
+ :name,
206
+ [:name, :city],
207
+ [:name, :street, :city],
208
+ [:name, :street_number, :street, :city]
209
+ ]
210
+ end
189
211
  end
190
- end
191
212
 
192
- bob = Person.create! :name => "Bob Smith", :location => "New York City"
193
- bob.friendly_id #=> "bob-smith-from-new-york-city"
213
+ r1 = Restaurant.create! name: 'Plaza Diner', city: 'New Paltz'
214
+ r2 = Restaurant.create! name: 'Plaza Diner', city: 'Kingston'
194
215
 
195
- ==== Providing Your Own Slug Processing Method
216
+ r1.friendly_id #=> 'plaza-diner'
217
+ r2.friendly_id #=> 'plaza-diner-kingston'
196
218
 
197
- You can override {FriendlyId::Slugged#normalize_friendly_id} in your model for
198
- total control over the slug format.
219
+ To use candidates, make your FriendlyId base method return an array. The
220
+ method need not be named `slug_candidates`; it can be anything you want. The
221
+ array may contain any combination of symbols, strings, procs or lambdas and
222
+ will be evaluated lazily and in order. If you include symbols, FriendlyId will
223
+ invoke a method on your model class with the same name. Strings will be
224
+ interpreted literally. Procs and lambdas will be called and their return values
225
+ used as the basis of the friendly id. If none of the candidates can generate a
226
+ unique slug, then FriendlyId will append a UUID to the first candidate as a
227
+ last resort.
199
228
 
200
- ==== Deciding When to Generate New Slugs
229
+ #### Sequence Separator
201
230
 
202
- Overriding {FriendlyId::Slugged#should_generate_new_friendly_id?} lets you
203
- control whether new friendly ids are created when a model is updated. For
204
- example, if you only want to generate slugs once and then treat them as
205
- read-only:
231
+ By default, FriendlyId uses a dash to separate the slug from a sequence.
206
232
 
207
- class Post < ActiveRecord::Base
208
- extend FriendlyId
209
- friendly_id :title, :use => :slugged
233
+ You can change this with the {FriendlyId::Slugged::Configuration#sequence_separator
234
+ sequence_separator} configuration option.
210
235
 
211
- def should_generate_new_friendly_id?
212
- new_record?
213
- end
214
- end
236
+ #### Providing Your Own Slug Processing Method
215
237
 
216
- post = Post.create!(:title => "Hello world!")
217
- post.slug #=> "hello-world"
218
- post.title = "Hello there, world!"
219
- post.save!
220
- post.slug #=> "hello-world"
238
+ You can override {FriendlyId::Slugged#normalize_friendly_id} in your model for
239
+ total control over the slug format. It will be invoked for any generated slug,
240
+ whether for a single slug or for slug candidates.
221
241
 
222
- ==== Locale-specific Transliterations
242
+ #### Deciding When to Generate New Slugs
223
243
 
224
- Active Support's +parameterize+ uses
225
- transliterate[http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate],
226
- which in turn can use I18n's transliteration rules to consider the current
227
- locale when replacing Latin characters:
244
+ Previous versions of FriendlyId provided a method named
245
+ `should_generate_new_friendly_id?` which you could override to control when new
246
+ slugs were generated.
228
247
 
229
- # config/locales/de.yml
230
- de:
231
- i18n:
232
- transliterate:
233
- rule:
234
- ü: "ue"
235
- ö: "oe"
236
- etc...
248
+ As of FriendlyId 5.0, slugs are only generated when the `slug` field is nil. If
249
+ you want a slug to be regenerated, you must explicity set the field to nil:
237
250
 
238
- movie = Movie.create! :title => "Der Preis fürs Überleben"
239
- movie.slug #=> "der-preis-fuers-ueberleben"
251
+ restaurant.friendly_id # joes-diner
252
+ restaurant.name = "The Plaza Diner"
253
+ restaurant.save!
254
+ restaurant.friendly_id # joes-diner
255
+ restaurant.slug = nil
256
+ restaurant.save!
257
+ restaurant.friendly_id # the-plaza-diner
240
258
 
241
- This functionality was in fact taken from earlier versions of FriendlyId.
242
259
 
243
- ==== Gotchas: Common Problems
260
+ #### Locale-specific Transliterations
244
261
 
245
- ===== Slugs That Begin With Numbers
262
+ Active Support's `parameterize` uses
263
+ [transliterate](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate),
264
+ which in turn can use I18n's transliteration rules to consider the current
265
+ locale when replacing Latin characters:
246
266
 
247
- Ruby's `to_i` function casts strings to integers in such a way that +23abc.to_i+
248
- returns 23. Because FriendlyId falls back to finding by numeric id, this means
249
- that if you attempt to find a record with a non-existant slug, and that slug
250
- begins with a number, your find will probably return the wrong record.
267
+ # config/locales/de.yml
268
+ de:
269
+ i18n:
270
+ transliterate:
271
+ rule:
272
+ ü: "ue"
273
+ ö: "oe"
274
+ etc...
251
275
 
252
- There are two fairly simple ways to avoid this:
276
+ movie = Movie.create! :title => "Der Preis fürs Überleben"
277
+ movie.slug #=> "der-preis-fuers-ueberleben"
253
278
 
254
- * Use validations to ensure that slugs don't begin with numbers.
255
- * Use explicit finders like +find_by_id+ to always find by the numeric id, or
256
- +find_by_slug+ to always find using the friendly id.
279
+ This functionality was in fact taken from earlier versions of FriendlyId.
257
280
 
258
- ===== Concurrency Issues
281
+ #### Gotchas: Common Problems
259
282
 
260
283
  FriendlyId uses a before_validation callback to generate and set the slug. This
261
284
  means that if you create two model instances before saving them, it's possible
@@ -266,15 +289,11 @@ attributes creates more than one record for a model that uses friendly_id. The
266
289
  second, in concurrent code, either in threads or multiple processes.
267
290
 
268
291
  To solve the nested attributes issue, I recommend simply avoiding them when
269
- creating more than one nested record for a model that uses FriendlyId. See {this
270
- Github issue}[https://github.com/FriendlyId/friendly_id/issues/185] for discussion.
271
-
272
- To solve the concurrency issue, I recommend locking the model's table against
273
- inserts while when saving the record. See {this Github
274
- issue}[https://github.com/FriendlyId/friendly_id/issues/180] for discussion.
292
+ creating more than one nested record for a model that uses FriendlyId. See [this
293
+ Github issue](https://github.com/norman/friendly_id/issues/185) for discussion.
275
294
 
276
295
 
277
- == History: Avoiding 404's When Slugs Change
296
+ ## History: Avoiding 404's When Slugs Change
278
297
 
279
298
  FriendlyId's {FriendlyId::History History} module adds the ability to store a
280
299
  log of a model's slugs, so that when its friendly id changes, it's still
@@ -282,26 +301,24 @@ possible to perform finds by the old id.
282
301
 
283
302
  The primary use case for this is avoiding broken URLs.
284
303
 
285
- === Setup
304
+ ### Setup
286
305
 
287
306
  In order to use this module, you must add a table to your database schema to
288
307
  store the slug records. FriendlyId provides a generator for this purpose:
289
308
 
290
- rails generate friendly_id
291
- rake db:migrate
309
+ rails generate friendly_id
310
+ rake db:migrate
292
311
 
293
- This will add a table named +friendly_id_slugs+, used by the {FriendlyId::Slug}
312
+ This will add a table named `friendly_id_slugs`, used by the {FriendlyId::Slug}
294
313
  model.
295
314
 
296
- === Considerations
297
-
298
- This module is incompatible with the +:scoped+ module.
315
+ ### Considerations
299
316
 
300
317
  Because recording slug history requires creating additional database records,
301
- this module has an impact on the performance of the associated model's +create+
318
+ this module has an impact on the performance of the associated model's `create`
302
319
  method.
303
320
 
304
- === Example
321
+ ### Example
305
322
 
306
323
  class Post < ActiveRecord::Base
307
324
  extend FriendlyId
@@ -327,13 +344,13 @@ method.
327
344
  end
328
345
 
329
346
 
330
- == Unique Slugs by Scope
347
+ ## Unique Slugs by Scope
331
348
 
332
349
  The {FriendlyId::Scoped} module allows FriendlyId to generate unique slugs
333
350
  within a scope.
334
351
 
335
352
  This allows, for example, two restaurants in different cities to have the slug
336
- +joes-diner+:
353
+ `joes-diner`:
337
354
 
338
355
  class Restaurant < ActiveRecord::Base
339
356
  extend FriendlyId
@@ -347,16 +364,16 @@ This allows, for example, two restaurants in different cities to have the slug
347
364
  friendly_id :name, :use => :slugged
348
365
  end
349
366
 
350
- City.find("seattle").restaurants.find("joes-diner")
351
- City.find("chicago").restaurants.find("joes-diner")
367
+ City.friendly.find("seattle").restaurants.friendly.find("joes-diner")
368
+ City.friendly.find("chicago").restaurants.friendly.find("joes-diner")
352
369
 
353
370
  Without :scoped in this case, one of the restaurants would have the slug
354
- +joes-diner+ and the other would have +joes-diner-f9f3789a-daec-4156-af1d-fab81aa16ee5+.
371
+ `joes-diner` and the other would have `joes-diner-f9f3789a-daec-4156-af1d-fab81aa16ee5`.
355
372
 
356
- The value for the +:scope+ option can be the name of a +belongs_to+ relation, or
373
+ The value for the `:scope` option can be the name of a `belongs_to` relation, or
357
374
  a column.
358
375
 
359
- Additionally, the +:scope+ option can receive an array of scope values:
376
+ Additionally, the `:scope` option can receive an array of scope values:
360
377
 
361
378
  class Cuisine < ActiveRecord::Base
362
379
  extend FriendlyId
@@ -378,30 +395,30 @@ Additionally, the +:scope+ option can receive an array of scope values:
378
395
 
379
396
  All supplied values will be used to determine scope.
380
397
 
381
- === Finding Records by Friendly ID
398
+ ### Finding Records by Friendly ID
382
399
 
383
400
  If you are using scopes your friendly ids may not be unique, so a simple find
384
401
  like
385
402
 
386
- Restaurant.find("joes-diner")
403
+ Restaurant.friendly.find("joes-diner")
387
404
 
388
405
  may return the wrong record. In these cases it's best to query through the
389
406
  relation:
390
407
 
391
- @city.restaurants.find("joes-diner")
408
+ @city.restaurants.friendly.find("joes-diner")
392
409
 
393
410
  Alternatively, you could pass the scope value as a query parameter:
394
411
 
395
- Restaurant.find("joes-diner").where(:city_id => @city.id)
412
+ Restaurant.friendly.find("joes-diner").where(:city_id => @city.id)
396
413
 
397
414
 
398
- === Finding All Records That Match a Scoped ID
415
+ ### Finding All Records That Match a Scoped ID
399
416
 
400
417
  Query the slug column directly:
401
418
 
402
- Restaurant.find_all_by_slug("joes-diner")
419
+ Restaurant.where(:slug => "joes-diner")
403
420
 
404
- === Routes for Scoped Models
421
+ ### Routes for Scoped Models
405
422
 
406
423
  Recall that FriendlyId is a database-centric library, and does not set up any
407
424
  routes for scoped models. You must do this yourself in your application. Here's
@@ -416,15 +433,15 @@ an example of one way to set this up:
416
433
  <%= link_to 'Show', [@city, @restaurant] %>
417
434
 
418
435
  # in controllers
419
- @city = City.find(params[:city_id])
420
- @restaurant = @city.restaurants.find(params[:id])
436
+ @city = City.friendly.find(params[:city_id])
437
+ @restaurant = @city.restaurants.friendly.find(params[:id])
421
438
 
422
439
  # URLs:
423
440
  http://example.org/cities/seattle/restaurants/joes-diner
424
441
  http://example.org/cities/chicago/restaurants/joes-diner
425
442
 
426
443
 
427
- == Translating Slugs Using Simple I18n
444
+ ## Translating Slugs Using Simple I18n
428
445
 
429
446
  The {FriendlyId::SimpleI18n SimpleI18n} module adds very basic i18n support to
430
447
  FriendlyId.
@@ -432,64 +449,64 @@ FriendlyId.
432
449
  In order to use this module, your model must have a slug column for each locale.
433
450
  By default FriendlyId looks for columns named, for example, "slug_en",
434
451
  "slug_es", etc. The first part of the name can be configured by passing the
435
- +:slug_column+ option if you choose. Note that the column for the default locale
452
+ `:slug_column` option if you choose. Note that the column for the default locale
436
453
  must also include the locale in its name.
437
454
 
438
455
  This module is most suitable to applications that need to support few locales.
439
456
  If you need to support two or more locales, you may wish to use the
440
457
  friendly_id_globalize gem instead.
441
458
 
442
- === Example migration
459
+ ### Example migration
443
460
 
444
- def self.up
445
- create_table :posts do |t|
446
- t.string :title
447
- t.string :slug_en
448
- t.string :slug_es
449
- t.text :body
461
+ def self.up
462
+ create_table :posts do |t|
463
+ t.string :title
464
+ t.string :slug_en
465
+ t.string :slug_es
466
+ t.text :body
467
+ end
468
+ add_index :posts, :slug_en
469
+ add_index :posts, :slug_es
450
470
  end
451
- add_index :posts, :slug_en
452
- add_index :posts, :slug_es
453
- end
454
471
 
455
- === Finds
472
+ ### Finds
456
473
 
457
474
  Finds will take into consideration the current locale:
458
475
 
459
- I18n.locale = :es
460
- Post.find("la-guerra-de-las-galaxas")
461
- I18n.locale = :en
462
- Post.find("star-wars")
476
+ I18n.locale = :es
477
+ Post.find("la-guerra-de-las-galaxas")
478
+ I18n.locale = :en
479
+ Post.find("star-wars")
463
480
 
464
481
  To find a slug by an explicit locale, perform the find inside a block
465
- passed to I18n's +with_locale+ method:
482
+ passed to I18n's `with_locale` method:
466
483
 
467
- I18n.with_locale(:es) do
468
- Post.find("la-guerra-de-las-galaxas")
469
- end
484
+ I18n.with_locale(:es) do
485
+ Post.find("la-guerra-de-las-galaxas")
486
+ end
470
487
 
471
- === Creating Records
488
+ ### Creating Records
472
489
 
473
490
  When new records are created, the slug is generated for the current locale only.
474
491
 
475
- === Translating Slugs
492
+ ### Translating Slugs
476
493
 
477
494
  To translate an existing record's friendly_id, use
478
495
  {FriendlyId::SimpleI18n::Model#set_friendly_id}. This will ensure that the slug
479
496
  you add is properly escaped, transliterated and sequenced:
480
497
 
481
- post = Post.create :name => "Star Wars"
482
- post.set_friendly_id("La guerra de las galaxas", :es)
498
+ post = Post.create :name => "Star Wars"
499
+ post.set_friendly_id("La guerra de las galaxas", :es)
483
500
 
484
501
  If you don't pass in a locale argument, FriendlyId::SimpleI18n will just use the
485
502
  current locale:
486
503
 
487
- I18n.with_locale(:es) do
488
- post.set_friendly_id("La guerra de las galaxas")
489
- end
504
+ I18n.with_locale(:es) do
505
+ post.set_friendly_id("La guerra de las galaxas")
506
+ end
490
507
 
491
508
 
492
- == Reserved Words
509
+ ## Reserved Words
493
510
 
494
511
  The {FriendlyId::Reserved Reserved} module adds the ability to exlude a list of
495
512
  words from use as FriendlyId slugs.
@@ -504,7 +521,7 @@ FriendlyId.defaults}:
504
521
  config.reserved_words = %w(new edit nueva nuevo editar)
505
522
  end
506
523
 
507
- Note that the error message will appear on the field +:friendly_id+. If you are
524
+ Note that the error message will appear on the field `:friendly_id`. If you are
508
525
  using Rails's scaffolded form errors display, then it will have no field to
509
526
  highlight. If you'd like to change this so that scaffolding works as expected,
510
527
  one way to accomplish this is to move the error message to a different field.