rakutenusa-friendly_id 2.0.6 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 6
3
+ :patch: 7
4
4
  :major: 2
data/lib/friendly_id.rb CHANGED
@@ -13,7 +13,8 @@ module FriendlyId
13
13
  :scope => nil,
14
14
  :strip_diacritics => false,
15
15
  :strip_non_ascii => false,
16
- :use_slug => false }.freeze
16
+ :use_slug => false,
17
+ :replace_slug_on_save => true }.freeze
17
18
 
18
19
  # Valid keys for has_friendly_id options.
19
20
  VALID_FRIENDLY_ID_KEYS = [
@@ -23,7 +24,8 @@ module FriendlyId
23
24
  :scope,
24
25
  :strip_diacritics,
25
26
  :strip_non_ascii,
26
- :use_slug ].freeze
27
+ :use_slug,
28
+ :replace_slug_on_save ].freeze
27
29
 
28
30
  # This error is raised when it's not possible to generate a unique slug.
29
31
  class SlugGenerationError < StandardError ; end
@@ -37,6 +39,7 @@ module FriendlyId
37
39
  #
38
40
  # Options:
39
41
  # * <tt>:use_slug</tt> - Defaults to false. Use slugs when you want to use a non-unique text field for friendly ids.
42
+ # * <tt>:replace_slug_on_save</tt> -
40
43
  # * <tt>:max_length</tt> - Defaults to 255. The maximum allowed length for a slug.
41
44
  # * <tt>:strip_diacritics</tt> - Defaults to false. If true, it will remove accents, umlauts, etc. from western characters.
42
45
  # * <tt>:strip_non_ascii</tt> - Defaults to false. If true, it will all non-ascii ([^a-z0-9]) characters.
@@ -38,7 +38,11 @@ module FriendlyId::SluggableInstanceMethods
38
38
  # Has the basis of our friendly id changed, requiring the generation of a
39
39
  # new slug?
40
40
  def new_slug_needed?
41
- !slug || slug_text != slug.name
41
+ !slug || ((slug_text != slug.name) && replace_slug?)
42
+ end
43
+
44
+ def replace_slug?
45
+ friendly_id_options[:replace_slug_on_save]
42
46
  end
43
47
 
44
48
  # Returns the most recent slug, which is used to determine the friendly
@@ -122,6 +122,11 @@ class SluggedModelTest < Test::Unit::TestCase
122
122
  end
123
123
  end
124
124
 
125
+ should "determine that a new slug is needed" do
126
+ @post.title = "Changed title"
127
+ assert_equal true, @post.new_slug_needed?
128
+ end
129
+
125
130
  context "and configured to strip diacritics" do
126
131
  setup do
127
132
  Post.friendly_id_options = Post.friendly_id_options.merge(:strip_diacritics => true)
@@ -260,4 +265,23 @@ class SluggedModelTest < Test::Unit::TestCase
260
265
 
261
266
  end
262
267
 
268
+ context "A slugged model with replace_slug_on_save option set to false" do
269
+
270
+ setup do
271
+ Post.friendly_id_options = FriendlyId::DEFAULT_FRIENDLY_ID_OPTIONS.merge(:column => :title, :use_slug => true, :replace_slug_on_save => false)
272
+ @post = Post.new :title => "Test post", :content => "Test content"
273
+ @post.save!
274
+ end
275
+
276
+ should "decide to not replace a slug" do
277
+ assert_equal false, @post.replace_slug?
278
+ end
279
+
280
+ should "determine that a new slug is not needed" do
281
+ @post.title = "Changed title"
282
+ assert_equal false, @post.new_slug_needed?
283
+ end
284
+
285
+ end
286
+
263
287
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rakutenusa-friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - RakutenUSA
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-29 00:00:00 -07:00
12
+ date: 2009-05-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -80,7 +80,7 @@ requirements: []
80
80
  rubyforge_project:
81
81
  rubygems_version: 1.2.0
82
82
  signing_key:
83
- specification_version: 3
83
+ specification_version: 2
84
84
  summary: TODO
85
85
  test_files:
86
86
  - test/custom_slug_normalizer_test.rb