Commit 2ad50e1 Victor Kmita
committed
1 parent 8069fc9 commit 2ad50e1 Copy full SHA for 2ad50e1
File tree 4 files changed +46
-6
lines changed
4 files changed +46
-6
lines changed Original file line number Diff line number Diff line change @@ -187,9 +187,15 @@ module Base
187
187
# allows an alternate configuration syntax, and conditional configuration
188
188
# logic.
189
189
#
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
+ #
190
195
# @yieldparam config The model class's {FriendlyId::Configuration friendly_id_config}.
191
196
def friendly_id ( base = nil , options = { } , &block )
192
197
yield friendly_id_config if block_given?
198
+ friendly_id_config . dependent = options . delete :dependent
193
199
friendly_id_config . use options . delete :use
194
200
friendly_id_config . send :set , base ? options . merge ( :base => base ) : options
195
201
include Model
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ class Configuration
18
18
# The module to use for finders
19
19
attr_accessor :finder_methods
20
20
21
+ # The value used for the slugged association's dependent option
22
+ attr_accessor :dependent
23
+
21
24
def initialize ( model_class , values = nil )
22
25
@model_class = model_class
23
26
@defaults = { }
Original file line number Diff line number Diff line change @@ -54,9 +54,16 @@ def find_post
54
54
=end
55
55
module History
56
56
57
+ module Configuration
58
+ def dependent_value
59
+ dependent . nil? ? :destroy : dependent
60
+ end
61
+ end
62
+
57
63
def self . setup ( model_class )
58
64
model_class . instance_eval do
59
65
friendly_id_config . use :slugged
66
+ friendly_id_config . class . send :include , History ::Configuration
60
67
friendly_id_config . finder_methods = FriendlyId ::History ::FinderMethods
61
68
if friendly_id_config . uses? :finders
62
69
relation . class . send ( :include , friendly_id_config . finder_methods )
@@ -72,7 +79,7 @@ def self.included(model_class)
72
79
model_class . class_eval do
73
80
has_many :slugs , -> { order ( "#{ Slug . quoted_table_name } .id DESC" ) } , {
74
81
:as => :sluggable ,
75
- :dependent => :destroy ,
82
+ :dependent => @friendly_id_config . dependent_value ,
76
83
:class_name => Slug . to_s
77
84
}
78
85
Original file line number Diff line number Diff line change 1
1
require "helper"
2
2
3
- class Manual < ActiveRecord ::Base
4
- extend FriendlyId
5
- friendly_id :name , :use => [ :slugged , :history ]
6
- end
7
-
8
3
class HistoryTest < TestCaseClass
9
4
10
5
include FriendlyId ::Test
11
6
include FriendlyId ::Test ::Shared ::Core
12
7
8
+ class Manual < ActiveRecord ::Base
9
+ extend FriendlyId
10
+ friendly_id :name , :use => [ :slugged , :history ]
11
+ end
12
+
13
13
def model_class
14
14
Manual
15
15
end
@@ -172,6 +172,30 @@ def model_class
172
172
end
173
173
end
174
174
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
+
175
199
class HistoryTestWithSti < HistoryTest
176
200
class Journalist < ActiveRecord ::Base
177
201
extend FriendlyId
You can’t perform that action at this time.
0 commit comments