dcidev_approval 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dcidev_approval.rb +143 -163
- 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: acf499a815402aa955b457d3fa2db0a97a6018135c1c8814ecad4c4894eeaf62
|
4
|
+
data.tar.gz: 12c8dffb9dea52b6d335e0a38194344f780dbf9e7d13259d4c92892d92dbdc60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cd2869a8e8709375a4b82fde759ab37a7e43a41f1ceedbdf1f563e68a6ac58976c950b8b1088ca8bd7c50ee81e42a8d744bd207d5a5819d23d967fa6be8f242
|
7
|
+
data.tar.gz: 844432fe9092b9918d238ce1ff730dd4028993c087665f26c8361518f49d7c5a189362a7a987c2fc3a92fcb81ecd94cadcd651d3311649fa62e4a79357718e80
|
data/lib/dcidev_approval.rb
CHANGED
@@ -1,170 +1,150 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module DcidevApproval
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
delete_changes
|
130
|
-
end
|
131
|
-
yield self
|
132
|
-
end
|
133
|
-
|
134
|
-
def delete_data(agent, bypass = true)
|
135
|
-
raise 'data still waiting for approval' if waiting_approval?
|
136
|
-
|
137
|
-
if bypass || agent.is_admin?
|
138
|
-
ActiveRecord::Base.transaction do
|
139
|
-
# ActivityLog.write("Delete #{self.class.to_s}", request, agent, menu, self) if params.log
|
140
|
-
raise errors.full_messages.join(', ') unless destroy
|
141
|
-
end
|
142
|
-
else
|
143
|
-
raise errors.full_messages.join(', ') unless update(change_status: :pending_delete)
|
144
|
-
# ActivityLog.write("Request Delete #{self.class.to_s}", request, agent, menu, self) if params.log
|
145
|
-
end
|
146
|
-
yield true
|
4
|
+
def self.included(base)
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
def changes_present?(changes)
|
11
|
+
present = false
|
12
|
+
changes.each do |k, v|
|
13
|
+
if eval("self.#{k}") != v
|
14
|
+
present = true
|
15
|
+
break
|
16
|
+
end
|
17
|
+
rescue StandardError => _e
|
18
|
+
end
|
19
|
+
present
|
20
|
+
end
|
21
|
+
|
22
|
+
def waiting_approval?
|
23
|
+
%w[pending_update pending_delete].include?(change_status) || status == 'waiting'
|
24
|
+
end
|
25
|
+
|
26
|
+
def pending_insert?
|
27
|
+
change_status.nil? && %w[waiting rejected].include?(status)
|
28
|
+
end
|
29
|
+
|
30
|
+
def pending_update?
|
31
|
+
change_status == 'pending_update'
|
32
|
+
end
|
33
|
+
|
34
|
+
def pending_delete?
|
35
|
+
change_status == 'pending_delete'
|
36
|
+
end
|
37
|
+
|
38
|
+
def approved?
|
39
|
+
status == 'approved' || change_status.nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
def rejected?
|
43
|
+
status == 'rejected'
|
44
|
+
end
|
45
|
+
|
46
|
+
def waiting?
|
47
|
+
status == 'waiting'
|
48
|
+
end
|
49
|
+
|
50
|
+
def last_modified_by
|
51
|
+
log = audit_trails.where(activity_type: %w[update delete]).limit(1).try(:first)
|
52
|
+
{
|
53
|
+
modified_by: log.present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent).try(:roles).try(:first).try(:name)})" : nil,
|
54
|
+
modified_at: log.present? ? log.try(:created_at) || try(:updated_at) || try(:created_at) : nil
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def created_by
|
59
|
+
log = audit_trails(:asc).where(activity_type: :create).limit(1).try(:first)
|
60
|
+
{
|
61
|
+
created_by: log.present? && log.try(:agent).try(:name).present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent_role).try(:name)})" : 'System',
|
62
|
+
created_at: self.try(:created_at) || log.try(:created_at)
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def last_approved_by
|
67
|
+
last_approve = audit_trails.where(activity_type: :approve).limit(1).try(:first)
|
68
|
+
last_entry = audit_trails.limit(1).order(created_at: :desc).try(:first)
|
69
|
+
{
|
70
|
+
approved_by: last_approve.try(:id) == last_entry.try(:id) ? last_approve.try(:agent).try(:name).to_s + " (#{last_approve.try(:agent_role).try(:name)})" : nil,
|
71
|
+
approved_at: last_approve.try(:id) == last_entry.try(:id) ? last_approve.try(:created_at) : nil
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def approve_changes
|
76
|
+
if change_status.nil? && %w[waiting rejected].include?(status) && !update(status: :approved, data_changes: nil, change_status: nil)
|
77
|
+
raise errors.full_messages.join(', ')
|
78
|
+
end
|
79
|
+
|
80
|
+
case change_status
|
81
|
+
when 'pending_update'
|
82
|
+
raise errors.full_messages.join(', ') unless update_by_params(data_changes, false)
|
83
|
+
raise errors.full_messages.join(', ') unless update(status: :approved, data_changes: nil, change_status: nil)
|
84
|
+
when 'pending_delete'
|
85
|
+
destroy
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def delete_changes
|
90
|
+
raise errors.full_messages.join(', ') unless update(data_changes: nil, change_status: nil, status: status == 'waiting' ? :rejected : :approved)
|
91
|
+
end
|
92
|
+
|
93
|
+
def edit_data(params, agent, bypass = true)
|
94
|
+
raise 'data still waiting for approval' if waiting_approval?
|
95
|
+
|
96
|
+
if bypass
|
97
|
+
raise errors.full_messages.join(', ') unless update_by_params(params, false)
|
98
|
+
elsif changes_present?(params)
|
99
|
+
ActiveRecord::Base.transaction do
|
100
|
+
data = agent.is_admin? || status == 'waiting' ? params : { change_status: :pending_update, data_changes: agent.is_admin? ? nil : params }
|
101
|
+
raise errors.full_messages.join(', ') unless update_by_params(data, false)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
yield self
|
105
|
+
end
|
106
|
+
|
107
|
+
def approval(params)
|
108
|
+
case params.status
|
109
|
+
when 'approved'
|
110
|
+
approve_changes
|
111
|
+
when 'rejected'
|
112
|
+
delete_changes
|
113
|
+
end
|
114
|
+
yield self
|
115
|
+
end
|
116
|
+
|
117
|
+
def delete_data(agent, bypass = true)
|
118
|
+
raise 'data still waiting for approval' if waiting_approval?
|
119
|
+
|
120
|
+
if bypass || agent.is_admin?
|
121
|
+
ActiveRecord::Base.transaction do
|
122
|
+
raise errors.full_messages.join(', ') unless destroy
|
123
|
+
end
|
124
|
+
else
|
125
|
+
raise errors.full_messages.join(', ') unless update(change_status: :pending_delete)
|
126
|
+
end
|
127
|
+
yield true
|
128
|
+
end
|
147
129
|
end
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
130
|
+
|
131
|
+
module ClassMethods
|
132
|
+
def create_data(params, agent, bypass = true)
|
133
|
+
if bypass
|
134
|
+
ActiveRecord::Base.transaction do
|
135
|
+
data = params.merge!({ status: :approved })
|
136
|
+
d = new_from_params(data)
|
137
|
+
raise d.errors.full_messages.join(', ') unless d.save
|
138
|
+
|
139
|
+
yield d
|
140
|
+
end
|
141
|
+
else
|
142
|
+
d = new_from_params(params)
|
143
|
+
d.status = agent.is_admin? ? :approved : :waiting
|
144
|
+
raise d.errors.full_messages.join(', ') unless d.save
|
145
|
+
|
146
|
+
yield d
|
147
|
+
end
|
160
148
|
end
|
161
|
-
else
|
162
|
-
d = new_from_params(params)
|
163
|
-
d.status = agent.is_admin? ? :approved : :waiting
|
164
|
-
raise d.errors.full_messages.join(', ') unless d.save
|
165
|
-
|
166
|
-
yield d
|
167
|
-
end
|
168
149
|
end
|
169
|
-
end
|
170
150
|
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.12
|
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-06-
|
11
|
+
date: 2022-06-14 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.1
|
54
|
+
rubygems_version: 3.0.3.1
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Logic for implementing record changes approval
|