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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dcidev_approval.rb +143 -163
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 047563f0a296d733db4828b485a0446cd2757bb3f915809808555d44719a5e44
4
- data.tar.gz: 2a3fb2c40952698d8ea669a33f67bb503e5a0dfb44d6c900caef2d4ef27e1231
3
+ metadata.gz: acf499a815402aa955b457d3fa2db0a97a6018135c1c8814ecad4c4894eeaf62
4
+ data.tar.gz: 12c8dffb9dea52b6d335e0a38194344f780dbf9e7d13259d4c92892d92dbdc60
5
5
  SHA512:
6
- metadata.gz: b9ef11b07ae7f35c5c21c1a1c93da4565077570d5b277786a87b913d690b9b4885703cbed14337e8ecc9c16c5c0e3f822fd6359776cac2db608069dff51619d2
7
- data.tar.gz: '099759cd2d503012da3ac66a22a28fee1b67b2d07e67f0c421ae348477fd7db1898a8fed75087d2365f9dff61cc3c189219fc9184105a0fb8e11990d55684856'
6
+ metadata.gz: 1cd2869a8e8709375a4b82fde759ab37a7e43a41f1ceedbdf1f563e68a6ac58976c950b8b1088ca8bd7c50ee81e42a8d744bd207d5a5819d23d967fa6be8f242
7
+ data.tar.gz: 844432fe9092b9918d238ce1ff730dd4028993c087665f26c8361518f49d7c5a189362a7a987c2fc3a92fcb81ecd94cadcd651d3311649fa62e4a79357718e80
@@ -1,170 +1,150 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DcidevApproval
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
- # p self.audit_trail
52
- log = activity_logs.where(activity_type: %w[update delete]).limit(1).order(created_at: :desc).try(:first)
53
- {
54
- modified_by: log.present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent).try(:username)} | #{log.try(:agent).try(:roles).try(:first).try(:name)})" : nil,
55
- modified_at: log.present? ? log.try(:created_at) || try(:updated_at) || try(:created_at) : nil
56
- }
57
- end
58
-
59
- def created_by
60
- log = activity_logs.order(created_at: :desc).limit(1).try(:first)
61
- {
62
- created_by: log.present? && log.try(:agent).try(:name).present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent_role).try(:name)})" : 'System',
63
- created_at: try(:created_at) || log.try(:created_at)
64
- }
65
- end
66
-
67
- def last_approved_by
68
- last_approve = activity_logs.where(activity_type: 'approve').limit(1).order(created_at: :desc).try(:first)
69
- last_entry = activity_logs.limit(1).order(created_at: :desc).try(:first)
70
- {
71
- 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,
72
- approved_at: last_approve.try(:id) == last_entry.try(:id) ? last_approve.try(:created_at) : nil
73
- }
74
- end
75
-
76
- def approve_changes
77
- if change_status.nil? && %w[waiting rejected].include?(status) && !update(status: :approved, data_changes: nil, change_status: nil)
78
- raise errors.full_messages.join(', ')
79
- # ActivityLog.write("Approve insert to #{self.class.to_s}", request, agent, menu, self) if params.log
80
- # self.delay(queue: "reorder_#{self.id}", priority: 0).reorder if self.class.column_names.include?("view_order")
81
-
82
- end
83
-
84
- case change_status
85
- when 'pending_update'
86
- raise errors.full_messages.join(', ') unless update_by_params(data_changes, false)
87
- raise errors.full_messages.join(', ') unless update(status: :approved, data_changes: nil, change_status: nil)
88
- # ActivityLog.write("Approve update to #{self.class.to_s}", request, agent, menu, self) if params.log
89
- # self.delay(queue: "reorder_#{self.id}", priority: 0).reorder if self.class.column_names.include?("view_order")
90
-
91
- when 'pending_delete'
92
- destroy
93
- end
94
- end
95
-
96
- def delete_changes
97
- # return unless %w[pending_update pending_delete].include? self.change_status
98
- raise errors.full_messages.join(', ') unless update(data_changes: nil, change_status: nil, status: status == 'waiting' ? :rejected : :approved)
99
- # ActivityLog.write("Reject changes to #{self.class.to_s}", request, agent, menu, self) if params.log
100
- end
101
-
102
- def edit_data(params, agent, bypass = true)
103
- raise 'data still waiting for approval' if waiting_approval?
104
-
105
- if bypass
106
- raise errors.full_messages.join(', ') unless update_by_params(params, false)
107
- # ActivityLog.write("Edit #{self.class.to_s}", request, agent, menu, self) if params.log
108
- elsif changes_present?(params)
109
- ActiveRecord::Base.transaction do
110
- data = if agent.is_admin? || status == 'waiting'
111
- params
112
- else
113
- {
114
- change_status: :pending_update, data_changes: agent.is_admin? ? nil : params
115
- }
116
- end
117
- raise errors.full_messages.join(', ') unless update_by_params(data, false)
118
- end
119
- # ActivityLog.write("#{agent.is_admin? ? nil : "Request "}Edit #{self.class.to_s}", request, agent, menu, self) if params.log
120
- end
121
- yield self
122
- end
123
-
124
- def approval(params)
125
- case params.status
126
- when 'approved'
127
- approve_changes
128
- when 'rejected'
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
- end
149
-
150
- module ClassMethods
151
- def create_data(params, agent, bypass = true)
152
- if bypass
153
- ActiveRecord::Base.transaction do
154
- data = params.merge!({ status: :approved })
155
- d = new_from_params(data)
156
- raise d.errors.full_messages.join(', ') unless d.save
157
-
158
- yield d
159
- # ActivityLog.write("#{agent.is_admin? ? nil : "Request "} Add #{self.to_s}", request, agent, menu, d) if params.log
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.11
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-13 00:00:00.000000000 Z
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.2
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