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 regenerating slug when regeneration condition overriden (fixes #521) #541

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/friendly_id/scoped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ def serialized_scope

def scope_for_slug_generator
relation = self.class.unscoped.friendly
primary_key_name = self.class.primary_key

friendly_id_config.scope_columns.each do |column|
relation = relation.where(column => send(column))
end
if changed.include?(friendly_id_config.slug_column)
primary_key_name = self.class.primary_key
relation = relation.where.not(primary_key_name => send(primary_key_name))
end
relation = relation.where.not(primary_key_name => send(primary_key_name))

relation
end
private :scope_for_slug_generator
Expand Down
7 changes: 3 additions & 4 deletions lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,12 @@ def set_slug(normalized_slug = nil)
private :set_slug

def scope_for_slug_generator
primary_key_name = self.class.primary_key

scope = self.class.base_class.unscoped
scope = scope.friendly unless scope.respond_to?(:exists_by_friendly_id?)
scope = scope.where.not(primary_key_name => send(primary_key_name))

if changed.include?(friendly_id_config.slug_column)
primary_key_name = self.class.primary_key
scope = scope.where.not(primary_key_name => send(primary_key_name))
end
scope
end
private :scope_for_slug_generator
Expand Down
11 changes: 10 additions & 1 deletion test/scoped_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Novel < ActiveRecord::Base
friendly_id :name, :use => :scoped, :scope => [:publisher, :novelist]

def should_generate_new_friendly_id?
new_record? || super
name_changed? || new_record? || super
end
end

Expand Down Expand Up @@ -112,4 +112,13 @@ def model_class
assert_equal old_id, record.friendly_id
end
end

test 'should allow a record to reuse its own slug when other attr changed' do
with_instance_of(model_class) do |record|
old_id = record.friendly_id
record.name = "A-B-C"
record.save!
assert_equal old_id, record.friendly_id
end
end
end
13 changes: 13 additions & 0 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
class Journalist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :slugged

def should_generate_new_friendly_id?
name_changed? || super
end
end

class Article < ActiveRecord::Base
Expand Down Expand Up @@ -77,6 +81,15 @@ def self.name
assert_equal old_id, record.friendly_id
end
end

test 'should allow record to reuse slug when other attr changed' do
with_instance_of(model_class) do |record|
old_id = record.friendly_id
record.name = "A-B-C"
record.save!
assert_equal old_id, record.friendly_id
end
end
end

class SlugGeneratorTest < MiniTest::Unit::TestCase
Expand Down