loggable_activity 0.5.0 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +10 -0
- data/CHANGELOG.md +15 -0
- data/GETTING-STARTED.md +60 -0
- data/README.md +42 -31
- data/app/assets/config/loggable_activity_manifest.js +1 -3
- data/app/assets/javascripts/loggable_activity/application.js +0 -1
- data/app/assets/stylesheets/loggable_activity/activities.scss +25 -0
- data/app/assets/stylesheets/loggable_activity/application.scss +2 -6
- data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_first_page.html.erb +0 -1
- data/app/views/kaminari/pico/_last_page.html.erb +12 -0
- data/app/views/layouts/loggable_activity/application.html.slim +20 -0
- data/app/views/loggable_activity/activities/_activities.html.slim +28 -0
- data/app/views/loggable_activity/activities/index.html.erb +5 -6
- data/app/views/loggable_activity/bootstrap/kaminari/_first_page.html.erb +13 -0
- data/app/views/loggable_activity/bootstrap/kaminari/_gap.html.erb +8 -0
- data/app/views/loggable_activity/bootstrap/kaminari/_next_page.html.erb +13 -0
- data/app/views/loggable_activity/bootstrap/kaminari/_page.html.erb +14 -0
- data/app/views/loggable_activity/bootstrap/kaminari/_paginator.html.erb +27 -0
- data/app/views/loggable_activity/bootstrap/kaminari/_prev_page.html.erb +13 -0
- data/config/initializers/kaminari_config.rb +1 -1
- data/lib/generators/loggable_activity/install/install_generator.rb +68 -0
- data/lib/generators/loggable_activity/install/templates/create_loggable_activities.rb +45 -0
- data/lib/generators/loggable_activity/install/templates/loggable_activity.en.yml +32 -0
- data/lib/generators/loggable_activity/install/templates/loggable_activity.rb +27 -0
- data/lib/generators/loggable_activity/install/templates/loggable_activity.yml +103 -0
- data/lib/loggable_activity/activity.rb +4 -3
- data/lib/loggable_activity/config_schema.json +2 -20
- data/lib/loggable_activity/configuration.rb +0 -15
- data/lib/loggable_activity/encryption.rb +33 -12
- data/lib/loggable_activity/encryption_key.rb +1 -1
- data/lib/loggable_activity/engine.rb +0 -5
- data/lib/loggable_activity/error.rb +3 -3
- data/lib/loggable_activity/hooks.rb +42 -18
- data/lib/loggable_activity/payload.rb +6 -4
- data/lib/loggable_activity/services/base_payloads_builder.rb +2 -1
- data/lib/loggable_activity/services/custom_payloads_builder.rb +55 -0
- data/lib/loggable_activity/services/payloads_builder.rb +3 -3
- data/lib/loggable_activity/services/update_payloads_builder.rb +4 -4
- data/lib/loggable_activity/version.rb +1 -1
- data/lib/loggable_activity.rb +4 -1
- data/loggable_activity-0.5.0.gem +0 -0
- metadata +34 -127
- data/app/views/layouts/loggable_activity/application.html.erb +0 -19
- data/app/views/loggable_activity/activities/_activities.html.erb +0 -56
- data/app/views/loggable_activity/activities/show.html.erb +0 -2
- /data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_gap.html.erb +0 -0
- /data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_next_page.html.erb +0 -0
- /data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_page.html.erb +0 -0
- /data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_paginator.html.erb +0 -0
- /data/app/views/kaminari/{kaminari-turbo-bootstrap → pico}/_prev_page.html.erb +0 -0
- /data/app/views/{kaminari/kaminari-turbo-bootstrap → loggable_activity/bootstrap/kaminari}/_last_page.html.erb +0 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
Company:
|
2
|
+
fetch_record_name_from: name
|
3
|
+
route: /companies/:id
|
4
|
+
loggable_attrs:
|
5
|
+
- name
|
6
|
+
public_attrs:
|
7
|
+
- name
|
8
|
+
relations:
|
9
|
+
- has_many: :users
|
10
|
+
route: /users/:id
|
11
|
+
model: User
|
12
|
+
loggable_attrs:
|
13
|
+
- first_name
|
14
|
+
- last_name
|
15
|
+
auto_log:
|
16
|
+
- create
|
17
|
+
- update
|
18
|
+
- destroy
|
19
|
+
Hat:
|
20
|
+
fetch_record_name_from: color
|
21
|
+
route: /user/:id
|
22
|
+
loggable_attrs:
|
23
|
+
- color
|
24
|
+
auto_log:
|
25
|
+
- create
|
26
|
+
- destroy
|
27
|
+
relations:
|
28
|
+
- belongs_to: :user
|
29
|
+
route: /demo/users/:id
|
30
|
+
model: User
|
31
|
+
loggable_attrs:
|
32
|
+
- first_name
|
33
|
+
User:
|
34
|
+
fetch_record_name_from: full_name
|
35
|
+
route: /demo/users/:id
|
36
|
+
loggable_attrs:
|
37
|
+
- first_name
|
38
|
+
- last_name
|
39
|
+
- age
|
40
|
+
- email
|
41
|
+
- user_type
|
42
|
+
auto_log:
|
43
|
+
- create
|
44
|
+
- update
|
45
|
+
- destroy
|
46
|
+
public_attrs:
|
47
|
+
- age
|
48
|
+
- user_type
|
49
|
+
relations:
|
50
|
+
- has_one: :profile
|
51
|
+
route: /demo/users/:id/profile
|
52
|
+
model: Profile
|
53
|
+
loggable_attrs:
|
54
|
+
- bio
|
55
|
+
- phone_number
|
56
|
+
public_attrs:
|
57
|
+
- date_of_birth
|
58
|
+
- has_many: :hats
|
59
|
+
route: /users/:id
|
60
|
+
model: Hat
|
61
|
+
loggable_attrs:
|
62
|
+
- color
|
63
|
+
- belongs_to: :company
|
64
|
+
route: /companies/:id
|
65
|
+
model: Company
|
66
|
+
loggable_attrs:
|
67
|
+
- name
|
68
|
+
Profile:
|
69
|
+
fetch_record_name_from: phone_number
|
70
|
+
route: /demo/profiles/:id
|
71
|
+
loggable_attrs:
|
72
|
+
- bio
|
73
|
+
- profile_picture_url
|
74
|
+
- location
|
75
|
+
- date_of_birth
|
76
|
+
- phone_number
|
77
|
+
public_attrs:
|
78
|
+
- date_of_birth
|
79
|
+
# auto_log:
|
80
|
+
# - create
|
81
|
+
# - update
|
82
|
+
# - destroy
|
83
|
+
# relations:
|
84
|
+
# - belongs_to: :user
|
85
|
+
# route: /demo/users/:id
|
86
|
+
# model: User
|
87
|
+
# loggable_attrs:
|
88
|
+
# - first_name
|
89
|
+
# - last_name
|
90
|
+
# - age
|
91
|
+
# - email
|
92
|
+
# - user_type
|
93
|
+
# - belongs_to: :demo_address
|
94
|
+
# route: show_demo_address
|
95
|
+
# model: Demo::Address
|
96
|
+
# loggable_attrs:
|
97
|
+
# - street
|
98
|
+
# - postal_code
|
99
|
+
# - belongs_to: :demo_club
|
100
|
+
# route: show_demo_club
|
101
|
+
# model: Demo::Club
|
102
|
+
# loggable_attrs:
|
103
|
+
# - name
|
@@ -31,7 +31,8 @@ module LoggableActivity
|
|
31
31
|
'has_many_update_payload' => 'has_many',
|
32
32
|
'belongs_to_payload' => 'belongs_to',
|
33
33
|
'belongs_to_destroy_payload' => 'belongs_to',
|
34
|
-
'belongs_to_update_payload' => 'belongs_to'
|
34
|
+
'belongs_to_update_payload' => 'belongs_to',
|
35
|
+
'custom_payload' => 'self'
|
35
36
|
}.freeze
|
36
37
|
|
37
38
|
# Returns an array of hashes, each representing an activity's attributes and its associated relations. The structure and relations to include are specified in 'config/loggable_activity.yaml'. This format is designed for UI display purposes.
|
@@ -106,7 +107,7 @@ module LoggableActivity
|
|
106
107
|
# "David Bowie"
|
107
108
|
#
|
108
109
|
def record_display_name
|
109
|
-
primary_payload
|
110
|
+
primary_payload&.record_display_name
|
110
111
|
end
|
111
112
|
|
112
113
|
# Returns the path for the activity.
|
@@ -171,7 +172,7 @@ module LoggableActivity
|
|
171
172
|
# payload.record_type # => 'SOMD_MODEL_NAME'
|
172
173
|
#
|
173
174
|
def primary_payload
|
174
|
-
related_to_activity_as = %w[primary_payload primary_update_payload primary_destroy_payload]
|
175
|
+
related_to_activity_as = %w[primary_payload primary_update_payload primary_destroy_payload custom_payload]
|
175
176
|
payloads.detect { |p| related_to_activity_as.include?(p.related_to_activity_as) }
|
176
177
|
end
|
177
178
|
|
@@ -1,19 +1,8 @@
|
|
1
1
|
{
|
2
2
|
"$schema": "http://json-schema.org/draft-06/schema#",
|
3
3
|
"type": "object",
|
4
|
-
"properties": {
|
5
|
-
"current_user_model_name": {
|
6
|
-
"type": "string"
|
7
|
-
},
|
8
|
-
"current_user_name": {
|
9
|
-
"type": "string"
|
10
|
-
},
|
11
|
-
"task_for_sanitization": {
|
12
|
-
"type": "boolean"
|
13
|
-
}
|
14
|
-
},
|
15
4
|
"patternProperties": {
|
16
|
-
"^
|
5
|
+
"^[A-Za-z0-9_:]+$": {
|
17
6
|
"type": "object",
|
18
7
|
"properties": {
|
19
8
|
"data_owner": {
|
@@ -72,9 +61,6 @@
|
|
72
61
|
"route": {
|
73
62
|
"type": "string",
|
74
63
|
"minLength": 1
|
75
|
-
},
|
76
|
-
"fetch_record_name_from": {
|
77
|
-
"type": "string"
|
78
64
|
}
|
79
65
|
},
|
80
66
|
"required": [
|
@@ -88,9 +74,5 @@
|
|
88
74
|
]
|
89
75
|
}
|
90
76
|
},
|
91
|
-
"additionalProperties": false
|
92
|
-
"required": [
|
93
|
-
"current_user_model_name",
|
94
|
-
"current_user_name"
|
95
|
-
]
|
77
|
+
"additionalProperties": false
|
96
78
|
}
|
@@ -51,21 +51,6 @@ module LoggableActivity
|
|
51
51
|
def for_class(class_name)
|
52
52
|
config_data[class_name]
|
53
53
|
end
|
54
|
-
|
55
|
-
# Returns the name of the field or method to use for the actor's display name.
|
56
|
-
def current_user_name
|
57
|
-
config_data['current_user_name']
|
58
|
-
end
|
59
|
-
|
60
|
-
# Returns the name of the model to use for the current user.
|
61
|
-
def current_user_model_name
|
62
|
-
config_data['current_user_model_name']
|
63
|
-
end
|
64
|
-
|
65
|
-
# Returns whatever models should be sanitized on delete.
|
66
|
-
def task_for_sanitization
|
67
|
-
config_data['task_for_sanitization']
|
68
|
-
end
|
69
54
|
end
|
70
55
|
end
|
71
56
|
end
|
@@ -16,13 +16,11 @@ module LoggableActivity
|
|
16
16
|
# "SOME_ENCRYPTED_STRING"
|
17
17
|
#
|
18
18
|
def self.encrypt(data, secret_key)
|
19
|
-
return nil if secret_key.nil?
|
20
|
-
return nil if data.nil?
|
19
|
+
return nil if secret_key.nil? || data.nil?
|
21
20
|
|
22
21
|
encryption_key = Base64.decode64(secret_key)
|
23
22
|
unless encryption_key.bytesize == 32
|
24
|
-
raise EncryptionError,
|
25
|
-
"Encryption failed: Invalid encoded_key length #{encryption_key.bytesize}"
|
23
|
+
raise EncryptionError, "Encryption failed: Invalid encoded_key length #{encryption_key.bytesize}"
|
26
24
|
end
|
27
25
|
|
28
26
|
cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt
|
@@ -39,19 +37,47 @@ module LoggableActivity
|
|
39
37
|
# Decrypts the given data using the given encryption key
|
40
38
|
#
|
41
39
|
# Example:
|
42
|
-
# ::LoggableActivity::Encryption.decrypt('
|
40
|
+
# ::LoggableActivity::Encryption.decrypt('SOME_ENCRYPTED_DATA', 'SECRET_KEY')
|
43
41
|
#
|
44
42
|
# Returns:
|
45
43
|
# "my secret data"
|
46
44
|
#
|
47
45
|
def self.decrypt(data, secret_key)
|
46
|
+
case data
|
47
|
+
when Hash
|
48
|
+
decrypt_hash(data, secret_key)
|
49
|
+
when Array
|
50
|
+
decrypt_array(data, secret_key)
|
51
|
+
else
|
52
|
+
decrypt_data(data, secret_key)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Decrypts a hash's values using the given encryption key
|
57
|
+
def self.decrypt_hash(data, secret_key)
|
58
|
+
data
|
59
|
+
.transform_values { |value| decrypt(value, secret_key) }
|
60
|
+
.transform_keys(&:to_sym)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Decrypts an array's values using the given encryption key
|
64
|
+
def self.decrypt_array(data, secret_key)
|
65
|
+
data.map { |value| decrypt(value, secret_key) }
|
66
|
+
end
|
67
|
+
|
68
|
+
# Checks if a value is blank
|
69
|
+
def self.blank?(value)
|
70
|
+
value.respond_to?(:empty?) ? value.empty? : !value
|
71
|
+
end
|
72
|
+
|
73
|
+
# Decrypts individual data using the given encryption key
|
74
|
+
def self.decrypt_data(data, secret_key)
|
48
75
|
return I18n.t('loggable_activity.activity.deleted') if secret_key.nil?
|
49
76
|
return '' if data.blank?
|
50
77
|
|
51
78
|
encryption_key = Base64.decode64(secret_key)
|
52
79
|
unless encryption_key.bytesize == 32
|
53
|
-
raise EncryptionError,
|
54
|
-
"Decryption failed: Invalid encoded_key length: #{encryption_key.bytesize}"
|
80
|
+
raise EncryptionError, "Decryption failed: Invalid encoded_key length: #{encryption_key.bytesize}"
|
55
81
|
end
|
56
82
|
|
57
83
|
cipher = OpenSSL::Cipher.new('AES-256-CBC').decrypt
|
@@ -73,10 +99,5 @@ module LoggableActivity
|
|
73
99
|
Rails.logger.error "ArgumentError Decryption failed: #{e.message}"
|
74
100
|
I18n.t('loggable_activity.decryption.failed')
|
75
101
|
end
|
76
|
-
|
77
|
-
# Returns true if the given value is blank
|
78
|
-
def self.blank?(value)
|
79
|
-
value.respond_to?(:empty?) ? value.empty? : !value
|
80
|
-
end
|
81
102
|
end
|
82
103
|
end
|
@@ -14,7 +14,7 @@ module LoggableActivity
|
|
14
14
|
|
15
15
|
# Prepare the record for deletion
|
16
16
|
def mark_as_deleted!
|
17
|
-
LoggableActivity
|
17
|
+
LoggableActivity.task_for_sanitization ? update(delete_at: DateTime.now + 1.month) : delete
|
18
18
|
end
|
19
19
|
|
20
20
|
# check if the encryption key is deleted or it is about to be deleted
|
@@ -18,10 +18,5 @@ module LoggableActivity
|
|
18
18
|
#
|
19
19
|
class Engine < ::Rails::Engine
|
20
20
|
isolate_namespace LoggableActivity
|
21
|
-
|
22
|
-
# initializer 'loggable_activity.assets.precompile' do |app|
|
23
|
-
# app.config.assets.precompile += %w( loggable_activity/application.css )
|
24
|
-
# app.config.assets.precompile += %w( loggable_activity/bootstrap.css )
|
25
|
-
# end
|
26
21
|
end
|
27
22
|
end
|
@@ -4,21 +4,21 @@ module LoggableActivity
|
|
4
4
|
# Error class for loggable activity.
|
5
5
|
class Error < StandardError
|
6
6
|
def initialize(msg = '')
|
7
|
-
super
|
7
|
+
super
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
# Error class for encryption.
|
12
12
|
class EncryptionError < StandardError
|
13
13
|
def initialize(msg = '')
|
14
|
-
super
|
14
|
+
super
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
# This class is used to load the configuration file located at config/loggable_activity.yml
|
19
19
|
class ConfigurationError < StandardError
|
20
20
|
def initialize(msg = '')
|
21
|
-
super
|
21
|
+
super
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -42,7 +42,7 @@ module LoggableActivity
|
|
42
42
|
# @param params [Hash] Additional parameters for the activity.
|
43
43
|
def log(action, actor: nil, params: {})
|
44
44
|
@action = action
|
45
|
-
@actor = actor || Thread.current[:current_user]
|
45
|
+
@actor = actor || Thread.current[:current_user] || created_by_self
|
46
46
|
return nil if @actor.nil?
|
47
47
|
|
48
48
|
@record = self
|
@@ -56,8 +56,14 @@ module LoggableActivity
|
|
56
56
|
log_destroy
|
57
57
|
when :update
|
58
58
|
log_update
|
59
|
+
when :login
|
60
|
+
log_login
|
61
|
+
when :logout
|
62
|
+
log_logout
|
63
|
+
when :sign_up
|
64
|
+
log_sign_up
|
59
65
|
else
|
60
|
-
log_custom_activity
|
66
|
+
log_custom_activity
|
61
67
|
end
|
62
68
|
end
|
63
69
|
|
@@ -67,6 +73,15 @@ module LoggableActivity
|
|
67
73
|
|
68
74
|
private
|
69
75
|
|
76
|
+
# If the actor is the same as the record, it is created by the actor.
|
77
|
+
# This assumes it is a sign_up action.
|
78
|
+
def created_by_self
|
79
|
+
return unless LoggableActivity.actor_model_name == self.class.name
|
80
|
+
|
81
|
+
@action = :sign_up
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
70
85
|
# Logs an activity for the current action.
|
71
86
|
def log_activity
|
72
87
|
create_activity(build_payloads)
|
@@ -95,6 +110,28 @@ module LoggableActivity
|
|
95
110
|
)
|
96
111
|
end
|
97
112
|
|
113
|
+
def log_login
|
114
|
+
create_activity(build_payloads)
|
115
|
+
end
|
116
|
+
|
117
|
+
def log_logout
|
118
|
+
create_activity(build_payloads)
|
119
|
+
end
|
120
|
+
|
121
|
+
def log_sign_up
|
122
|
+
create_activity(build_payloads)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Logs a custom activity.
|
126
|
+
def log_custom_activity
|
127
|
+
create_activity(build_custom_payload)
|
128
|
+
end
|
129
|
+
|
130
|
+
def build_custom_payload
|
131
|
+
::LoggableActivity::Services::CustomPayloadsBuilder
|
132
|
+
.new(self, @payloads, @params).build
|
133
|
+
end
|
134
|
+
|
98
135
|
# Builds update payloads for the current action.
|
99
136
|
def build_update_payloads
|
100
137
|
::LoggableActivity::Services::UpdatePayloadsBuilder
|
@@ -118,9 +155,6 @@ module LoggableActivity
|
|
118
155
|
@payloads.empty?
|
119
156
|
end
|
120
157
|
|
121
|
-
# Logs a custom activity.
|
122
|
-
def log_custom_activity(activity); end
|
123
|
-
|
124
158
|
# Logs an update activity automatically if configured.
|
125
159
|
def log_update_activity
|
126
160
|
return unless hooks_enabled?
|
@@ -165,13 +199,13 @@ module LoggableActivity
|
|
165
199
|
|
166
200
|
# Returns the encrypted name of the actor.
|
167
201
|
def encrypted_actor_name
|
168
|
-
name = @actor.send(
|
202
|
+
name = @actor.send(fetch_actor_name_from)
|
169
203
|
::LoggableActivity::Encryption.encrypt(name, actor_secret_key)
|
170
204
|
end
|
171
205
|
|
172
206
|
# Reads the field to feetch the record name from.
|
173
|
-
def
|
174
|
-
|
207
|
+
def fetch_actor_name_from
|
208
|
+
LoggableActivity.fetch_actor_name_from
|
175
209
|
end
|
176
210
|
|
177
211
|
# Returns the action key for the current action.
|
@@ -194,16 +228,6 @@ module LoggableActivity
|
|
194
228
|
encryption_key_for_record(@actor)&.secret_key
|
195
229
|
end
|
196
230
|
|
197
|
-
# Returns the display name of the actor.
|
198
|
-
def actor__name_field
|
199
|
-
Rails.application.config.loggable_activity.actor_display_name || "id: #{@actor.id}, class: #{@actor.class.name}"
|
200
|
-
end
|
201
|
-
|
202
|
-
# Returns the model name of the current user.
|
203
|
-
def current_user_model?
|
204
|
-
Rails.application.config.loggable_activity.current_user_model_name == self.class.name
|
205
|
-
end
|
206
|
-
|
207
231
|
class_methods do
|
208
232
|
# The loggable_attrs attribute is used read the configuration for the model that included LoggableActivity::Hooks.
|
209
233
|
attr_accessor :loggable_attrs, :relations, :auto_log, :fetch_record_name_from, :route, :public_attrs
|
@@ -22,6 +22,7 @@ module LoggableActivity
|
|
22
22
|
has_one_payload
|
23
23
|
belongs_to_update_payload
|
24
24
|
has_many_create_payload
|
25
|
+
custom_payload
|
25
26
|
].freeze
|
26
27
|
|
27
28
|
# Enumeration for different updatepayload types
|
@@ -47,7 +48,8 @@ module LoggableActivity
|
|
47
48
|
belongs_to_update_payload: 9,
|
48
49
|
belongs_to_destroy_payload: 10,
|
49
50
|
has_one_destroy_payload: 11,
|
50
|
-
has_many_destroy_payload: 12
|
51
|
+
has_many_destroy_payload: 12,
|
52
|
+
custom_payload: 13
|
51
53
|
}
|
52
54
|
|
53
55
|
# Returns the decrypted attrs.
|
@@ -62,14 +64,14 @@ module LoggableActivity
|
|
62
64
|
#
|
63
65
|
def attrs
|
64
66
|
return deleted_attrs if record.nil?
|
65
|
-
|
67
|
+
|
66
68
|
case related_to_activity_as
|
67
69
|
when *DECRYPT_ATTRS_TYPES
|
68
|
-
decrypted_attrs.merge(public_attrs:
|
70
|
+
decrypted_attrs.merge(public_attrs:)
|
69
71
|
when *DECRYPT_UPDATE_ATTRS_TYPES
|
70
72
|
decrypted_update_attrs + public_attrs['changes']
|
71
73
|
else
|
72
|
-
{ public_attrs:
|
74
|
+
{ public_attrs: }
|
73
75
|
end
|
74
76
|
end
|
75
77
|
|
@@ -6,9 +6,10 @@ module LoggableActivity
|
|
6
6
|
# Other service modules related to the loggable activity will inherit from this module.
|
7
7
|
class BasePayloadsBuilder
|
8
8
|
# Initializes the PayloadsBuilder with a record and an initial collection of payloads,
|
9
|
-
def initialize(record, payloads)
|
9
|
+
def initialize(record, payloads, params = {})
|
10
10
|
@record = record
|
11
11
|
@payloads = payloads
|
12
|
+
@params = params
|
12
13
|
@loggable_attrs = record.class.loggable_attrs
|
13
14
|
@public_attrs = record.class.public_attrs
|
14
15
|
# @relation_config = record.relation_config
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LoggableActivity
|
4
|
+
module Services
|
5
|
+
# This service class builds custom payloads.
|
6
|
+
class CustomPayloadsBuilder < BasePayloadsBuilder
|
7
|
+
# Builds a custom payloads for a ::LoggableActivity::Activity.
|
8
|
+
def build
|
9
|
+
encryption_key = encryption_key_for_record(@record)
|
10
|
+
@secret_key = encryption_key&.secret_key
|
11
|
+
encrypted_attrs = build_custom_payloads(@params)
|
12
|
+
|
13
|
+
payload = ::LoggableActivity::Payload.new(
|
14
|
+
encryption_key:,
|
15
|
+
record: @record,
|
16
|
+
encrypted_record_name:,
|
17
|
+
encrypted_attrs:,
|
18
|
+
related_to_activity_as: 'custom_payload',
|
19
|
+
route: @record.class.route,
|
20
|
+
current_payload: true,
|
21
|
+
data_owner: @record,
|
22
|
+
public_attrs: {}
|
23
|
+
)
|
24
|
+
|
25
|
+
unless payload.valid?
|
26
|
+
error_message = "Payload validation failed: #{payload.errors.full_messages.join(', ')}"
|
27
|
+
raise LoggableActivity::Error, error_message
|
28
|
+
end
|
29
|
+
|
30
|
+
@payloads << payload
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def encrypted_record_name
|
36
|
+
return encrypt_attr(@params[:display_name], @secret_key) if @params[:display_name]
|
37
|
+
|
38
|
+
encrypt_record_name_for_record(@record, @secret_key)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_custom_payloads(params)
|
42
|
+
params.transform_values do |value|
|
43
|
+
case value
|
44
|
+
when Hash
|
45
|
+
build_custom_payloads(value)
|
46
|
+
when Array
|
47
|
+
value.map { |v| v.is_a?(Hash) ? build_custom_payloads(v) : encrypt_attr(v, @secret_key) }
|
48
|
+
else
|
49
|
+
encrypt_attr(value, @secret_key)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -57,7 +57,7 @@ module LoggableActivity
|
|
57
57
|
def build_has_many_payloads(relation_config)
|
58
58
|
relation = (relation_config['has_many']).to_s
|
59
59
|
records = @record.send(relation)
|
60
|
-
data_owner = relation_config['data_owner']
|
60
|
+
data_owner = relation_config['data_owner'] || false
|
61
61
|
options = { related_to_activity_as: 'has_many_payload', current_payload: true, data_owner: }
|
62
62
|
records.each do |record|
|
63
63
|
record.disable_hooks!
|
@@ -73,7 +73,7 @@ module LoggableActivity
|
|
73
73
|
|
74
74
|
record.disable_hooks!
|
75
75
|
|
76
|
-
data_owner = relation_config['data_owner']
|
76
|
+
data_owner = relation_config['data_owner'] || false
|
77
77
|
options =
|
78
78
|
{
|
79
79
|
related_to_activity_as: 'has_one_payload',
|
@@ -90,7 +90,7 @@ module LoggableActivity
|
|
90
90
|
record = @record.send(relation)
|
91
91
|
return if record.nil?
|
92
92
|
|
93
|
-
data_owner = relation_config['data_owner']
|
93
|
+
data_owner = relation_config['data_owner'] || false
|
94
94
|
|
95
95
|
if data_owner
|
96
96
|
encryption_key = encryption_key_for_record(@record)
|
@@ -56,7 +56,7 @@ module LoggableActivity
|
|
56
56
|
relation_id = "#{relation_config['belongs_to']}_id"
|
57
57
|
model_class_name = relation_config['model']
|
58
58
|
model_class = model_class_name.constantize
|
59
|
-
data_owner = relation_config['data_owner']
|
59
|
+
data_owner = relation_config['data_owner'] || false
|
60
60
|
|
61
61
|
model_ids = saved_changes(@record).map { |hash| hash[relation_id] }
|
62
62
|
|
@@ -101,7 +101,7 @@ module LoggableActivity
|
|
101
101
|
)
|
102
102
|
|
103
103
|
previous_values = previous_values.slice(*loggable_attrs)
|
104
|
-
data_owner = relation_config['data_owner']
|
104
|
+
data_owner = relation_config['data_owner'] || false
|
105
105
|
options = { related_to_activity_as: 'has_many_update_payload', current_payload: true, data_owner: }
|
106
106
|
|
107
107
|
build_encrypted_update_payload(
|
@@ -115,7 +115,7 @@ module LoggableActivity
|
|
115
115
|
|
116
116
|
def build_has_many_create_payload(relation_config, record)
|
117
117
|
record.disable_hooks = true
|
118
|
-
data_owner = relation_config['data_owner']
|
118
|
+
data_owner = relation_config['data_owner'] || false
|
119
119
|
options =
|
120
120
|
{ related_to_activity_as: 'has_many_create_payload', current_payload: true, data_owner: }
|
121
121
|
build_encrypted_payload(record, options)
|
@@ -138,7 +138,7 @@ module LoggableActivity
|
|
138
138
|
|
139
139
|
previous_values = previous_values.slice(*loggable_attrs)
|
140
140
|
current_values = current_values.slice(*loggable_attrs)
|
141
|
-
data_owner = relation_config['data_owner']
|
141
|
+
data_owner = relation_config['data_owner'] || false
|
142
142
|
options = { related_to_activity_as: 'has_one_update_payload', current_payload: true, data_owner: }
|
143
143
|
|
144
144
|
build_encrypted_update_payload(
|
data/lib/loggable_activity.rb
CHANGED
@@ -12,6 +12,7 @@ require 'loggable_activity/payload'
|
|
12
12
|
require 'loggable_activity/error'
|
13
13
|
require 'loggable_activity/sanitizer'
|
14
14
|
require 'loggable_activity/services/base_payloads_builder'
|
15
|
+
require 'loggable_activity/services/custom_payloads_builder'
|
15
16
|
require 'loggable_activity/services/payloads_builder'
|
16
17
|
require 'loggable_activity/services/update_payloads_builder'
|
17
18
|
require 'loggable_activity/services/destroy_payloads_builder'
|
@@ -48,6 +49,8 @@ require 'kaminari'
|
|
48
49
|
#
|
49
50
|
# This module is designed to be both powerful and easy to integrate, providing a solid foundation for activity logging.
|
50
51
|
module LoggableActivity
|
51
|
-
mattr_accessor :
|
52
|
+
mattr_accessor :actor_model_name
|
53
|
+
mattr_accessor :fetch_actor_name_from
|
52
54
|
mattr_accessor :config_file_path
|
55
|
+
mattr_accessor :task_for_sanitization
|
53
56
|
end
|
Binary file
|