Skip to content

Commit

Permalink
Allow records to reuse their own slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
norman committed Aug 21, 2013
1 parent 8efb0bf commit 70a6d5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ def set_slug(normalized_slug = nil)
private :set_slug

def slug_generator
friendly_id_config.slug_generator_class.new(self.class.base_class.unscoped.friendly)
scope = self.class.base_class.unscoped.friendly
if changed.include?(friendly_id_config.slug_column)
column = self.class.quoted_table_name + '.' + self.class.quoted_primary_key
scope = scope.where("#{column} <> ?", send(self.class.primary_key))
end
friendly_id_config.slug_generator_class.new(scope)
end
private :slug_generator

Expand Down
9 changes: 9 additions & 0 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def self.name
refute instance2.valid?
end
end

test 'should allow a record to reuse its own slug' do
with_instance_of(model_class) do |record|
old_id = record.friendly_id
record.slug = nil
record.save!
assert_equal old_id, record.friendly_id
end
end
end

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

0 comments on commit 70a6d5a

Please sign in to comment.