Class: Decidim::Upgrade::WysiwygMigrator
- Inherits:
-
Object
- Object
- Decidim::Upgrade::WysiwygMigrator
show all
- Defined in:
- decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb
Defined Under Namespace
Classes: UndefinedColumnError
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of WysiwygMigrator.
168
169
170
171
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 168
def initialize(content)
@doc = Nokogiri::HTML5.parse("")
@content = content
end
|
Class Method Details
.batch_range(idx, data) ⇒ Object
31
32
33
34
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 31
def batch_range(idx, data)
start = idx * batch_size
(start + 1)..(start + data.count)
end
|
.batch_size ⇒ Object
27
28
29
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 27
def batch_size
@batch_size ||= 100
end
|
.convert(content) ⇒ Object
159
160
161
162
163
164
165
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 159
def convert(content)
if content.is_a?(Hash)
content.transform_values { |v| convert(v) }
else
new(content).run
end
end
|
.convert_model_data(relation, columns) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 52
def convert_model_data(relation, columns)
{}.tap do |converted|
relation.pluck(:id, *columns).map do |data|
record_data = {}
columns.each_with_index do |column, idx|
value = data[idx + 1]
record_data[column.to_s] = yield value, column
end
converted[data[0]] = record_data
end
end
end
|
.editor_attributes_for(manifest) ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 124
def editor_attributes_for(manifest)
editor_attributes = { global: [], step: [] }
editor_attributes.keys.each do |type|
manifest.settings(type).attributes.each do |key, attribute|
editor_attributes[type] << key.to_s if attribute.editor
end
editor_attributes.delete(type) if editor_attributes[type].blank?
end
editor_attributes
end
|
.hash_subkey(hash, subkey = nil) ⇒ Object
This is just a simplification method to avoid extending the cyclomatic complexity of the settings hash value update lambda. In case a subkey is given, the value of that key in the given hash will be yielded. If no subkey is given, the hash itself is yielded. This allows us to avoid repeating the same code twice in the ‘update_settings` method depending on the depth of the hash we want to manage.
72
73
74
75
76
77
78
79
80
81
82
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 72
def hash_subkey(hash, subkey = nil)
return if hash.blank?
if subkey.nil?
yield hash
else
return if hash[subkey.to_s].blank?
yield hash[subkey.to_s]
end
end
|
.model_registry ⇒ Object
9
10
11
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 9
def model_registry
@model_registry ||= []
end
|
.register_model(klass, columns) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 13
def register_model(klass, columns)
if klass.is_a?(String)
return unless Object.const_defined?(klass)
klass = Object.const_get(klass)
end
columns.each do |col|
raise UndefinedColumnError, "#{klass} does not have column named '#{col}'." unless klass.column_names.include?(col.to_s)
end
model_registry << { class: klass, columns: }
end
|
.update_component_settings ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 135
def update_component_settings
Decidim.component_manifests.each do |manifest|
editor_attributes = editor_attributes_for(manifest)
next if editor_attributes.blank?
keys = {}
keys[:global] = { type: :single, keys: editor_attributes[:global] } if editor_attributes[:global].present?
keys[:steps] = { type: :multi, keys: editor_attributes[:step] } if editor_attributes[:step].present?
update_settings(
Decidim::Component.where(manifest_name: manifest.name),
keys
) do |_klass, range|
yield manifest.name, range if block_given?
end
end
end
|
.update_models ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 84
def update_models(&)
model_registry.each do |model|
update_records_batches(
model[:class],
model[:columns],
->(value, _column) { convert(value) },
&
)
end
end
|
.update_records_batches(query, columns, value_convert) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 36
def update_records_batches(query, columns, value_convert)
query.in_batches(of: batch_size).each_with_index do |relation, idx|
data = convert_model_data(relation, columns, &value_convert)
next if data.empty?
yield relation.klass, batch_range(idx, data) if block_given?
data.each do |id, values|
relation.klass.find(id).update_columns(values) end
end
end
|
.update_settings(query, keys) ⇒ Object
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
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 95
def update_settings(query, keys, &)
keys = { nil => keys } unless keys.is_a?(Hash)
update_records_batches(
query,
[:settings],
lambda do |settings, _column|
keys.each do |key, definition|
definition = { type: :single, keys: definition } unless definition.is_a?(Hash)
hash_subkey(settings, key) do |current|
subkeys = definition[:type] == :multi ? current.keys : [nil]
subkeys.each do |subkey|
hash_subkey(current, subkey) do |attrs|
definition[:keys].each do |attribute|
attrs[attribute.to_s] = convert(attrs[attribute.to_s])
end
attrs
end
end
end
end
settings
end,
&
)
end
|
Instance Method Details
#run ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'decidim-core/lib/decidim/upgrade/wysiwyg_migrator.rb', line 173
def run
return content unless content
content_doc = Nokogiri::HTML5.parse(content)
content_root = content_doc.at("//body")
return content if content_root.children.empty?
return content if content_root.children.length == 1 && content_root.children.first.name == "text"
root = doc.at("//body")
content_root.children.each do |node|
append_node(root, convert_node(node))
end
root.inner_html
end
|