dcidev_approval 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +93 -1
- data/lib/dcidev_approval.rb +6 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e97350e7fc396ea5b6badf639f46c97c256de119290e7f4cf209a29b55c13fc2
|
4
|
+
data.tar.gz: 32378050bd803f53adf02652f4d5502b871e2ea6ce7aed380970a395d1562761
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59b9cf658199b5dd33f76c3eb22f6d302718a908f220d986a46d9fe90a28f82b490fbc31993a36193f555cadcd5bed77ef91f73c8276a513af210db61aa7f53b
|
7
|
+
data.tar.gz: f800249db88dacaafadfaa400946f602753c558288e6f0671c25c780de21b3fd4660d4e4954472ec8d1264fde3b3214d9c24dee755284224d2cbb146979d85de
|
data/README.md
CHANGED
@@ -1 +1,93 @@
|
|
1
|
-
|
1
|
+
# Setup
|
2
|
+
##### 1. Add new column named `status` to every model requiring this gem
|
3
|
+
`db/migrate/migration_file.rb`
|
4
|
+
```ruby
|
5
|
+
class AddStatusToProduk < ActiveRecord::Migration[6.0]
|
6
|
+
def change
|
7
|
+
add_column :produk, :status, :string
|
8
|
+
add_column :produk, :change_status, :string
|
9
|
+
end
|
10
|
+
end
|
11
|
+
```
|
12
|
+
`app/models/model.rb`
|
13
|
+
```ruby
|
14
|
+
STATUS = %w[waiting approved rejected].freeze
|
15
|
+
enum status: STATUS.zip(STATUS).to_h, _prefix: true
|
16
|
+
|
17
|
+
CHANGE_STATUS = %w(pending_delete pending_update).freeze
|
18
|
+
enum change_status: CHANGE_STATUS.zip(CHANGE_STATUS).to_h, _prefix: true
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
##### 2. Format your `Agent` and `Role` with one-many relationship.
|
24
|
+
##### 3. Declare instance method `is_admin?` to `Agent` to find out whether an agent is an admin or not
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
def is_admin?
|
28
|
+
# add your custom logic here
|
29
|
+
self.approved? && ["admin", "super_admin", "checker"].include?(self.try(:roles).try(:first).try(:code))
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
##### 4. Include the module to every model requiring approval. Or just put it in `ApplicationRecord`
|
34
|
+
`app/models/application_record.rb`
|
35
|
+
```ruby
|
36
|
+
class ApplicationRecord < ActiveRecord::Base
|
37
|
+
include DcidevApproval
|
38
|
+
self.abstract_class = true
|
39
|
+
# ...
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
# Features
|
44
|
+
* Create: `Model.create_data(declared(params), current_user, bypass)`
|
45
|
+
* Update: `model.edit_data(declared(params), current_user, bypass)`
|
46
|
+
* Delete: `model.delete_data(declared(params), current_user, bypass)`
|
47
|
+
* Approval: `model.approval(declared(params))`
|
48
|
+
* Compare current database value and argument to check if there are any update: `model.changes_present?(params)`
|
49
|
+
* Check approval status: `model.waiting_approval?`, `model.pending_insert?`, `model.pending_update?`, `model.pending_delete?`
|
50
|
+
* Find last lodifier & timestamp: `model.last_modified_by`
|
51
|
+
* Find author: `model.created_by`
|
52
|
+
* Find approval agent & timestamp: `model.last_approved_by`
|
53
|
+
|
54
|
+
Explanation
|
55
|
+
* `declared(params)`: is a hash value from Grape Parameters, plain ruby hash can also be used
|
56
|
+
* `current_user`: the agent responsible for the changes
|
57
|
+
* `bypass`: boolean value to toogle the approval system. If not sent, the default value is `true`
|
58
|
+
|
59
|
+
To track changes peformed to a record, call
|
60
|
+
# Callbacks
|
61
|
+
To execute code before/after the CRUD, include module `DcidevApproval` in `ApplicationRecord` and peform overide and or overload on it's child model.
|
62
|
+
|
63
|
+
`app/models/application_record.rb`
|
64
|
+
```ruby
|
65
|
+
class ApplicationRecord < ActiveRecord::Base
|
66
|
+
include DcidevApproval
|
67
|
+
self.abstract_class = true
|
68
|
+
# ...
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
`app/models/child_model.rb`
|
73
|
+
```ruby
|
74
|
+
class ChildModel < ApplicationRecord
|
75
|
+
# ...
|
76
|
+
def self.create_data(params, agent, request)
|
77
|
+
super(params, agent, false) do |data|
|
78
|
+
# do something after the record is successfully created
|
79
|
+
# in this case, write an activity log
|
80
|
+
# the data variable will return the created record
|
81
|
+
ActivityLog.write("#{agent.is_admin? || params.bypass ? nil : "Request "} Add #{self.class.to_s}", request, agent, menu, data) if params.log
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def edit_data(params, agent, request)
|
86
|
+
super(params, agent, false) do |_|
|
87
|
+
# do something after the record is successfully edited and require approval
|
88
|
+
end
|
89
|
+
end
|
90
|
+
# ...
|
91
|
+
end
|
92
|
+
|
93
|
+
```
|
data/lib/dcidev_approval.rb
CHANGED
@@ -37,7 +37,7 @@ module DcidevApproval
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def approved?
|
40
|
-
self.status == "approved"
|
40
|
+
self.status == "approved" || self.change_status.nil?
|
41
41
|
end
|
42
42
|
|
43
43
|
def rejected?
|
@@ -56,7 +56,7 @@ module DcidevApproval
|
|
56
56
|
log = self.activity_logs.where("activity LIKE '%edit%'").limit(1).order(created_at: :desc).try(:first)
|
57
57
|
end
|
58
58
|
{
|
59
|
-
modified_by: log.present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent).try(:username).to_s} | #{log.try(:agent).try(:roles).try(:first).try(:name)})" :
|
59
|
+
modified_by: log.present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent).try(:username).to_s} | #{log.try(:agent).try(:roles).try(:first).try(:name)})" : nil,
|
60
60
|
modified_at: log.present? ? log.try(:created_at) || self.try(:updated_at) || self.try(:created_at) : nil
|
61
61
|
}
|
62
62
|
end
|
@@ -121,7 +121,7 @@ module DcidevApproval
|
|
121
121
|
# ActivityLog.write("#{agent.is_admin? ? nil : "Request "}Edit #{self.class.to_s}", request, agent, menu, self) if params.log
|
122
122
|
end
|
123
123
|
end
|
124
|
-
yield
|
124
|
+
yield self
|
125
125
|
end
|
126
126
|
|
127
127
|
def approval(params)
|
@@ -130,7 +130,7 @@ module DcidevApproval
|
|
130
130
|
elsif params.status == "rejected"
|
131
131
|
self.delete_changes
|
132
132
|
end
|
133
|
-
yield
|
133
|
+
yield self
|
134
134
|
end
|
135
135
|
|
136
136
|
def delete_data(agent, bypass = true)
|
@@ -155,15 +155,15 @@ module DcidevApproval
|
|
155
155
|
data = params.merge!({ status: :approved })
|
156
156
|
d = self.new_from_params(data)
|
157
157
|
raise d.errors.full_messages.join(", ") unless d.save
|
158
|
+
yield d
|
158
159
|
# ActivityLog.write("#{agent.is_admin? ? nil : "Request "} Add #{self.to_s}", request, agent, menu, d) if params.log
|
159
160
|
end
|
160
161
|
else
|
161
162
|
d = self.new_from_params(params)
|
162
163
|
d.status = agent.is_admin? ? :approved : :waiting
|
163
164
|
raise d.errors.full_messages.join(", ") unless d.save
|
164
|
-
|
165
|
+
yield d
|
165
166
|
end
|
166
|
-
yield d
|
167
167
|
end
|
168
168
|
end
|
169
169
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dcidev_approval
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Punto Damar P
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dcidev_active_record
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
|
-
rubygems_version: 3.
|
54
|
+
rubygems_version: 3.1.2
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Logic for implementing record changes approval
|