deleted_at 0.6.0.pre.2 → 0.6.0.pre.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/lib/deleted_at.rb +1 -1
- data/lib/deleted_at/active_record.rb +40 -17
- data/lib/deleted_at/core.rb +11 -18
- data/lib/deleted_at/railtie.rb +3 -0
- data/lib/deleted_at/relation.rb +0 -45
- data/lib/deleted_at/table.rb +23 -0
- data/lib/deleted_at/version.rb +1 -1
- metadata +22 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 661d2ca255bbab054db044f56237cc8fa6119dd86777f133d55952ad7e9951cf
|
4
|
+
data.tar.gz: e58379a9130a4e9b1bbd30672b659469171a254eddffd4f4b2734329c759cd0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a488a453bef0c8fbc14694f88458a5ddb8fb24c7384ee528a16a31c2e72d6d7dd3e61596444391e26bfba802245cb75be83a9dc8d08cb28922ab3f35376d53f
|
7
|
+
data.tar.gz: 3949f4a143aedd3f0158888b8b07d53d011777750c7d2c672c65102a7880926b17c59fca055a1bd4e53f3ef8fdd8dc366d3ba391727fb2daa9c8425243f51ad3
|
data/LICENSE
CHANGED
data/lib/deleted_at.rb
CHANGED
@@ -1,50 +1,73 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'deleted_at/relation'
|
3
|
-
|
4
1
|
module DeletedAt
|
5
2
|
module ActiveRecord
|
6
3
|
|
7
4
|
def self.prepended(subclass)
|
8
|
-
subclass.init_deleted_at_relations
|
9
5
|
subclass.extend(ClassMethods)
|
6
|
+
|
7
|
+
subclass.class_eval do
|
8
|
+
init_deleted_at_relations
|
9
|
+
default_frame { where(deleted_at[:column] => nil) }
|
10
|
+
frame :all, -> {}
|
11
|
+
frame :deleted, -> { where.not(deleted_at[:column] => nil) }
|
12
|
+
end
|
10
13
|
end
|
11
14
|
|
12
15
|
def initialize(*args)
|
13
|
-
super
|
14
|
-
|
16
|
+
super.tap do
|
17
|
+
@destroyed = deleted_at_nil?
|
18
|
+
end
|
15
19
|
end
|
16
20
|
|
17
21
|
def destroy
|
18
|
-
|
19
|
-
|
22
|
+
run_callbacks(:destroy) do
|
23
|
+
soft_delete
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
def delete
|
23
28
|
soft_delete
|
24
|
-
|
29
|
+
end
|
30
|
+
|
31
|
+
def destroy!
|
32
|
+
run_callbacks(:destroy) do
|
33
|
+
soft_delete
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete!
|
38
|
+
soft_delete
|
25
39
|
end
|
26
40
|
|
27
41
|
private
|
28
42
|
|
29
43
|
def soft_delete
|
44
|
+
return if destroyed?
|
30
45
|
update_columns(self.class.deleted_at_attributes)
|
31
46
|
@destroyed = true
|
47
|
+
freeze
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def deleted_at_nil?
|
52
|
+
!read_attribute(self.class.deleted_at[:column]).nil?
|
32
53
|
end
|
33
54
|
|
34
55
|
module ClassMethods
|
35
56
|
|
36
57
|
def inherited(subclass)
|
37
58
|
super
|
38
|
-
subclass.init_deleted_at_relations
|
59
|
+
# subclass.init_deleted_at_relations if deleted_at[:inherit]
|
60
|
+
end
|
61
|
+
|
62
|
+
def deleted_at_attributes
|
63
|
+
attributes = {
|
64
|
+
deleted_at[:column] => deleted_at[:proc].call
|
65
|
+
}
|
39
66
|
end
|
40
67
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
44
|
-
all.tap do |_query|
|
45
|
-
_query.deleted_at_scope = const
|
46
|
-
end
|
47
|
-
else super
|
68
|
+
def init_deleted_at_relations
|
69
|
+
instance_variable_get(:@relation_delegate_cache).each do |base, klass|
|
70
|
+
klass.send(:prepend, DeletedAt::Relation)
|
48
71
|
end
|
49
72
|
end
|
50
73
|
|
data/lib/deleted_at/core.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'deleted_at/active_record'
|
2
|
-
|
3
2
|
module DeletedAt
|
4
3
|
|
5
4
|
module Core
|
@@ -15,39 +14,33 @@ module DeletedAt
|
|
15
14
|
|
16
15
|
def self.raise_missing(klass)
|
17
16
|
message = "Missing `#{klass.deleted_at[:column]}` in `#{klass.name}` when trying to employ `deleted_at`"
|
18
|
-
raise(DeletedAt::
|
17
|
+
raise(DeletedAt::MissingColumnError, message)
|
19
18
|
end
|
20
19
|
|
21
20
|
def self.has_deleted_at_column?(klass)
|
22
21
|
klass.columns.map(&:name).include?(klass.deleted_at.dig(:column).to_s)
|
23
22
|
end
|
24
23
|
|
24
|
+
def self.deleted_at_ready?(klass)
|
25
|
+
!::DeletedAt.disabled? &&
|
26
|
+
klass != ::ActiveRecord::Base &&
|
27
|
+
!klass.abstract_class? &&
|
28
|
+
klass.connected? &&
|
29
|
+
klass.table_exists? &&
|
30
|
+
!(klass < DeletedAt::ActiveRecord)
|
31
|
+
end
|
32
|
+
|
25
33
|
module ClassMethods
|
26
34
|
|
27
35
|
def with_deleted_at(options={}, &block)
|
28
36
|
self.deleted_at = DeletedAt::DEFAULT_OPTIONS.merge(options)
|
29
37
|
self.deleted_at[:proc] = block if block_given?
|
30
38
|
|
31
|
-
return
|
32
|
-
|
39
|
+
return unless Core.deleted_at_ready?(self)
|
33
40
|
DeletedAt::Core.raise_missing(self) unless Core.has_deleted_at_column?(self)
|
34
41
|
|
35
42
|
self.prepend(DeletedAt::ActiveRecord)
|
36
|
-
|
37
43
|
end
|
38
|
-
|
39
|
-
def deleted_at_attributes
|
40
|
-
attributes = {
|
41
|
-
deleted_at[:column] => deleted_at[:proc].call
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
|
-
def init_deleted_at_relations
|
46
|
-
instance_variable_get(:@relation_delegate_cache).each do |base, klass|
|
47
|
-
klass.send(:prepend, DeletedAt::Relation)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
44
|
end # End ClassMethods
|
52
45
|
|
53
46
|
end
|
data/lib/deleted_at/railtie.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'rails/railtie'
|
2
|
+
require 'active_record/framing'
|
3
|
+
require 'deleted_at/relation'
|
2
4
|
require 'deleted_at/core'
|
3
5
|
require 'deleted_at/table_definition'
|
4
6
|
|
@@ -8,6 +10,7 @@ module DeletedAt
|
|
8
10
|
ActiveSupport.on_load(:active_record) do
|
9
11
|
::ActiveRecord::Base.prepend(DeletedAt::Core)
|
10
12
|
::ActiveRecord::ConnectionAdapters::TableDefinition.prepend(DeletedAt::TableDefinition)
|
13
|
+
# ::ActiveRecord::Associations::Association.prepend(DeletedAt::Association)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
data/lib/deleted_at/relation.rb
CHANGED
@@ -1,50 +1,5 @@
|
|
1
1
|
module DeletedAt
|
2
2
|
module Relation
|
3
|
-
|
4
|
-
def self.prepended(subclass)
|
5
|
-
subclass.class_eval do
|
6
|
-
attr_writer :deleted_at_scope
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def deleted_at_scope
|
11
|
-
@deleted_at_scope ||= :Present
|
12
|
-
end
|
13
|
-
|
14
|
-
def deleted_at_select
|
15
|
-
scoped_arel = case deleted_at_scope
|
16
|
-
when :Deleted
|
17
|
-
vanilla.dup.where(table[klass.deleted_at[:column]].not_eq(nil))
|
18
|
-
when :Present
|
19
|
-
vanilla.dup.where(table[klass.deleted_at[:column]].eq(nil))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def vanilla
|
24
|
-
# @vanilla ||= klass.const_get(:All).unscope(:where).freeze
|
25
|
-
@vanilla ||= klass.unscoped.tap do |rel|
|
26
|
-
rel.deleted_at_scope = :All
|
27
|
-
end.freeze
|
28
|
-
end
|
29
|
-
|
30
|
-
# Rails 4.x
|
31
|
-
def from_value
|
32
|
-
if (subselect = deleted_at_select)
|
33
|
-
[subselect, ::ActiveRecord::Base.connection.quote_table_name(table_name)]
|
34
|
-
else
|
35
|
-
super
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Rails 5.x
|
40
|
-
def from_clause
|
41
|
-
if (subselect = deleted_at_select)
|
42
|
-
::ActiveRecord::Relation::FromClause.new(subselect, ::ActiveRecord::Base.connection.quote_table_name(table_name))
|
43
|
-
else
|
44
|
-
super
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
3
|
def delete_all(*args)
|
49
4
|
if args.pop
|
50
5
|
ActiveSupport::Deprecation.warn(<<~STR)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DeletedAt
|
2
|
+
class Table < Arel::Table
|
3
|
+
|
4
|
+
attr_accessor :shadow
|
5
|
+
|
6
|
+
def initialize(table_name, engine, shadow:)
|
7
|
+
super(table_name).tap do |table|
|
8
|
+
@engine = engine
|
9
|
+
@shadow = shadow
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
case Thread.current[:selecting_deleted_at]
|
15
|
+
when nil, false
|
16
|
+
super
|
17
|
+
else
|
18
|
+
super + shadow.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/deleted_at/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deleted_at
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.0.pre.
|
4
|
+
version: 0.6.0.pre.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dale Stevens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: active_record-framing
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.1.0.pre.4
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.1.0.pre.4
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: pg
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,16 +76,16 @@ dependencies:
|
|
62
76
|
name: bundler
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
64
78
|
requirements:
|
65
|
-
- - "
|
79
|
+
- - ">="
|
66
80
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
81
|
+
version: '0'
|
68
82
|
type: :development
|
69
83
|
prerelease: false
|
70
84
|
version_requirements: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
72
|
-
- - "
|
86
|
+
- - ">="
|
73
87
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
88
|
+
version: '0'
|
75
89
|
- !ruby/object:Gem::Dependency
|
76
90
|
name: rake
|
77
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +130,7 @@ files:
|
|
116
130
|
- lib/deleted_at/legacy.rb
|
117
131
|
- lib/deleted_at/railtie.rb
|
118
132
|
- lib/deleted_at/relation.rb
|
133
|
+
- lib/deleted_at/table.rb
|
119
134
|
- lib/deleted_at/table_definition.rb
|
120
135
|
- lib/deleted_at/version.rb
|
121
136
|
homepage: https://github.com/TwilightCoders/deleted_at
|
@@ -140,8 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
155
|
- !ruby/object:Gem::Version
|
141
156
|
version: 1.3.1
|
142
157
|
requirements: []
|
143
|
-
|
144
|
-
rubygems_version: 2.7.6
|
158
|
+
rubygems_version: 3.0.3
|
145
159
|
signing_key:
|
146
160
|
specification_version: 4
|
147
161
|
summary: Soft delete your data, but keep it clean.
|