Skip to content

Commit 2ad50e1

Browse files
author
Victor Kmita
committed
added the ability not to delete historic slugs when the sluggable class is destroyed
When using acts_as_paranoid on a sluggable models, we aren't actually deleting the models when calling destroy. If we delete the slugs, we’re losing the history. We're alsosetting up a condition where a unique contstraint may end up being violated
1 parent 8069fc9 commit 2ad50e1

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

lib/friendly_id/base.rb

+6
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,15 @@ module Base
187187
# allows an alternate configuration syntax, and conditional configuration
188188
# logic.
189189
#
190+
# @option options [Symbol,Boolean] :dependent Available when using `:history`.
191+
# Sets the value used for the slugged association's dependent option. Use
192+
# `false` if you do not want to dependently destroy the associated slugged
193+
# record. Defaults to `:destroy`.
194+
#
190195
# @yieldparam config The model class's {FriendlyId::Configuration friendly_id_config}.
191196
def friendly_id(base = nil, options = {}, &block)
192197
yield friendly_id_config if block_given?
198+
friendly_id_config.dependent = options.delete :dependent
193199
friendly_id_config.use options.delete :use
194200
friendly_id_config.send :set, base ? options.merge(:base => base) : options
195201
include Model

lib/friendly_id/configuration.rb

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Configuration
1818
# The module to use for finders
1919
attr_accessor :finder_methods
2020

21+
# The value used for the slugged association's dependent option
22+
attr_accessor :dependent
23+
2124
def initialize(model_class, values = nil)
2225
@model_class = model_class
2326
@defaults = {}

lib/friendly_id/history.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ def find_post
5454
=end
5555
module History
5656

57+
module Configuration
58+
def dependent_value
59+
dependent.nil? ? :destroy : dependent
60+
end
61+
end
62+
5763
def self.setup(model_class)
5864
model_class.instance_eval do
5965
friendly_id_config.use :slugged
66+
friendly_id_config.class.send :include, History::Configuration
6067
friendly_id_config.finder_methods = FriendlyId::History::FinderMethods
6168
if friendly_id_config.uses? :finders
6269
relation.class.send(:include, friendly_id_config.finder_methods)
@@ -72,7 +79,7 @@ def self.included(model_class)
7279
model_class.class_eval do
7380
has_many :slugs, -> {order("#{Slug.quoted_table_name}.id DESC")}, {
7481
:as => :sluggable,
75-
:dependent => :destroy,
82+
:dependent => @friendly_id_config.dependent_value,
7683
:class_name => Slug.to_s
7784
}
7885

test/history_test.rb

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
require "helper"
22

3-
class Manual < ActiveRecord::Base
4-
extend FriendlyId
5-
friendly_id :name, :use => [:slugged, :history]
6-
end
7-
83
class HistoryTest < TestCaseClass
94

105
include FriendlyId::Test
116
include FriendlyId::Test::Shared::Core
127

8+
class Manual < ActiveRecord::Base
9+
extend FriendlyId
10+
friendly_id :name, :use => [:slugged, :history]
11+
end
12+
1313
def model_class
1414
Manual
1515
end
@@ -172,6 +172,30 @@ def model_class
172172
end
173173
end
174174

175+
class DependentDestroyTest < HistoryTest
176+
177+
include FriendlyId::Test
178+
179+
class Manual < ActiveRecord::Base
180+
extend FriendlyId
181+
friendly_id :name, :use => :history, :dependent => false
182+
end
183+
184+
def model_class
185+
Manual
186+
end
187+
188+
test "should allow disabling of dependent destroy" do
189+
transaction do
190+
assert FriendlyId::Slug.find_by_slug('test').nil?
191+
l = model_class.create! :name => 'test'
192+
assert FriendlyId::Slug.find_by_slug('test').present?
193+
l.destroy
194+
assert FriendlyId::Slug.find_by_slug('test').present?
195+
end
196+
end
197+
end
198+
175199
class HistoryTestWithSti < HistoryTest
176200
class Journalist < ActiveRecord::Base
177201
extend FriendlyId

0 commit comments

Comments
 (0)