on_destroy 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -5
- data/lib/on_destroy/config.rb +4 -7
- data/lib/on_destroy/model.rb +22 -29
- data/lib/on_destroy/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -31,23 +31,22 @@ Something similar to light version of rails3_acts_as_paranoid could be produced
|
|
31
31
|
default_scope where("deleted_at IS NOT NULL")
|
32
32
|
on_destroy :do_not_delete, set: :deleted_at, to: lambda {Time.now}
|
33
33
|
|
34
|
-
|
34
|
+
If you have the same deleted column and process for all models, instead of using on_destroy in every model, use configure:
|
35
35
|
|
36
36
|
# Do it for everything
|
37
37
|
OnDestroy.configure do
|
38
38
|
self.do_not_delete = true
|
39
|
-
self.
|
40
|
-
self.to = true
|
39
|
+
self.on_destroy_options = [:do_not_delete, {set: :deleted_at, to: lambda {Time.now}]
|
41
40
|
end
|
42
41
|
|
43
42
|
If you want control over how it intuits how something is deleted (which is to look at the deleted date) use `:is_deleted_if` which can be a value, nil, or a Proc that takes the attribute value:
|
44
43
|
|
45
44
|
on_destroy :do_not_delete, set: :some_column, to: lambda {Time.now}, is_deleted_if: {|c|Time.now > c} # provide time-travel resistent behavior of destroyed?/deleted?
|
46
|
-
on_destroy :do_not_delete, set: :some_column, to: 1 is_deleted_if: 1 # this is not necessary, as it will already check for 1 since you specified it in the :to
|
45
|
+
on_destroy :do_not_delete, set: :some_column, to: 1, is_deleted_if: 1 # this is not necessary, as it will already check for 1 since you specified it in the :to
|
47
46
|
|
48
47
|
If you use a proc to set the value and a value of nil indicates that it should be deleted (which is kind of wierd), you'll need to set:
|
49
48
|
|
50
|
-
on_destroy :do_not_delete, set: :some_column, to: lambda {nil} is_deleted_if: nil
|
49
|
+
on_destroy :do_not_delete, set: :some_column, to: lambda {nil}, is_deleted_if: nil
|
51
50
|
|
52
51
|
To call a normal-ish `destroy` method, use the `really_destroy` method. I think this is better than using `destroy!` because all of the other bang methods on ActiveRecord.base tend to just throw errors, not behave that differently, despite the `destroy!` method being a naming convention from acts_as_paranoid.
|
53
52
|
|
data/lib/on_destroy/config.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
module OnDestroy
|
2
2
|
OPTIONS = [
|
3
3
|
:do_not_delete,
|
4
|
-
:
|
5
|
-
:to,
|
6
|
-
:is_deleted_if
|
4
|
+
:on_destroy_options,
|
7
5
|
]
|
8
6
|
|
9
7
|
class << self
|
@@ -12,9 +10,8 @@ module OnDestroy
|
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
15
|
-
#
|
13
|
+
# defaults
|
16
14
|
#OnDestroy.configure do
|
17
|
-
# self.do_not_delete =
|
18
|
-
# self.
|
19
|
-
# self.to = true
|
15
|
+
# self.do_not_delete = false
|
16
|
+
# self.on_destroy_options = nil
|
20
17
|
#end
|
data/lib/on_destroy/model.rb
CHANGED
@@ -3,8 +3,6 @@ module OnDestroy
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
class_attribute :defined_is_deleted_if
|
7
|
-
# use values from config
|
8
6
|
OnDestroy::OPTIONS.each do |key|
|
9
7
|
class_attribute key, instance_writer: true
|
10
8
|
self.send("#{key}=".to_sym, OnDestroy.send(key))
|
@@ -18,20 +16,8 @@ module OnDestroy
|
|
18
16
|
# and to default self.is_deleted_if to a proc that
|
19
17
|
def on_destroy(*args)
|
20
18
|
options = args.extract_options!
|
21
|
-
self.do_not_delete =
|
22
|
-
self.
|
23
|
-
self.to = options[:to] if options[:to]
|
24
|
-
if options[:is_deleted_if]
|
25
|
-
# set defined_is_deleted_if to allow explicit set to nil
|
26
|
-
self.defined_is_deleted_if = true
|
27
|
-
self.is_deleted_if = options[:is_deleted_if]
|
28
|
-
elsif (options[:to].is_a?(Proc))
|
29
|
-
# if :to is a Proc, assume that a nil value means it is not deleted and if not-nil is deleted.
|
30
|
-
# that is just a guess, based on the mark with deleted_at date example.
|
31
|
-
self.is_deleted_if = Proc.new {|v| v != nil}
|
32
|
-
else
|
33
|
-
self.is_deleted_if = Proc.new {|v| v == options[:to]}
|
34
|
-
end
|
19
|
+
self.do_not_delete = args.include?(:do_not_delete)
|
20
|
+
self.on_destroy_options = options
|
35
21
|
end
|
36
22
|
end
|
37
23
|
|
@@ -39,11 +25,14 @@ module OnDestroy
|
|
39
25
|
|
40
26
|
# if self.set then will use update_attributes! to set the self.set attribute to self.to or self.to.call if it is a Proc.
|
41
27
|
def do_on_destroy
|
42
|
-
if self.
|
43
|
-
|
44
|
-
|
28
|
+
if self.on_destroy_options
|
29
|
+
o_set = self.on_destroy_options[:set]
|
30
|
+
o_to = self.on_destroy_options[:to]
|
31
|
+
if o_set
|
32
|
+
update_attributes! o_set => (o_to.is_a?(Proc) ? o_to.call : o_to)
|
33
|
+
end
|
34
|
+
yield
|
45
35
|
end
|
46
|
-
yield
|
47
36
|
end
|
48
37
|
|
49
38
|
# if self.do_not_delete? runs no/empty callback on :destroy, otherwise calls super.
|
@@ -62,17 +51,21 @@ module OnDestroy
|
|
62
51
|
run_callbacks(:destroy) {delete}
|
63
52
|
end
|
64
53
|
|
65
|
-
# if self.is_deleted_if is a Proc compares self.is_deleted_if.call to send(self.to).
|
66
|
-
# if self.is_deleted_if is not nil compares self.is_deleted_if.call to send(self.to).
|
67
|
-
# If self.is_deleted_if not a Proc, calls super.
|
68
54
|
def destroyed?
|
69
|
-
if self.
|
70
|
-
self.is_deleted_if
|
71
|
-
|
72
|
-
|
73
|
-
|
55
|
+
if self.on_destroy_options
|
56
|
+
is_deleted_if = self.on_destroy_options[:is_deleted_if]
|
57
|
+
o_set = self.on_destroy_options[:set]
|
58
|
+
o_to = self.on_destroy_options[:to]
|
59
|
+
if is_deleted_if.is_a?(Proc)
|
60
|
+
send(o_set) == is_deleted_if.call
|
61
|
+
elsif o_to.is_a?(Proc)
|
62
|
+
# assume that a :to defined as a Proc is going to evaluate to a non-nil to indicate the model is null
|
63
|
+
send(o_set) != nil
|
64
|
+
else
|
65
|
+
send(o_set) == o_to
|
66
|
+
end
|
74
67
|
else
|
75
|
-
|
68
|
+
super
|
76
69
|
end
|
77
70
|
end
|
78
71
|
|
data/lib/on_destroy/version.rb
CHANGED