public
Rubygem
Description: FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
Homepage: http://friendly-id.rubyforge.org
Clone URL: git://github.com/norman/friendly_id.git
Added support for reseved slugs.
metatribe (author)
Wed Oct 22 10:17:56 -0700 2008
norman (committer)
Wed Oct 22 10:17:56 -0700 2008
commit  4c9617e3256e2ed8270d2b6ad4cd14bb3ab2eb1d
tree    2245f924ccbe146e34a2055755adcf2f3a7a0fd5
parent  89601d10ba528cdbae18688affe0314dfe0e5381
...
8
9
10
11
12
 
 
13
14
15
...
20
21
22
 
23
24
25
...
228
229
230
231
232
 
233
234
235
...
8
9
10
 
 
11
12
13
14
15
...
20
21
22
23
24
25
26
...
229
230
231
 
232
233
234
235
236
0
@@ -8,8 +8,8 @@ module FriendlyId
0
   module ClassMethods
0
 
0
     # Default options for friendly_id.
0
-    DEFAULT_FRIENDLY_ID_OPTIONS = {:method => nil, :use_slug => false, :max_length => 255, :strip_diacritics => false}.freeze
0
-    VALID_FRIENDLY_ID_KEYS = [:use_slug, :max_length, :strip_diacritics].freeze
0
+    DEFAULT_FRIENDLY_ID_OPTIONS = {:method => nil, :use_slug => false, :max_length => 255, :reserved => [], :strip_diacritics => false}.freeze
0
+    VALID_FRIENDLY_ID_KEYS = [:use_slug, :max_length, :reserved, :strip_diacritics].freeze
0
 
0
     # Set up an ActiveRecord model to use a friendly_id.
0
     #
0
@@ -20,6 +20,7 @@ module FriendlyId
0
     # * <tt>:use_slug</tt> - Defaults to false. Use slugs when you want to use a non-unique text field for friendly ids.
0
     # * <tt>:max_length</tt> - Defaults to 255. The maximum allowed length for a slug.
0
     # * <tt>:strip_diacritics</tt> - Defaults to false. If true, it will remove accents, umlauts, etc. from western characters. You must have the unicode gem installed for this to work.
0
+    # * <tt>:reseved</tt> - Array of words that are reserved and can't be used as slugs. If such a word is used, it will be treated the same as if that slug was already taken (numeric extension will be appended). Defaults to [].
0
     def has_friendly_id(column, options = {})
0
       options.assert_valid_keys VALID_FRIENDLY_ID_KEYS
0
       options = DEFAULT_FRIENDLY_ID_OPTIONS.merge(options).merge(:column => column)
0
@@ -228,8 +229,8 @@ module FriendlyId
0
     # Generate the text for the friendly id, ensuring no duplication.
0
     def generate_friendly_id
0
       slug_text = truncated_friendly_id_base
0
-
0
       count = Slug.count_matches slug_text, self.class.name, :all, :conditions => "sluggable_id <> #{ id.to_i }"
0
+      count += 1 if self.class.friendly_id_options[:reserved].include?(slug_text)
0
       count == 0 ? slug_text : generate_friendly_id_with_extension(slug_text, count)
0
     end
0
 
...
1
2
 
3
...
1
 
2
3
0
@@ -1,3 +1,3 @@
0
 class Post < ActiveRecord::Base
0
-  has_friendly_id :name, :use_slug => true
0
+  has_friendly_id :name, :use_slug => true, :reserved => ['new', 'recent']
0
 end
...
175
176
177
 
 
 
 
 
 
 
 
 
 
178
179
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
0
@@ -175,4 +175,14 @@ class SluggableTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+  def test_should_not_use_reserved_slugs
0
+    post = Post.create!(:name => 'new')
0
+    assert_not_equal 'new', post.friendly_id
0
+  end
0
+  
0
+  def test_should_append_extension_to_reseved_slugs
0
+    post = Post.create!(:name => 'new')
0
+    assert_equal 'new-2', post.friendly_id
0
+  end
0
+
0
 end
0
\ No newline at end of file

Comments