soft_deleter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +3 -0
- data/lib/generators/soft_deleter_generator.rb +19 -0
- data/lib/soft_deleter/railtie.rb +4 -0
- data/lib/soft_deleter/version.rb +3 -0
- data/lib/soft_deleter.rb +100 -0
- data/lib/tasks/soft_deleter_tasks.rake +4 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b07926d2e933d2130b86b24432f696a7cb55426accf3c921e111707abeba93f2
|
4
|
+
data.tar.gz: c220f8ad356001fcc61ad1d4bfc372302b02fd5c253d7772c7d9692c096e3b7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e534f6c8e9c0cf861b779a6bea0060ca792b2ba9dd37bf4558c805ecfc7039895ddf3684c3892d91edb826a2af4e6294bc1cda8134ed420af369d6f399c1c5ec
|
7
|
+
data.tar.gz: 1239740720c61a1c008dd9dff21c5f4f8d3590bea20c86d2a4d50f909c6c151012bfdfc3e4016bcc10bbcd120adc7ce576d7e7ebd7307d9efec2e0ab2279f3bf
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# SoftDeleter
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "soft_deleter"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install soft_deleter
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class SoftDeleterGenerator < Rails::Generators::NamedBase
|
2
|
+
argument :attributes, type: :array, default: []
|
3
|
+
|
4
|
+
def out
|
5
|
+
snake_name = name.underscore
|
6
|
+
add_attributes = attributes.map do |attr|
|
7
|
+
arg = "#{attr.name}:#{attr.type}"
|
8
|
+
arg += ":index" if attr.has_index?
|
9
|
+
arg += ":uniq" if attr.has_uniq_index?
|
10
|
+
arg += "{#{attr.attr_options[:limit]}}" if attr.attr_options.present?
|
11
|
+
arg
|
12
|
+
end.join(" ")
|
13
|
+
|
14
|
+
system("bundle exec rails g model #{name} #{add_attributes} deleter_type:string deleter_id:integer deleted_at:timestamp")
|
15
|
+
system("sed -e '2i \\ include SoftDeleter' app/models/#{snake_name}.rb > app/models/_tmp.rb")
|
16
|
+
system("rm app/models/#{snake_name}.rb")
|
17
|
+
system("mv app/models/_tmp.rb app/models/#{snake_name}.rb")
|
18
|
+
end
|
19
|
+
end
|
data/lib/soft_deleter.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require "soft_deleter/version"
|
2
|
+
require "soft_deleter/railtie"
|
3
|
+
|
4
|
+
module SoftDeleter
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
scope :enabled, -> { where(deleted_at: nil) }
|
9
|
+
scope :deleted, -> { where.not(deleted_at: nil) }
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def included_tables
|
14
|
+
result = %i[has_many has_one].map { |a| self.reflect_on_all_associations(a) }.flatten.filter_map do |i|
|
15
|
+
next unless %i[destroy delete delete_all].include?(i.options[:dependent]) && i.options.keys.exclude?(:through)
|
16
|
+
|
17
|
+
i.options[:class_name]&.underscore&.pluralize&.to_sym || i.name
|
18
|
+
end.compact
|
19
|
+
result
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def soft_delete!(deleter = self)
|
24
|
+
ActiveRecord::Base.transaction do
|
25
|
+
with_associations(:soft_delete_witout_associations!, deleter)
|
26
|
+
end
|
27
|
+
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def soft_delete(deleter = self)
|
32
|
+
ActiveRecord::Base.transaction do
|
33
|
+
with_associations(:soft_delete_witout_associations, deleter)
|
34
|
+
end
|
35
|
+
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def restore
|
40
|
+
ActiveRecord::Base.transaction do
|
41
|
+
with_associations(:restore_without_associations)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def deleter_type
|
46
|
+
self[:deleter_type].constantize if self.soft_deleted?
|
47
|
+
end
|
48
|
+
|
49
|
+
def soft_deleted?
|
50
|
+
self.deleted_at.present?
|
51
|
+
end
|
52
|
+
|
53
|
+
def alive?
|
54
|
+
self.deleted_at.nil?
|
55
|
+
end
|
56
|
+
|
57
|
+
def deleter
|
58
|
+
self.deleter_type&.find_by(id: self.deleter_id)
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
def with_associations(callback, deleter = nil)
|
64
|
+
child_tables = self.class.included_tables
|
65
|
+
childs = child_tables.map do |child_table|
|
66
|
+
self.send(child_table)
|
67
|
+
end.flatten
|
68
|
+
|
69
|
+
childs.compact.each { |child| child.with_associations(callback, deleter) }
|
70
|
+
|
71
|
+
send(callback, deleter) if deleter.present?
|
72
|
+
send(callback) if deleter.blank?
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def soft_delete_witout_associations!(deleter)
|
78
|
+
update!(
|
79
|
+
deleter_type: deleter.class.to_s,
|
80
|
+
deleter_id: deleter.id,
|
81
|
+
deleted_at: Time.zone.now
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
def soft_delete_witout_associations(deleter)
|
86
|
+
update(
|
87
|
+
deleter_type: deleter.class.to_s,
|
88
|
+
deleter_id: deleter.id,
|
89
|
+
deleted_at: Time.zone.now
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
def restore_without_associations
|
94
|
+
update(
|
95
|
+
deleter_type: nil,
|
96
|
+
deleter_id: nil,
|
97
|
+
deleted_at: nil
|
98
|
+
)
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: soft_deleter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- testCodeV01
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 7.0.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: SoftDeleter.
|
42
|
+
email:
|
43
|
+
- qft-tk-1213@tk-labo.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/generators/soft_deleter_generator.rb
|
52
|
+
- lib/soft_deleter.rb
|
53
|
+
- lib/soft_deleter/railtie.rb
|
54
|
+
- lib/soft_deleter/version.rb
|
55
|
+
- lib/tasks/soft_deleter_tasks.rake
|
56
|
+
homepage: https://github.com/testCodeV01
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata:
|
60
|
+
allowed_push_host: https://rubygems.org/
|
61
|
+
homepage_uri: https://github.com/testCodeV01
|
62
|
+
source_code_uri: https://github.com/testCodeV01/soft_deleter
|
63
|
+
changelog_uri: https://github.com/testCodeV01
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubygems_version: 3.3.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: SoftDeleter.
|
83
|
+
test_files: []
|