From fd2d379d03ed926c995324fd6fefb89f2f19be9d Mon Sep 17 00:00:00 2001 From: Kirill Shnurov Date: Fri, 10 Aug 2018 16:58:12 +0300 Subject: [PATCH] Scoped: generate new slug if scope changed --- lib/friendly_id/scoped.rb | 4 ++++ test/scoped_test.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/friendly_id/scoped.rb b/lib/friendly_id/scoped.rb index 22ea05a2a..d974db5c4 100644 --- a/lib/friendly_id/scoped.rb +++ b/lib/friendly_id/scoped.rb @@ -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 diff --git a/test/scoped_test.rb b/test/scoped_test.rb index 182b5ed53..8415e9682 100644 --- a/test/scoped_test.rb +++ b/test/scoped_test.rb @@ -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