Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slug regeneration behaviour #334

Merged
merged 2 commits into from Apr 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/friendly_id/base.rb
Expand Up @@ -189,7 +189,6 @@ def friendly_id(base = nil, options = {}, &block)
yield friendly_id_config if block_given?
friendly_id_config.use options.delete :use
friendly_id_config.send :set, base ? options.merge(:base => base) : options
before_save {|rec| rec.instance_eval {@current_friendly_id = friendly_id}}
include Model
end

Expand Down Expand Up @@ -255,8 +254,6 @@ def relation_class
# Instance methods that will be added to all classes using FriendlyId.
module Model

attr_reader :current_friendly_id

# Convenience method for accessing the class method of the same name.
def friendly_id_config
self.class.friendly_id_config
Expand Down
7 changes: 3 additions & 4 deletions lib/friendly_id/slugged.rb
Expand Up @@ -261,10 +261,9 @@ 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 (with and without sequence) is different from either the current
# friendly id or the slug value, then we'll generate a new friendly_id.
compare = (current_friendly_id || slug_value)
slug_base != compare && slug_base != compare.try(:sub, /#{separator}[\d]*\z/, '')
# If the slug base is different from the current slug value (with or
# without sequence number) then we'll generate a new friendly_id.
!(slug_base == slug_value || slug_base == slug_value.try(:sub, /#{separator}[\d]*\z/, ''))
end

# Sets the slug.
Expand Down
2 changes: 0 additions & 2 deletions test/shared.rb
Expand Up @@ -45,8 +45,6 @@ module Slugged
with_instance_of model_class do |record|
record.slug = nil
record.save!
assert_nil record.slug
record.save!
refute_nil record.slug
end
end
Expand Down
16 changes: 15 additions & 1 deletion test/slugged_test.rb
Expand Up @@ -160,6 +160,20 @@ def model_class
end
end

test "should detect when a stored slug does not match the current friendly_id" do
with_instance_of model_class do |record|
record.slug = "something-else"
assert record.should_generate_new_friendly_id?
end
end

test "should detect when a stored slug has been cleared" do
with_instance_of model_class do |record|
record.slug = nil
assert record.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"
Expand All @@ -184,7 +198,7 @@ def self.name
friendly_id :name, :use => :slugged, :sequence_separator => '-'
end
transaction do
record1 = model_class.create! :name => "Peugeuot 206"
record1 = model_class.create! :name => "Peugeot 206"
assert !record1.should_generate_new_friendly_id?
record1.save!
assert !record1.should_generate_new_friendly_id?
Expand Down