From aa3b4a69fbe1bed107312195bf1a4345489dd770 Mon Sep 17 00:00:00 2001 From: Adrian Mugnolo Date: Wed, 13 Nov 2013 16:21:20 -0200 Subject: [PATCH] Set slug to nil on dup Resolves #482 --- lib/friendly_id/base.rb | 5 +++++ test/shared.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/friendly_id/base.rb b/lib/friendly_id/base.rb index b0b70561a..5e10762f3 100644 --- a/lib/friendly_id/base.rb +++ b/lib/friendly_id/base.rb @@ -246,5 +246,10 @@ def to_param friendly_id.presence.to_param || super end end + + # Clears slug on duplicate records when calling `dup`. + def dup + super.tap { |duplicate| duplicate.slug = nil } + end end end diff --git a/test/shared.rb b/test/shared.rb index 7c0e7c7a9..fce22ac44 100644 --- a/test/shared.rb +++ b/test/shared.rb @@ -51,6 +51,13 @@ module Slugged end end + test "should set the slug to nil on dup" do + with_instance_of model_class do |record| + record2 = record.dup + assert_nil record2.slug + end + end + test "when validations block save, to_param should return friendly_id rather than nil" do my_model_class = Class.new(model_class) self.class.const_set("Foo", my_model_class)