Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for names ending in numbers and using single dash

  • Loading branch information...
commit dee89bf821f735c5200c8a02c975cd076d56ae0b 1 parent 9cccc3f
Tomás Arribas raymccoy authored
Showing with 36 additions and 20 deletions.
  1. +3 −2 lib/friendly_id/slugged.rb
  2. +33 −18 test/slugged_test.rb
5 lib/friendly_id/slugged.rb
View
@@ -261,9 +261,10 @@ def should_generate_new_friendly_id?
return true if new_record?
slug_base = normalize_friendly_id(base)
separator = Regexp.escape friendly_id_config.sequence_separator
- # If the slug base (without sequence) is different from either the current
+ # If the slug base (with and without sequence) is different from either the current
# friendly id or the slug value, then we'll generate a new friendly_id.
- slug_base != (current_friendly_id || slug_value).try(:sub, /#{separator}[\d]*\z/, '')
+ slug_base != (current_friendly_id || slug_value) &&
+ slug_base != (current_friendly_id || slug_value).try(:sub, /#{separator}[\d]*\z/, '')
end
# Sets the slug.
51 test/slugged_test.rb
View
@@ -129,24 +129,6 @@ def model_class
end
end
- test "should correctly sequence slugs that uses single dashes as sequence separator" do
- model_class = Class.new(ActiveRecord::Base) do
- self.table_name = "journalists"
- extend FriendlyId
- friendly_id :name, :use => :slugged, :sequence_separator => '-'
- validates_uniqueness_of :slug_en
- def self.name
- "Journalist"
- end
- end
- transaction do
- record1 = model_class.create! :name => "Peugeuot 206"
- assert_equal "peugeuot-206", record1.slug
- record2 = model_class.create! :name => "Peugeuot 206"
- assert_equal "peugeuot-206-2", record2.slug
- end
- end
-
end
class SlugSeparatorTest < MiniTest::Unit::TestCase
@@ -177,6 +159,39 @@ def model_class
assert record2.should_generate_new_friendly_id?
end
end
+
+ test "should correctly sequence slugs that uses single dashes as sequence separator" do
+ model_class = Class.new(ActiveRecord::Base) do
+ self.table_name = "journalists"
+ extend FriendlyId
+ friendly_id :name, :use => :slugged, :sequence_separator => '-'
+ def self.name
+ "Journalist"
+ end
+ end
+ transaction do
+ record1 = model_class.create! :name => "Peugeuot 206"
+ assert_equal "peugeuot-206", record1.slug
+ record2 = model_class.create! :name => "Peugeuot 206"
+ assert_equal "peugeuot-206-2", record2.slug
+ end
+ end
+
+ test "should detect when a sequenced slug has changed when name ends in number and using single dash" do
+ model_class = Class.new(ActiveRecord::Base) do
+ self.table_name = "journalists"
+ extend FriendlyId
+ friendly_id :name, :use => :slugged, :sequence_separator => '-'
+ end
+ transaction do
+ record1 = model_class.create! :name => "Peugeuot 206"
+ assert !record1.should_generate_new_friendly_id?
+ record1.save!
+ assert !record1.should_generate_new_friendly_id?
+ record1.name = "Peugeot 307"
+ assert record1.should_generate_new_friendly_id?
+ end
+ end
end
class DefaultScopeTest < MiniTest::Unit::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.