thecore_backend_commons 3.2.7 → 3.2.8
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a877ec156b6810a5c873ddbb929cd22fdc5e49b19c8985ebfc0d5af632b4931c
|
|
4
|
+
data.tar.gz: 9936ff7842df645b1b3ec179348224fa7a718ccd797f14b1fb01849fcf5782a8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8c817958ee00e2879490de8de099262bccb5b7d216595d7cd16369471ef03f1da450dcaf21fcd090a36f81aebcf4ff20e77e5cb6a466e46e713d0cdfeab8e22c
|
|
7
|
+
data.tar.gz: 1431fd2885496a43672c8beb6fdfc728fe74a65a3ab33beec824c7b26c67d840756366f02cfe38c5a51c72727730e6a24b3bd61122450288ca1a7bfb38c8d457
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
Rails.application.configure do
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
config.after_initialize do
|
|
3
|
+
Integer.send(:include, FixnumConcern)
|
|
4
|
+
String.send(:include, StringConcern)
|
|
5
|
+
ApplicationCable::Connection.send(:include, CableConnectionConcern)
|
|
6
|
+
# ApplicationRecord.send(:include, ApplicationRecordConcern)
|
|
7
|
+
ApplicationRecord.subclasses.each do |d|
|
|
8
|
+
d.send(:include, BaseApplicationRecordConcern) unless d.name.start_with?("ActiveStorage::", "ActionText::")
|
|
8
9
|
end
|
|
9
|
-
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -1,38 +1,97 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "active_support/concern"
|
|
2
2
|
|
|
3
|
-
module
|
|
3
|
+
module BaseApplicationRecordConcern
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
5
|
included do
|
|
6
|
+
# Add to all the models the ability to manage rich text content
|
|
7
|
+
has_rich_text :rich_content
|
|
8
|
+
|
|
9
|
+
# Add to all the models the ability to manage attached assets with remove and append functionality
|
|
10
|
+
has_many_attached :assets
|
|
11
|
+
|
|
12
|
+
attr_accessor :remove_assets, :append_assets
|
|
13
|
+
|
|
14
|
+
after_commit :manage_assets
|
|
15
|
+
|
|
16
|
+
# Broadcast messages via ActionCable for create, update, destroy actions
|
|
6
17
|
after_validation :validation_ko
|
|
7
18
|
|
|
8
19
|
after_commit :message_ok
|
|
9
20
|
|
|
10
21
|
after_rollback :message_ko
|
|
11
22
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
23
|
+
cattr_accessor :json_attrs
|
|
24
|
+
self.json_attrs = ::ModelDrivenApi.smart_merge (json_attrs || {}), {
|
|
25
|
+
methods: [:assets_paths, :rich_content_html],
|
|
26
|
+
}
|
|
27
|
+
end
|
|
15
28
|
|
|
16
|
-
|
|
17
|
-
ActionCable.server.broadcast("messages", build_message(true, true, [])) unless is_model_forbidden
|
|
18
|
-
end
|
|
29
|
+
private
|
|
19
30
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
def rich_content_html
|
|
32
|
+
rich_content&.body&.to_html
|
|
33
|
+
end
|
|
23
34
|
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
def assets_paths
|
|
36
|
+
# Returns the array of URLs for the assets attached to the project
|
|
37
|
+
assets.map do |asset|
|
|
38
|
+
{
|
|
39
|
+
id: asset.id,
|
|
40
|
+
filename: asset.filename.to_s,
|
|
41
|
+
content_type: asset.content_type,
|
|
42
|
+
url: Rails.application.routes.url_helpers.rails_blob_url(asset, only_path: true),
|
|
43
|
+
}
|
|
26
44
|
end
|
|
45
|
+
end
|
|
27
46
|
|
|
28
|
-
|
|
29
|
-
|
|
47
|
+
def validation_ko
|
|
48
|
+
ActionCable.server.broadcast("messages", build_message(false, false, self.errors.full_messages.uniq)) if self.errors.any? && !is_model_forbidden
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def message_ok
|
|
52
|
+
ActionCable.server.broadcast("messages", build_message(true, true, [])) unless is_model_forbidden
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def message_ko
|
|
56
|
+
ActionCable.server.broadcast("messages", build_message(false, true, [])) unless is_model_forbidden
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def is_model_forbidden
|
|
60
|
+
Rails.logger.debug("Checking if model #{self.class.name} is forbidden for ActionCable messages")
|
|
61
|
+
["User", "Role"].include?(self.class.name)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def build_message(success, valid, errors)
|
|
65
|
+
{ topic: :record, action: detect_action, class: self.class.name, success: success, valid: valid, errors: errors, record: self }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def detect_action
|
|
69
|
+
return :create if transaction_include_any_action?([:create])
|
|
70
|
+
return :update if transaction_include_any_action?([:update])
|
|
71
|
+
:destroy
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def manage_assets
|
|
75
|
+
# 1. Gestione CANCELLAZIONE
|
|
76
|
+
if remove_assets.present?
|
|
77
|
+
ids_to_remove = remove_assets
|
|
78
|
+
self.remove_assets = nil # Evita loop o doppie esecuzioni
|
|
79
|
+
|
|
80
|
+
Array(ids_to_remove).each do |id|
|
|
81
|
+
# Nota: usiamo purge per eliminare definitivamente file e blob
|
|
82
|
+
assets.find_by(id: id)&.purge
|
|
83
|
+
end
|
|
30
84
|
end
|
|
31
85
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
86
|
+
# 2. Gestione AGGIUNTA (APPEND)
|
|
87
|
+
if append_assets.present?
|
|
88
|
+
files_to_attach = append_assets
|
|
89
|
+
|
|
90
|
+
# Fondamentale: svuotare prima di attach per evitare ricorsione
|
|
91
|
+
# se attach dovesse innescare un nuovo save callbacks
|
|
92
|
+
self.append_assets = nil
|
|
93
|
+
|
|
94
|
+
assets.attach(files_to_attach)
|
|
36
95
|
end
|
|
37
96
|
end
|
|
38
97
|
end
|