Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 96 lines (67 sloc) 3.159 kb
df469ec @norman Doc tweaks
norman authored
1 # What's New in FriendlyId 4?
497a449 @norman Renamed About and added link to gem
norman authored
2
8777f06 @norman Initial commit to 4.0.0
norman authored
3 ## Back to basics
4
df469ec @norman Doc tweaks
norman authored
5 FriendlyId is mostly a different codebase from FriendlyId 3. However, this isn't
6 the "big rewrite," it's the "small rewrite:"
8777f06 @norman Initial commit to 4.0.0
norman authored
7
8 Adding new features with each release is not sustainable. This release *removes*
9 features, but makes it possible to add them back as addons. We can also remove
10 some complexity by relying on the better default functionality provided by newer
df469ec @norman Doc tweaks
norman authored
11 versions of Active Support and Active Record.
8777f06 @norman Initial commit to 4.0.0
norman authored
12
13 Here's what's changed:
14
df469ec @norman Doc tweaks
norman authored
15 ## New configuration and setup
16
17 FriendlyId is no longer added to Active Record by default, you must explicitly
18 add it to each model you want to use it in. The method and options have also
19 changed:
20
21 # FriendlyId 3
22 class Post < ActiveRecord::Base
23 has_friendly_id :title, :use_slugs => true
24 end
25
26 # FriendlyId 4
27 class Post < ActiveRecord::Base
28 extend FriendlyId
29 friendly_id :title, :use => :slugged
30 end
31
32 It also adds a new "defaults" method for configuring all models:
33
34 FriendlyId.defaults do |config|
35 config.use :slugged, :reserved
36 config.base = :name
37 end
38
8777f06 @norman Initial commit to 4.0.0
norman authored
39 ## Active Record 3+ only
40
7170cd1 @norman Make history module use its own finders
norman authored
41 For 2.3 support, you can use FriendlyId 3.x, which will continue to be
42 maintained until people don't want it any more.
8777f06 @norman Initial commit to 4.0.0
norman authored
43
44 ## In-table slugs
45
46 FriendlyId no longer creates a separate slugs table - it just stores the
47 generated slug value in the model table, which is simpler, faster and what most
df469ec @norman Doc tweaks
norman authored
48 want by default. Keeping slug history in a separate table is an
bd36198 @norman Fix module link
norman authored
49 {FriendlyId::History optional add-on} for FriendlyId 4.
8777f06 @norman Initial commit to 4.0.0
norman authored
50
51 ## No more multiple finds
52
53 Person.find "joe-schmoe" # Supported
54 Person.find ["joe-schmoe", "john-doe"] # No longer supported
55
56 If you want find by more than one friendly id, build your own query:
57
58 Person.where(:slug => ["joe-schmoe", "john-doe"])
59
60 This lets us do *far* less monkeypatching in Active Record. How much less?
61 FriendlyId overrides the base find with a mere 2 lines of code, and otherwise
62 changes nothing else. This means more stability and less breakage between Rails
63 updates.
64
65 ## No more finder status
66
67 FriendlyId 3 offered finder statuses to help you determine when an outdated
68 or non-friendly id was used to find the record, so that you could decide whether
69 to permanently redirect to the canonical URL. However, there's a simpler way to
70 do that, so this feature has been removed:
71
72 if request.path != person_path(@person)
73 return redirect_to @person, :status => :moved_permanently
74 end
75
76 ## Bye-bye Babosa
77
78 [Babosa](http://github.com/norman/babosa) is FriendlyId 3's slugging library.
79
80 FriendlyId 4 doesn't use it by default because the most important pieces of it
81 were already accepted into Active Support 3.
82
83 However, Babosa is still useful, for example, for idiomatically transliterating
84 Cyrillic ([or other
85 language](https://github.com/norman/babosa/tree/master/lib/babosa/transliterator))
86 strings to ASCII. It's very easy to include - just override
87 `#normalize_friendly_id` in your model:
88
89 class MyModel < ActiveRecord::Base
90 ...
91
92 def normalize_friendly_id(text)
b5b1870 @norman Fix typo
norman authored
93 text.to_slug.normalize! :transliterations => :russian
8777f06 @norman Initial commit to 4.0.0
norman authored
94 end
95 end
Something went wrong with that request. Please try again.