Skip to content

Commit

Permalink
Scoped: generate new slug if scope changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Shnurov authored and parndt committed Aug 16, 2018
1 parent 9cb316a commit fd2d379
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/friendly_id/scoped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def slug_generator
end
private :slug_generator

def should_generate_new_friendly_id?
(changed & friendly_id_config.scope_columns).any? || super
end

# This module adds the `:scope` configuration option to
# {FriendlyId::Configuration FriendlyId::Configuration}.
module Configuration
Expand Down
13 changes: 13 additions & 0 deletions test/scoped_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ def model_class
end
end

test "should generate new slug when scope changes" do
transaction do
novelist = Novelist.create! :name => "a"
publisher = Publisher.create! :name => "b"
novel1 = Novel.create! :name => "c", :novelist => novelist, :publisher => publisher
novel2 = Novel.create! :name => "c", :novelist => novelist, :publisher => Publisher.create(:name => "d")
assert_equal novel1.friendly_id, novel2.friendly_id
novel2.publisher = publisher
novel2.save!
assert novel2.friendly_id != novel1.friendly_id
end
end

end

0 comments on commit fd2d379

Please sign in to comment.