vario 1.0.7 → 2.0.0
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 +4 -4
- data/README.md +68 -1
- data/Rakefile +0 -4
- data/app/assets/stylesheets/vario/application.css +316 -0
- data/app/assets/tailwind/vario/engine.css +6 -0
- data/app/controllers/vario/application_controller.rb +9 -2
- data/app/controllers/vario/topics_controller.rb +131 -0
- data/app/controllers/vario/values_controller.rb +27 -0
- data/app/javascript/vario/application.js +18 -0
- data/app/javascript/vario/controllers/refinements_controller.js +29 -0
- data/app/javascript/vario/controllers/search_controller.js +63 -0
- data/app/javascript/vario/controllers/sortable_controller.js +61 -0
- data/app/javascript/vario/controllers/tabs_controller.js +19 -0
- data/app/javascript/vario/controllers/topic_controller.js +40 -0
- data/app/models/vario/conditions.rb +158 -0
- data/app/models/vario/refinement.rb +38 -0
- data/app/models/vario/setting.rb +64 -3
- data/app/policies/vario/topic_policy.rb +37 -0
- data/app/views/vario/shared/_card.html.erb +25 -0
- data/app/views/vario/topics/_applies.html.erb +49 -0
- data/app/views/vario/topics/_nav.html.erb +28 -0
- data/app/views/vario/topics/_refinements.html.erb +41 -0
- data/app/views/vario/topics/_setting.html.erb +9 -0
- data/app/views/vario/topics/index.html.erb +3 -0
- data/app/views/vario/topics/show.html.erb +31 -0
- data/app/views/vario/values/_form.html.erb +20 -0
- data/config/importmap.rb +4 -0
- data/config/locales/en.yml +50 -0
- data/config/locales/nl.yml +50 -0
- data/config/routes.rb +11 -6
- data/db/migrate/20261118190000_add_value_to_vario_settings.rb +8 -0
- data/db/migrate/20261118190100_create_vario_refinements.rb +19 -0
- data/lib/generators/vario/topics_from_config/topics_from_config_generator.rb +189 -0
- data/lib/tasks/vario_tasks.rake +30 -0
- data/lib/vario/action_view_helpers.rb +119 -16
- data/lib/vario/active_record_helpers.rb +3 -2
- data/lib/vario/api.rb +49 -60
- data/lib/vario/backup.rb +50 -0
- data/lib/vario/card.rb +27 -0
- data/lib/vario/config.rb +36 -11
- data/lib/vario/context_variables.rb +69 -0
- data/lib/vario/converter.rb +113 -0
- data/lib/vario/engine.rb +27 -0
- data/lib/vario/migrator.rb +83 -0
- data/lib/vario/resolver.rb +71 -0
- data/lib/vario/settable.rb +41 -23
- data/lib/vario/settable_topic.rb +37 -0
- data/lib/vario/settings_reader.rb +45 -15
- data/lib/vario/topic.rb +299 -0
- data/lib/vario/topic_reader.rb +42 -0
- data/lib/vario/types.rb +64 -0
- data/lib/vario/version.rb +1 -1
- data/lib/vario.rb +16 -0
- metadata +116 -34
- data/app/controllers/vario/levels_controller.rb +0 -82
- data/app/controllers/vario/settings_controller.rb +0 -35
- data/app/views/vario/levels/_form.html.slim +0 -53
- data/app/views/vario/levels/create.js.erb +0 -4
- data/app/views/vario/levels/destroy.js.erb +0 -4
- data/app/views/vario/levels/update.js.erb +0 -4
- data/app/views/vario/settings/_levels.html.slim +0 -22
- data/app/views/vario/settings/_new_level.html.slim +0 -7
- data/app/views/vario/settings/_settings.html.slim +0 -24
- data/app/views/vario/settings/index.html.slim +0 -28
- data/app/views/vario/settings/levels.html.slim +0 -1
- data/app/views/vario/settings/show.html.slim +0 -13
data/lib/vario/api.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'grape-swagger'
|
|
4
4
|
|
|
@@ -22,21 +22,24 @@ module Vario
|
|
|
22
22
|
record
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
# The keys it was called with, without blanks
|
|
26
|
+
def context_params
|
|
27
|
+
declared_params.except('id', 'value').to_h.compact_blank
|
|
27
28
|
end
|
|
28
29
|
|
|
29
|
-
def
|
|
30
|
-
|
|
30
|
+
def setting_response(record, name, context)
|
|
31
|
+
{ setting: name, value: record.reload.setting(name, context) }
|
|
31
32
|
end
|
|
33
|
+
end
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
class << self
|
|
36
|
+
def value_type_for(definition)
|
|
37
|
+
case definition.type
|
|
38
|
+
when :array then Array[String]
|
|
39
|
+
when :boolean then Grape::API::Boolean
|
|
40
|
+
when :integer then Integer
|
|
41
|
+
else String
|
|
38
42
|
end
|
|
39
|
-
context
|
|
40
43
|
end
|
|
41
44
|
end
|
|
42
45
|
|
|
@@ -45,98 +48,85 @@ module Vario
|
|
|
45
48
|
include m
|
|
46
49
|
end
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
before do
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
end if configuration[:before].present?
|
|
51
|
+
before do
|
|
52
|
+
[*configuration[:before]].each do |method|
|
|
53
|
+
send(method) if method.is_a?(Symbol)
|
|
54
|
+
end
|
|
55
|
+
end if configuration[:before].present?
|
|
54
56
|
|
|
57
|
+
Vario::Topic.for(configuration[:settable]).flat_map { |topic| topic.definitions.values }.each do |definition|
|
|
58
|
+
setting_name = definition.name
|
|
55
59
|
group_name, short_setting_name = setting_name.split('.', 2)
|
|
56
60
|
|
|
57
61
|
next unless Vario.config.show_group?(group_name)
|
|
58
62
|
|
|
63
|
+
keys = definition.legacy_keys
|
|
64
|
+
description = definition.description || definition.label
|
|
65
|
+
choices = definition.choice_values.map(&:to_s) if definition.choices.is_a?(Array)
|
|
66
|
+
|
|
59
67
|
route_param :id, type: String do
|
|
60
68
|
namespace configuration[:prefix] do
|
|
61
69
|
namespace group_name do
|
|
62
70
|
desc "Get setting value" do
|
|
63
|
-
detail "Get the setting value for #{setting_name}. Setting description: #{
|
|
71
|
+
detail "Get the setting value for #{setting_name}. Setting description: #{description}"
|
|
64
72
|
end
|
|
65
73
|
params do
|
|
66
|
-
|
|
74
|
+
keys.each do |context_key|
|
|
67
75
|
optional context_key, type: String
|
|
68
76
|
end
|
|
69
77
|
end
|
|
70
78
|
scopes = [configuration[:oauth2].to_sym, "#{configuration[:oath2]}:read".to_sym] if configuration[:oauth2]
|
|
71
79
|
scopes ||= nil
|
|
72
80
|
get "/#{short_setting_name}", scopes: scopes do
|
|
73
|
-
record
|
|
74
|
-
context = settable_setting_context(setting_name, declared_params.reject { |key, value| key == 'id' }.to_h )
|
|
81
|
+
record = find_settable(declared_params[:id])
|
|
75
82
|
|
|
76
|
-
{ setting: setting_name, value: record.setting(setting_name,
|
|
83
|
+
{ setting: setting_name, value: record.setting(setting_name, context_params) }
|
|
77
84
|
end
|
|
78
85
|
|
|
79
|
-
setting_type =
|
|
80
|
-
case setting_data[:type]
|
|
81
|
-
when :array
|
|
82
|
-
Array[String]
|
|
83
|
-
when :boolean
|
|
84
|
-
Boolean
|
|
85
|
-
when :integer
|
|
86
|
-
Integer
|
|
87
|
-
else
|
|
88
|
-
String
|
|
89
|
-
end
|
|
90
|
-
|
|
91
86
|
desc "Add or change setting value" do
|
|
92
|
-
detail "Add or change setting value for #{setting_name}. Setting description: #{
|
|
87
|
+
detail "Add or change setting value for #{setting_name}. Setting description: #{description}"
|
|
93
88
|
end
|
|
94
89
|
params do
|
|
95
|
-
|
|
90
|
+
keys.each do |context_key|
|
|
96
91
|
optional context_key, type: String, documentation: { in: 'body' }
|
|
97
92
|
end
|
|
98
|
-
requires :value, type:
|
|
93
|
+
requires :value, type: API.value_type_for(definition), values: choices, documentation: { in: 'body' }
|
|
99
94
|
end
|
|
100
95
|
scopes = [configuration[:oauth2].to_sym, "#{configuration[:oath2]}:write".to_sym] if configuration[:oauth2]
|
|
101
96
|
scopes ||= nil
|
|
97
|
+
# Without keys the settable's own value, else the value of the
|
|
98
|
+
# refinement on exactly those (a new one goes first, as 1.x's
|
|
99
|
+
# levels did)
|
|
102
100
|
route [:post, :put], "/#{short_setting_name}", scopes: scopes do
|
|
103
|
-
record
|
|
104
|
-
context
|
|
105
|
-
vario_setting
|
|
106
|
-
conditions_hash = context.values.compact.blank? ? {} : context
|
|
107
|
-
level = vario_setting.levels.find { |level| conditions_hash == level.conditions_hash }
|
|
108
|
-
|
|
109
|
-
if level.nil?
|
|
110
|
-
level = Level.new(vario_setting, { conditions: context })
|
|
111
|
-
vario_setting.levels.unshift level
|
|
112
|
-
end
|
|
101
|
+
record = find_settable(declared_params[:id])
|
|
102
|
+
context = context_params
|
|
103
|
+
vario_setting = record.settings.find_or_initialize_by(name: setting_name)
|
|
113
104
|
|
|
114
|
-
|
|
115
|
-
vario_setting.save!
|
|
105
|
+
vario_setting.value_on!(context, declared_params[:value])
|
|
116
106
|
|
|
117
|
-
|
|
107
|
+
setting_response(record, setting_name, context)
|
|
118
108
|
end
|
|
119
109
|
|
|
120
110
|
desc "Remove setting value" do
|
|
121
|
-
detail "Remove setting value for #{setting_name}. Setting description: #{
|
|
111
|
+
detail "Remove setting value for #{setting_name}. Setting description: #{description}"
|
|
122
112
|
end
|
|
123
113
|
params do
|
|
124
|
-
|
|
114
|
+
keys.each do |context_key|
|
|
125
115
|
optional context_key, type: String
|
|
126
116
|
end
|
|
127
117
|
end
|
|
128
118
|
scopes = [configuration[:oauth2].to_sym, "#{configuration[:oath2]}:write".to_sym] if configuration[:oauth2]
|
|
129
119
|
scopes ||= nil
|
|
120
|
+
# Without keys the settable's own value, else the refinements
|
|
121
|
+
# on exactly those
|
|
130
122
|
delete "/#{short_setting_name}", scopes: scopes do
|
|
131
|
-
record
|
|
132
|
-
context
|
|
133
|
-
|
|
123
|
+
record = find_settable(declared_params[:id])
|
|
124
|
+
context = context_params
|
|
125
|
+
vario_setting = record.settings.find_by(name: setting_name)
|
|
134
126
|
|
|
135
|
-
vario_setting
|
|
136
|
-
vario_setting.levels.reject! { |level| conditions_hash == level.conditions_hash }
|
|
137
|
-
vario_setting.save!
|
|
127
|
+
vario_setting&.value_on!(context, nil)
|
|
138
128
|
|
|
139
|
-
|
|
129
|
+
setting_response(record, setting_name, context)
|
|
140
130
|
end
|
|
141
131
|
end
|
|
142
132
|
end
|
|
@@ -145,4 +135,3 @@ module Vario
|
|
|
145
135
|
end
|
|
146
136
|
end
|
|
147
137
|
end
|
|
148
|
-
|
data/lib/vario/backup.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# All settings and their refinements in a JSON file, and back, exactly as
|
|
5
|
+
# they were, so the migration can run again from the same data:
|
|
6
|
+
#
|
|
7
|
+
# bin/rails vario:backup FILE=vario_backup.json # before vario:migrate
|
|
8
|
+
# bin/rails vario:restore FILE=vario_backup.json # all settings as then
|
|
9
|
+
#
|
|
10
|
+
# The rows are read and written as stored, without the models (whose
|
|
11
|
+
# callbacks change them), and restoring replaces every setting: those
|
|
12
|
+
# added since go, those removed since come back, changes since are lost.
|
|
13
|
+
class Backup
|
|
14
|
+
VERSION = 1
|
|
15
|
+
TABLES = { 'settings' => -> { Setting }, 'refinements' => -> { Refinement } }.freeze
|
|
16
|
+
|
|
17
|
+
# The rows' counts by table
|
|
18
|
+
def self.write(path)
|
|
19
|
+
data = TABLES.transform_values { |model| rows(model.call) }
|
|
20
|
+
File.write(path, JSON.pretty_generate({ 'vario_backup' => VERSION, 'created_at' => Time.current.iso8601, **data }))
|
|
21
|
+
data.transform_values(&:size)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The rows' counts by table, before and after
|
|
25
|
+
def self.restore(path)
|
|
26
|
+
data = JSON.parse(File.read(path))
|
|
27
|
+
raise ArgumentError, "#{path} isn't a vario backup (version #{VERSION})" unless data['vario_backup'] == VERSION
|
|
28
|
+
|
|
29
|
+
before = TABLES.transform_values { |model| model.call.count }
|
|
30
|
+
ActiveRecord::Base.transaction do
|
|
31
|
+
Refinement.delete_all
|
|
32
|
+
Setting.delete_all
|
|
33
|
+
TABLES.each do |table, model|
|
|
34
|
+
columns = model.call.column_names
|
|
35
|
+
data.fetch(table).each_slice(1000) { |rows| model.call.insert_all(rows.map { |row| row.slice(*columns) }) }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
{ before: before, after: TABLES.transform_values { |model| model.call.count } }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# As stored: json as its data, arrays as arrays, times to the microsecond
|
|
43
|
+
def self.rows(model)
|
|
44
|
+
result = model.connection.select_all(model.unscoped.order(:id).to_sql)
|
|
45
|
+
result.cast_values.map do |values|
|
|
46
|
+
result.columns.zip(values).to_h { |column, value| [column, value.is_a?(Time) ? value.utc.iso8601(6) : value] }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/vario/card.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# What a card (vario/shared/_card) shows besides its title: the actions and
|
|
5
|
+
# tabs the block given to vario_card adds
|
|
6
|
+
class Card
|
|
7
|
+
Tab = Data.define(:id, :title, :padding, :content)
|
|
8
|
+
|
|
9
|
+
attr_reader :tabs, :actions
|
|
10
|
+
|
|
11
|
+
def initialize(view)
|
|
12
|
+
@view = view
|
|
13
|
+
@tabs = []
|
|
14
|
+
@actions = []
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def tab(id, title: nil, padding: true, &block)
|
|
18
|
+
@tabs << Tab.new(id: id.to_s.parameterize(separator: '_'), title: title || id.to_s.humanize, padding: padding, content: @view.capture(&block))
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def action(&block)
|
|
23
|
+
@actions << @view.capture(&block)
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/vario/config.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
module Vario
|
|
2
2
|
class Config
|
|
3
3
|
attr_writer :logger, :current_settable
|
|
4
|
-
attr_reader :keys, :settable_types, :settable_settings
|
|
5
|
-
attr_accessor :base_controller, :admin_layout
|
|
4
|
+
attr_reader :keys, :settable_types, :settable_settings, :context_defaults, :legacy_keys
|
|
5
|
+
attr_accessor :base_controller, :admin_layout, :topic_policy, :layout
|
|
6
6
|
|
|
7
7
|
def initialize
|
|
8
8
|
@logger = Logger.new(STDOUT)
|
|
@@ -15,6 +15,36 @@ module Vario
|
|
|
15
15
|
@current_settable = nil
|
|
16
16
|
@admin_layout = 'application'
|
|
17
17
|
@show_group_if = {}
|
|
18
|
+
@context_defaults = {}
|
|
19
|
+
@legacy_keys = {}
|
|
20
|
+
@topic_policy = 'Vario::TopicPolicy'
|
|
21
|
+
@layout = :standalone
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# How the settings pages sit in the host's layout (admin_layout):
|
|
25
|
+
# :standalone with their topics listed beside the topic page, :host
|
|
26
|
+
# without, for a host whose own menu lists them (vario_menu_items)
|
|
27
|
+
def standalone?
|
|
28
|
+
layout == :standalone
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The value of a context root when a setting is read without it, for the
|
|
32
|
+
# settings whose context has it:
|
|
33
|
+
#
|
|
34
|
+
# config.context_default :environment, -> { Rails.env }
|
|
35
|
+
def context_default(name, value)
|
|
36
|
+
context_defaults[name.to_s] = value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# 1.x's keys, as the variables of the context they are now, so settings
|
|
40
|
+
# read with them (customer_id: id) meet refinements on those:
|
|
41
|
+
#
|
|
42
|
+
# config.legacy_key :customer_id, 'order.customer'
|
|
43
|
+
#
|
|
44
|
+
# location_id needs none for location: a rule on location takes
|
|
45
|
+
# location_id when there is no location.
|
|
46
|
+
def legacy_key(key, variable)
|
|
47
|
+
legacy_keys[key.to_s] = variable.to_s
|
|
18
48
|
end
|
|
19
49
|
|
|
20
50
|
# Config setters
|
|
@@ -76,6 +106,10 @@ module Vario
|
|
|
76
106
|
@logger.is_a?(Proc) ? instance_exec(&@logger) : @logger
|
|
77
107
|
end
|
|
78
108
|
|
|
109
|
+
# The settable whose topics the settings pages show, which is always the
|
|
110
|
+
# current one:
|
|
111
|
+
#
|
|
112
|
+
# config.current_settable = -> { Current.account }
|
|
79
113
|
def current_settable
|
|
80
114
|
@current_settable.is_a?(Proc) ? instance_exec(&@current_settable) : @current_settable
|
|
81
115
|
end
|
|
@@ -99,14 +133,5 @@ module Vario
|
|
|
99
133
|
def default_keys?(settable_type)
|
|
100
134
|
settable_types.dig(settable_type, :default_keys) || []
|
|
101
135
|
end
|
|
102
|
-
|
|
103
|
-
def pre_create_settings(settable)
|
|
104
|
-
return unless settable_settings[settable.class.name]
|
|
105
|
-
|
|
106
|
-
settable_settings[settable.class.name].keys.each do |key|
|
|
107
|
-
s = settable.settings.find_or_initialize_by(name: key)
|
|
108
|
-
s.save unless s.persisted?
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
136
|
end
|
|
112
137
|
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# What a setting's refinements can look at, for the conditions editor: the
|
|
5
|
+
# variables of its context (Topic::Definition#context) a level at a time,
|
|
6
|
+
# as Liquidum's, and the records an object variable can be.
|
|
7
|
+
#
|
|
8
|
+
# variables = ContextVariables.new(definition.context)
|
|
9
|
+
# variables.at('') # location, order, order_type
|
|
10
|
+
# variables.at('order') # the order drop's variables
|
|
11
|
+
# variables.describe('order.customer') # => ["Order › Customer", "object"]
|
|
12
|
+
# variables.model('order.customer') # => Customer
|
|
13
|
+
#
|
|
14
|
+
# A root that is a model (location: Location) is an object without
|
|
15
|
+
# variables of its own; a drop's are Liquidum's.
|
|
16
|
+
class ContextVariables
|
|
17
|
+
def initialize(context)
|
|
18
|
+
@context = context.to_h { |name, root| [name.to_s, root] }
|
|
19
|
+
@models = @context.select { |_name, root| model?(root) }
|
|
20
|
+
@liquidum = Liquidum::Variables.new(@context.reject { |name, _root| @models.key?(name) })
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def at(path)
|
|
24
|
+
names = path.to_s.split('.')
|
|
25
|
+
return @context.keys.filter_map { |name| root(name) } if names.empty?
|
|
26
|
+
return [] if @models.key?(names.first)
|
|
27
|
+
|
|
28
|
+
@liquidum.at(path)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Its label, the labels on its way ("Order › Customer"), and type
|
|
32
|
+
def describe(path)
|
|
33
|
+
*parents, name = path.to_s.split('.')
|
|
34
|
+
labels = []
|
|
35
|
+
variable = nil
|
|
36
|
+
[*parents, name].each_with_index do |part, index|
|
|
37
|
+
variable = at(parents.first(index).join('.'))&.find { |candidate| candidate.name == part }
|
|
38
|
+
return [path, nil] unless variable
|
|
39
|
+
|
|
40
|
+
labels << variable.label
|
|
41
|
+
end
|
|
42
|
+
[labels.join(' › '), variable.type]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The model of an object variable, whose records it can be
|
|
46
|
+
def model(path)
|
|
47
|
+
names = path.to_s.split('.')
|
|
48
|
+
return @models[names.first] if names.size == 1 && @models.key?(names.first)
|
|
49
|
+
|
|
50
|
+
*parents, name = names
|
|
51
|
+
variable = at(parents.join('.'))&.find { |candidate| candidate.name == name }
|
|
52
|
+
drop = variable&.drop || (@context[name] if parents.empty?)
|
|
53
|
+
drop.model if drop.respond_to?(:model)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def model?(root)
|
|
59
|
+
root.is_a?(Class) && root < ActiveRecord::Base
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def root(name)
|
|
63
|
+
return @liquidum.at('').find { |variable| variable.name == name } unless @models.key?(name)
|
|
64
|
+
|
|
65
|
+
model = @models[name]
|
|
66
|
+
Liquidum::Variable.new(name: name, label: model.model_name.human, type: 'object', hint: nil, drop: nil, collection: false)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# Converts a 1.x setting's levels to its own value and refinements, so it
|
|
5
|
+
# reads the same:
|
|
6
|
+
#
|
|
7
|
+
# - the levels in order: those with conditions before the first one
|
|
8
|
+
# without become refinements (match all, an is rule per key), that one
|
|
9
|
+
# the value
|
|
10
|
+
# - the levels after it never applied in 1.x (the first without conditions
|
|
11
|
+
# matches everything): they become refinements too, disabled, with a note
|
|
12
|
+
# - keys become the variables they are now (Topic::Definition#variable_for)
|
|
13
|
+
# - values are cast to the setting's type
|
|
14
|
+
#
|
|
15
|
+
# conversion = Converter.new(setting).call
|
|
16
|
+
# conversion.dead # => 1
|
|
17
|
+
# conversion.apply!
|
|
18
|
+
#
|
|
19
|
+
# A setting no topic defines isn't converted (unknown?).
|
|
20
|
+
class Converter
|
|
21
|
+
DEAD_NOTE = 'Was niet actief in Vario 1'
|
|
22
|
+
|
|
23
|
+
def initialize(setting)
|
|
24
|
+
@setting = setting
|
|
25
|
+
@definition = setting.definition
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call
|
|
29
|
+
return Conversion.new(setting: @setting, value: nil, refinements: [], dead: 0, empty: 0, uncastable: [], unknown: true) unless @definition
|
|
30
|
+
|
|
31
|
+
value = nil
|
|
32
|
+
refinements = []
|
|
33
|
+
empty = 0
|
|
34
|
+
uncastable = []
|
|
35
|
+
cut = false
|
|
36
|
+
|
|
37
|
+
levels.each do |level|
|
|
38
|
+
conditions = conditions_of(level)
|
|
39
|
+
|
|
40
|
+
unless value_present?(level['value'])
|
|
41
|
+
# 1.x didn't save these; one without conditions still made the rest dead
|
|
42
|
+
empty += 1
|
|
43
|
+
cut ||= conditions.empty?
|
|
44
|
+
next
|
|
45
|
+
end
|
|
46
|
+
uncastable << level['value'] unless castable?(level['value'])
|
|
47
|
+
|
|
48
|
+
if cut || conditions.any?
|
|
49
|
+
refinements << { conditions: { 'match' => 'all', 'rules' => rules_for(conditions) }, value: level['value'], enabled: !cut, note: (DEAD_NOTE if cut) }
|
|
50
|
+
else
|
|
51
|
+
value = level['value']
|
|
52
|
+
cut = true
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
Conversion.new(setting: @setting, value: value, refinements: refinements, dead: refinements.count { |refinement| !refinement[:enabled] },
|
|
57
|
+
empty: empty, uncastable: uncastable, unknown: false)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def levels
|
|
63
|
+
Array(@setting.attributes['levels']).map { |level| level.to_h.stringify_keys }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# As 1.x saw them: of the setting's keys, the ones with a value
|
|
67
|
+
def conditions_of(level)
|
|
68
|
+
conditions = (level['conditions'] || {}).to_h.stringify_keys
|
|
69
|
+
keys = Array(@setting.keys).map(&:to_s)
|
|
70
|
+
conditions.slice(*keys).reject { |_key, value| value.blank? }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def rules_for(conditions)
|
|
74
|
+
conditions.map { |key, value| { 'variable' => @definition.variable_for(key), 'operator' => 'is', 'value' => value } }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def value_present?(value)
|
|
78
|
+
value == false || value.present?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Whether the cast keeps what it says, and what 1.x read: not "abc" as
|
|
82
|
+
# 0, "2,5" as 2, "off" as false (1.x knew only 0, 1, true and false) or
|
|
83
|
+
# a time that isn't one
|
|
84
|
+
def castable?(value)
|
|
85
|
+
return false if @definition.cast(value).nil?
|
|
86
|
+
return true unless value.is_a?(String)
|
|
87
|
+
|
|
88
|
+
case @definition.type
|
|
89
|
+
when :integer then value.match?(/\A\s*[+-]?\d+\s*\z/)
|
|
90
|
+
when :decimal then value.match?(/\A\s*[+-]?\d+(\.\d+)?\s*\z/)
|
|
91
|
+
when :boolean then %w[0 1 true false].include?(value)
|
|
92
|
+
else true
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# What a setting converts to (Converter), and what came up
|
|
98
|
+
Conversion = Data.define(:setting, :value, :refinements, :dead, :empty, :uncastable, :unknown) do
|
|
99
|
+
alias_method :unknown?, :unknown
|
|
100
|
+
|
|
101
|
+
# Its value and refinements, instead of those it has. Its levels stay as
|
|
102
|
+
# they are (1.x's callbacks on the setting would change them).
|
|
103
|
+
def apply!
|
|
104
|
+
raise Error, "Setting '#{setting.name}' is not defined for #{setting.settable_type}" if unknown?
|
|
105
|
+
|
|
106
|
+
Setting.transaction do
|
|
107
|
+
setting.refinements.destroy_all
|
|
108
|
+
setting.update_columns(value: setting.definition.serialize(value), updated_at: Time.current)
|
|
109
|
+
refinements.each_with_index { |refinement, index| setting.refinements.create!(refinement.merge(position: index + 1)) }
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
data/lib/vario/engine.rb
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
require 'importmap-rails'
|
|
2
|
+
require 'liquid'
|
|
3
|
+
require 'liquidum'
|
|
4
|
+
require 'entdec/view_components'
|
|
5
|
+
require 'positioning'
|
|
6
|
+
require 'pundit'
|
|
7
|
+
require 'form_components'
|
|
8
|
+
|
|
1
9
|
module Vario
|
|
2
10
|
class Engine < ::Rails::Engine
|
|
3
11
|
config.autoload_paths << File.expand_path("../../lib", __dir__)
|
|
@@ -11,5 +19,24 @@ module Vario
|
|
|
11
19
|
end
|
|
12
20
|
end
|
|
13
21
|
end
|
|
22
|
+
|
|
23
|
+
# Topics (Vario::Topic) in app/settings/ of the app and its engines, also
|
|
24
|
+
# after a reload
|
|
25
|
+
config.to_prepare do
|
|
26
|
+
Vario::Topic.load!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Not allowed (Vario::TopicPolicy) is forbidden, as Pundit suggests,
|
|
30
|
+
# unless the app rescues it itself
|
|
31
|
+
config.action_dispatch.rescue_responses['Pundit::NotAuthorizedError'] = :forbidden
|
|
32
|
+
|
|
33
|
+
initializer 'vario.assets' do |app|
|
|
34
|
+
app.config.assets.paths << root.join('app/javascript') if app.config.respond_to?(:assets)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
initializer 'vario.importmap', before: 'importmap' do |app|
|
|
38
|
+
app.config.importmap.paths << root.join('config/importmap.rb')
|
|
39
|
+
app.config.importmap.cache_sweepers << root.join('app/javascript')
|
|
40
|
+
end
|
|
14
41
|
end
|
|
15
42
|
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# Converts the 1.x settings (their levels) of all settables, or reports
|
|
5
|
+
# what that would do (Converter):
|
|
6
|
+
#
|
|
7
|
+
# bin/rails vario:migrate:report # what it would do, per settable
|
|
8
|
+
# bin/rails vario:migrate # do it
|
|
9
|
+
#
|
|
10
|
+
# Settings already converted (with a value or refinements) are skipped, so
|
|
11
|
+
# it can run again; FORCE=1 converts them again, which discards what was
|
|
12
|
+
# changed in 2.0 since.
|
|
13
|
+
class Migrator
|
|
14
|
+
def initialize(force: false)
|
|
15
|
+
@force = force
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The report, applied or not
|
|
19
|
+
def call(apply: false)
|
|
20
|
+
report = Report.new
|
|
21
|
+
|
|
22
|
+
Setting.where.not(levels: nil).includes(:settable, :refinements).find_each do |setting|
|
|
23
|
+
# Nothing to convert (1.x's create_on_request rows, 2.0's)
|
|
24
|
+
next if setting.attributes['levels'].blank?
|
|
25
|
+
|
|
26
|
+
if !@force && (!setting[:value].nil? || setting.refinements.any?)
|
|
27
|
+
report.add(setting, nil)
|
|
28
|
+
next
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
conversion = Converter.new(setting).call
|
|
32
|
+
conversion.apply! if apply && !conversion.unknown?
|
|
33
|
+
report.add(setting, conversion)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
report
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# What the migration did or would do, per settable
|
|
41
|
+
class Report
|
|
42
|
+
Line = Data.define(:name, :conversion)
|
|
43
|
+
|
|
44
|
+
def initialize
|
|
45
|
+
@lines = Hash.new { |hash, settable| hash[settable] = [] }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# conversion is nil for a setting already converted
|
|
49
|
+
def add(setting, conversion)
|
|
50
|
+
@lines[[setting.settable_type, setting.settable_id, setting.settable]] << Line.new(name: setting.name, conversion: conversion)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def totals
|
|
54
|
+
lines = @lines.values.flatten
|
|
55
|
+
conversions = lines.filter_map(&:conversion)
|
|
56
|
+
known = conversions.reject(&:unknown?)
|
|
57
|
+
|
|
58
|
+
{ settables: @lines.size, converted: known.size, dead: known.sum(&:dead), empty: known.sum(&:empty),
|
|
59
|
+
uncastable: known.sum { |conversion| conversion.uncastable.size }, unknown: conversions.count(&:unknown?), skipped: lines.count { |line| line.conversion.nil? } }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_s
|
|
63
|
+
output = @lines.sort_by { |(type, id, settable), _lines| [type.to_s, settable.try(:name).to_s, id.to_s] }.map do |(type, id, settable), lines|
|
|
64
|
+
title = [type, settable.try(:name), id && "(#{id})"].compact.join(' ')
|
|
65
|
+
conversions = lines.select(&:conversion)
|
|
66
|
+
known = conversions.reject { |line| line.conversion.unknown? }
|
|
67
|
+
|
|
68
|
+
[
|
|
69
|
+
title,
|
|
70
|
+
" converted #{known.size}, dead #{known.sum { |line| line.conversion.dead }}, empty #{known.sum { |line| line.conversion.empty }}, already converted #{lines.size - conversions.size}",
|
|
71
|
+
*known.select { |line| line.conversion.dead.positive? }.map { |line| " dead: #{line.name} (#{line.conversion.dead})" },
|
|
72
|
+
*known.flat_map { |line| line.conversion.uncastable.map { |value| " uncastable: #{line.name} #{value.inspect}" } },
|
|
73
|
+
*conversions.select { |line| line.conversion.unknown? }.map { |line| " unknown: #{line.name}" }
|
|
74
|
+
].join("\n")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
totals = self.totals
|
|
78
|
+
output << "Total: #{totals[:settables]} settables, #{totals[:converted]} settings converted, #{totals[:dead]} dead levels, " \
|
|
79
|
+
"#{totals[:empty]} empty, #{totals[:uncastable]} uncastable, #{totals[:unknown]} unknown, #{totals[:skipped]} already converted"
|
|
80
|
+
output.join("\n\n")
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|