friendly_id 4.0.6 → 4.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -6,6 +6,14 @@ suggestions, ideas and improvements to FriendlyId.
6
6
  * Table of Contents
7
7
  {:toc}
8
8
 
9
+ ## 4.0.7 (2012-06-06)
10
+
11
+ * to_param just calls super when no friendly_id is present, to keep the model's
12
+ default behavior. (Andrew White)
13
+
14
+ * FriendlyId can now properly sequence slugs that end in numbers even when a
15
+ single dash is used as the separator (Tomás Arribas).
16
+
9
17
  ## 4.0.6 (2012-05-21)
10
18
 
11
19
  * Fix nil return value from to_param when save fails because of validation errors (Tomás Arribas)
data/lib/friendly_id.rb CHANGED
@@ -45,7 +45,7 @@ with numeric ids:
45
45
  module FriendlyId
46
46
 
47
47
  # The current version.
48
- VERSION = "4.0.6"
48
+ VERSION = "4.0.7"
49
49
 
50
50
  @mutex = Mutex.new
51
51
 
@@ -268,7 +268,7 @@ often better and easier to use {FriendlyId::Slugged slugs}.
268
268
  if diff = changes[friendly_id_config.query_field]
269
269
  diff.first || diff.second
270
270
  else
271
- friendly_id.present? ? friendly_id : id.to_s
271
+ friendly_id.presence || super
272
272
  end
273
273
  end
274
274
  end
@@ -31,8 +31,7 @@ module FriendlyId
31
31
  end
32
32
 
33
33
  def extract_sequence_from_slug(slug)
34
- # Don't assume that the separator is unique in the slug.
35
- slug.gsub(/^#{Regexp.quote(normalized)}(#{Regexp.quote(separator)})?/, '').to_i
34
+ slug.split("#{normalized}#{separator}").last.to_i
36
35
  end
37
36
 
38
37
  def column
@@ -261,9 +261,10 @@ issue}[https://github.com/norman/friendly_id/issues/180] for discussion.
261
261
  return true if new_record?
262
262
  slug_base = normalize_friendly_id(base)
263
263
  separator = Regexp.escape friendly_id_config.sequence_separator
264
- # If the slug base (without sequence) is different from either the current
264
+ # If the slug base (with and without sequence) is different from either the current
265
265
  # friendly id or the slug value, then we'll generate a new friendly_id.
266
- slug_base != (current_friendly_id || slug_value).try(:sub, /#{separator}[\d]*\z/, '')
266
+ compare = (current_friendly_id || slug_value)
267
+ slug_base != compare && slug_base != compare.try(:sub, /#{separator}[\d]*\z/, '')
267
268
  end
268
269
 
269
270
  # Sets the slug.
data/test/shared.rb CHANGED
@@ -145,6 +145,10 @@ module FriendlyId
145
145
  assert_equal record.id.to_s, record.to_param
146
146
  end
147
147
  end
148
+
149
+ test "should return nil for to_param with a new record" do
150
+ assert_equal nil, model_class.new.to_param
151
+ end
148
152
  end
149
153
  end
150
154
  end
data/test/slugged_test.rb CHANGED
@@ -159,6 +159,39 @@ class SlugSeparatorTest < MiniTest::Unit::TestCase
159
159
  assert record2.should_generate_new_friendly_id?
160
160
  end
161
161
  end
162
+
163
+ test "should correctly sequence slugs that uses single dashes as sequence separator" do
164
+ model_class = Class.new(ActiveRecord::Base) do
165
+ self.table_name = "journalists"
166
+ extend FriendlyId
167
+ friendly_id :name, :use => :slugged, :sequence_separator => '-'
168
+ def self.name
169
+ "Journalist"
170
+ end
171
+ end
172
+ transaction do
173
+ record1 = model_class.create! :name => "Peugeuot 206"
174
+ assert_equal "peugeuot-206", record1.slug
175
+ record2 = model_class.create! :name => "Peugeuot 206"
176
+ assert_equal "peugeuot-206-2", record2.slug
177
+ end
178
+ end
179
+
180
+ test "should detect when a sequenced slug has changed when name ends in number and using single dash" do
181
+ model_class = Class.new(ActiveRecord::Base) do
182
+ self.table_name = "journalists"
183
+ extend FriendlyId
184
+ friendly_id :name, :use => :slugged, :sequence_separator => '-'
185
+ end
186
+ transaction do
187
+ record1 = model_class.create! :name => "Peugeuot 206"
188
+ assert !record1.should_generate_new_friendly_id?
189
+ record1.save!
190
+ assert !record1.should_generate_new_friendly_id?
191
+ record1.name = "Peugeot 307"
192
+ assert record1.should_generate_new_friendly_id?
193
+ end
194
+ end
162
195
  end
163
196
 
164
197
  class DefaultScopeTest < MiniTest::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.6
4
+ version: 4.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-21 00:00:00.000000000 Z
12
+ date: 2012-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties