soft_deleter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +97 -5
  3. data/lib/soft_deleter/version.rb +1 -1
  4. metadata +2 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b07926d2e933d2130b86b24432f696a7cb55426accf3c921e111707abeba93f2
4
- data.tar.gz: c220f8ad356001fcc61ad1d4bfc372302b02fd5c253d7772c7d9692c096e3b7c
3
+ metadata.gz: 128f9591b3e2fa2005aa7bb7722c83291a341af111d2f208bdba0bd5bcd050dd
4
+ data.tar.gz: 6e2cd7dea9ebfdee761cbad7a615920da78a378394ed51c9a8f67b48571af08b
5
5
  SHA512:
6
- metadata.gz: e534f6c8e9c0cf861b779a6bea0060ca792b2ba9dd37bf4558c805ecfc7039895ddf3684c3892d91edb826a2af4e6294bc1cda8134ed420af369d6f399c1c5ec
7
- data.tar.gz: 1239740720c61a1c008dd9dff21c5f4f8d3590bea20c86d2a4d50f909c6c151012bfdfc3e4016bcc10bbcd120adc7ce576d7e7ebd7307d9efec2e0ab2279f3bf
6
+ metadata.gz: a99e22f07eda44d726c803aa9f0a2c95f831a1f2af52788663c32b4eb577eadda9fec044a822f03c161e8533fee514e6de18faa9de7b411d9f866d92bce4c757
7
+ data.tar.gz: f4b7c19000066067279b95a79e85895a78a1db457f769a5801e6adb3f5c4b213f5a479d791ab17c91dc6ab548c50b2c5341cfd14fbff6883caaf901d428e8159
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # SoftDeleter
2
- Short description and motivation.
3
-
4
- ## Usage
5
- How to use my plugin.
2
+ soft delete Rails plugin.
6
3
 
7
4
  ## Installation
8
5
  Add this line to your application's Gemfile:
@@ -17,10 +14,105 @@ $ bundle
17
14
  ```
18
15
 
19
16
  Or install it yourself as:
20
- ```bash
17
+ ```
21
18
  $ gem install soft_deleter
22
19
  ```
23
20
 
21
+ ## Usage
22
+ ### Introduce
23
+ Soft delete model User.
24
+ ```
25
+ bundle exec rails g soft_deleter user
26
+ ```
27
+ It creates migration file to create user with attributes which is needed to introduce soft delete.<br/>
28
+ Add some attributes to migration file, like name, email, age, ...etc.<br />
29
+ And excute `bundle exec rails db:migrate`. That's all.<br />
30
+ <br />
31
+ Or if you already have User model, and you want introduce soft delete in it,
32
+ create migration file and add lines like bellow
33
+ ```ruby
34
+ class AddSoftDeleterAttributesToUsers < ActiveRecord::Migration[7.0]
35
+ def change
36
+ add_column :users, :deleter_type, :string
37
+ add_column :users, :deleter_id, :integer
38
+ add_column :users, :deleted_at, :timestamp
39
+ end
40
+ end
41
+ ```
42
+ and excute `bundle exec rails db:migrate`<br />
43
+ Here, `deleter_type` and `deleter_id`, these are the infomations who soft delete.<br />
44
+ Like `current_admin`, when admin which is "Admin" class does soft delete user record, admin's class name and id can be recorded.
45
+ <br /><br />
46
+ And add line to model
47
+ ```ruby
48
+ class User < ApplicationRecord
49
+ include SoftDeleter
50
+ end
51
+ ```
52
+ This line is added automatically if you use `rails g soft_deleter user` command to make user model.
53
+
54
+ ### scope
55
+ When you load users whitout soft deleted records, you need to scope like bellow.
56
+ ```ruby
57
+ users = User.enabled.all
58
+ ```
59
+ If you don't use enabled scope, you will load users in all records including soft deleted.<br />
60
+ Otherwise, if you need to load records with soft deleted, excute like bellow.
61
+ ```ruby
62
+ deleted_users = User.deleted.all
63
+ ```
64
+
65
+ ### Soft delete
66
+ ```ruby
67
+ user = User.enabled.first
68
+ user.soft_delete # soft delete
69
+ user.soft_delete! # soft delete or raise if fail to soft delete
70
+ user.restore # restore soft deleted user
71
+ ```
72
+ If your app have some models other than user, like `Admin` model,<br />
73
+ and you need to record to that Admin user did soft delete.<br />
74
+ Then,
75
+ ```ruby
76
+ user = User.enabled.first
77
+
78
+ admin = Admin.enabled.first # soft deleted by admin user
79
+ user.soft_delete(admin) # soft delete and set admin to deleter
80
+ user.soft_delete!(admin) # raise if fail to soft delete
81
+
82
+ user.deleter # => <Admin:0x00007f37f96a0c88
83
+ user.deleter_type # => Admin(id: integer, ...
84
+ user.deleter_id # => "admin.id" if deleter is not set, "user.id"
85
+ user.soft_deleted? # => true
86
+ user.alive? # => false
87
+ ```
88
+
89
+ ## Associations
90
+ If associations some models, User, Book, Section.
91
+ ```ruby
92
+ # User model
93
+ class User < ApplicationRecord
94
+ include SoftDeleter
95
+ has_many :books, dependent: :destroy
96
+ end
97
+
98
+ # Book model
99
+ class Book < ApplicationRecord
100
+ include SoftDeleter
101
+ belongs_to :user
102
+ has_many :sections, dependent: :destroy
103
+ end
104
+
105
+ # Section model
106
+ class Section < ApplicationRecord
107
+ include SoftDeleter
108
+ belongs_to :book
109
+ end
110
+ ```
111
+ So, if you excute `user.soft_delete`, then associations books, and sections are soft deleted.<br />
112
+ And excute `user.restore`, then associations books, and sections are restored.<br />
113
+ It works with dependent destroy descriptions. If not, it doesn't work.
114
+
115
+
24
116
  ## Contributing
25
117
  Contribution directions go here.
26
118
 
@@ -1,3 +1,3 @@
1
1
  module SoftDeleter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_deleter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - testCodeV01
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-15 00:00:00.000000000 Z
11
+ date: 2023-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
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
27
  description: SoftDeleter.
42
28
  email:
43
29
  - qft-tk-1213@tk-labo.com