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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus"
|
|
2
|
+
|
|
3
|
+
// The topics' list (topics/_nav), searched: the topics whose title has every
|
|
4
|
+
// word typed, and under their topic the settings whose label or description
|
|
5
|
+
// does, each word matching the start of a word. Without words, all topics,
|
|
6
|
+
// with the current one's settings. What matches is highlighted.
|
|
7
|
+
export default class extends Controller {
|
|
8
|
+
static targets = ["input", "topic", "setting", "label", "none"]
|
|
9
|
+
|
|
10
|
+
connect() {
|
|
11
|
+
this.labelTargets.forEach((label) => (label.dataset.text = label.textContent.trim()))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
filter() {
|
|
15
|
+
const words = this.inputTarget.value.toLowerCase().split(/\s+/).filter(Boolean)
|
|
16
|
+
const patterns = words.map((word) => new RegExp(`(^|[^\\p{L}\\p{N}])${escape(word)}`, "iu"))
|
|
17
|
+
const matches = (element) => patterns.every((pattern) => pattern.test(element.dataset.keywords))
|
|
18
|
+
|
|
19
|
+
let found = 0
|
|
20
|
+
this.topicTargets.forEach((topic) => {
|
|
21
|
+
const settings = this.settingTargets.filter((setting) => topic.contains(setting))
|
|
22
|
+
settings.forEach((setting) => {
|
|
23
|
+
setting.hidden = words.length === 0 ? !("current" in topic.dataset) : !matches(setting)
|
|
24
|
+
})
|
|
25
|
+
const shown = words.length === 0 || matches(topic) || settings.some((setting) => !setting.hidden)
|
|
26
|
+
topic.hidden = !shown
|
|
27
|
+
if (shown) found++
|
|
28
|
+
})
|
|
29
|
+
this.noneTargets.forEach((none) => (none.hidden = found > 0))
|
|
30
|
+
this.labelTargets.forEach((label) => highlight(label, words))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
clear() {
|
|
34
|
+
this.inputTarget.value = ""
|
|
35
|
+
this.filter()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function escape(text) {
|
|
40
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The label's text with the words, where they start a word, in a <mark>
|
|
44
|
+
function highlight(label, words) {
|
|
45
|
+
const text = label.dataset.text
|
|
46
|
+
label.replaceChildren()
|
|
47
|
+
if (words.length === 0) {
|
|
48
|
+
label.textContent = text
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const pattern = new RegExp(`(^|[^\\p{L}\\p{N}])(${words.map(escape).join("|")})`, "giu")
|
|
53
|
+
let last = 0
|
|
54
|
+
for (const match of text.matchAll(pattern)) {
|
|
55
|
+
const start = match.index + match[1].length
|
|
56
|
+
label.append(text.slice(last, start))
|
|
57
|
+
const mark = document.createElement("mark")
|
|
58
|
+
mark.textContent = match[2]
|
|
59
|
+
label.append(mark)
|
|
60
|
+
last = start + match[2].length
|
|
61
|
+
}
|
|
62
|
+
label.append(text.slice(last))
|
|
63
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus"
|
|
2
|
+
|
|
3
|
+
// A list whose items are dragged by their handle to another place, with the
|
|
4
|
+
// browser's own drag and drop. Dropped, the list fires vario--sortable:sorted,
|
|
5
|
+
// for the form that saves the order (vario--refinements numbers them).
|
|
6
|
+
//
|
|
7
|
+
// div data-controller="vario--sortable"
|
|
8
|
+
// div data-vario--sortable-target="item"
|
|
9
|
+
// button data-vario--sortable-target="handle"
|
|
10
|
+
export default class extends Controller {
|
|
11
|
+
static targets = ["item", "handle"]
|
|
12
|
+
|
|
13
|
+
connect() {
|
|
14
|
+
this.element.addEventListener("dragover", this.dragOver)
|
|
15
|
+
this.element.addEventListener("drop", this.drop)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
disconnect() {
|
|
19
|
+
this.element.removeEventListener("dragover", this.dragOver)
|
|
20
|
+
this.element.removeEventListener("drop", this.drop)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Only the handle starts a drag, so the item's fields stay usable
|
|
24
|
+
handleTargetConnected(handle) {
|
|
25
|
+
const item = this.itemTargets.find((item) => item.contains(handle))
|
|
26
|
+
if (!item) return
|
|
27
|
+
|
|
28
|
+
handle.addEventListener("mousedown", () => (item.draggable = true))
|
|
29
|
+
item.addEventListener("dragstart", (event) => {
|
|
30
|
+
this.dragged = item
|
|
31
|
+
this.dropped = false
|
|
32
|
+
this.origin = [item.parentNode, item.nextSibling]
|
|
33
|
+
event.dataTransfer.effectAllowed = "move"
|
|
34
|
+
})
|
|
35
|
+
// Dropped elsewhere: back where it was
|
|
36
|
+
item.addEventListener("dragend", () => {
|
|
37
|
+
if (!this.dropped) this.origin[0].insertBefore(item, this.origin[1])
|
|
38
|
+
item.draggable = false
|
|
39
|
+
this.dragged = null
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
dragOver = (event) => {
|
|
44
|
+
if (!this.dragged) return
|
|
45
|
+
|
|
46
|
+
event.preventDefault()
|
|
47
|
+
const over = this.itemTargets.find((item) => item !== this.dragged && item.contains(event.target))
|
|
48
|
+
if (!over) return
|
|
49
|
+
|
|
50
|
+
const { top, height } = over.getBoundingClientRect()
|
|
51
|
+
over.parentNode.insertBefore(this.dragged, event.clientY < top + height / 2 ? over : over.nextSibling)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
drop = (event) => {
|
|
55
|
+
if (!this.dragged) return
|
|
56
|
+
|
|
57
|
+
event.preventDefault()
|
|
58
|
+
this.dropped = true
|
|
59
|
+
this.dispatch("sorted", { detail: { item: this.dragged } })
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus"
|
|
2
|
+
|
|
3
|
+
// The tabs of a card (shared/_card): one panel at a time
|
|
4
|
+
//
|
|
5
|
+
// div data-controller="vario--tabs"
|
|
6
|
+
// button data-vario--tabs-target="tab" data-action="vario--tabs#select"
|
|
7
|
+
// div data-vario--tabs-target="panel"
|
|
8
|
+
export default class extends Controller {
|
|
9
|
+
static targets = ["tab", "panel"]
|
|
10
|
+
|
|
11
|
+
select(event) {
|
|
12
|
+
this.show(this.tabTargets.indexOf(event.currentTarget))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
show(index) {
|
|
16
|
+
this.tabTargets.forEach((tab, i) => tab.setAttribute("aria-selected", i === index))
|
|
17
|
+
this.panelTargets.forEach((panel, i) => (panel.hidden = i !== index))
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus"
|
|
2
|
+
|
|
3
|
+
// A topic's form (topics/show): once something changed, its save bar says
|
|
4
|
+
// so and can cancel, and leaving the page asks first, also by a Turbo visit
|
|
5
|
+
// (which doesn't unload it). Cancel leaves without asking.
|
|
6
|
+
export default class extends Controller {
|
|
7
|
+
static targets = ["unsaved", "cancel"]
|
|
8
|
+
static values = { confirm: String }
|
|
9
|
+
|
|
10
|
+
connect() {
|
|
11
|
+
this.beforeUnload = (event) => {
|
|
12
|
+
if (this.leaving()) event.preventDefault()
|
|
13
|
+
}
|
|
14
|
+
this.beforeVisit = (event) => {
|
|
15
|
+
if (this.leaving() && !window.confirm(this.confirmValue)) event.preventDefault()
|
|
16
|
+
}
|
|
17
|
+
window.addEventListener("beforeunload", this.beforeUnload)
|
|
18
|
+
document.addEventListener("turbo:before-visit", this.beforeVisit)
|
|
19
|
+
this.element.addEventListener("submit", () => (this.submitting = true))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
disconnect() {
|
|
23
|
+
window.removeEventListener("beforeunload", this.beforeUnload)
|
|
24
|
+
document.removeEventListener("turbo:before-visit", this.beforeVisit)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
changed() {
|
|
28
|
+
this.dirty = true
|
|
29
|
+
this.unsavedTargets.forEach((target) => (target.hidden = false))
|
|
30
|
+
this.cancelTargets.forEach((target) => (target.hidden = false))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
discard() {
|
|
34
|
+
this.dirty = false
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
leaving() {
|
|
38
|
+
return this.dirty && !this.submitting
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# When a refinement applies: "if [all] of the following conditions are met:
|
|
5
|
+
# [location] [is] [Rotterdam]". A rule's variable is a path in the context
|
|
6
|
+
# a setting is read with (order.business_model), compared with Mensa's
|
|
7
|
+
# operators. Ported from Nuntius::Conditions, with the same format and the
|
|
8
|
+
# same cases (test/fixtures/files/conditions_cases.json, from flow forms).
|
|
9
|
+
#
|
|
10
|
+
# Stored as {"match" => "all" | "any", "rules" => [{"variable", "operator",
|
|
11
|
+
# "value", "value_to"}]}, value_to only for between.
|
|
12
|
+
#
|
|
13
|
+
# A variable that is a record or a drop (location, order.customer) is its
|
|
14
|
+
# id, so a rule on it stores the id; without it, <variable>_id counts, so
|
|
15
|
+
# setting(..., location_id: id) meets a rule on location too.
|
|
16
|
+
class Conditions
|
|
17
|
+
MATCHES = %w[all any].freeze
|
|
18
|
+
OPERATORS = %w[is isnt matches does_not_match gt gteq lt lteq between is_empty isnt_empty].freeze
|
|
19
|
+
WITHOUT_VALUE = %w[is_empty isnt_empty].freeze
|
|
20
|
+
TRUE_VALUES = %w[true yes ja 1 y j].freeze
|
|
21
|
+
|
|
22
|
+
Rule = Data.define(:variable, :operator, :value, :value_to)
|
|
23
|
+
|
|
24
|
+
attr_reader :match, :rules
|
|
25
|
+
|
|
26
|
+
# data: the stored hash, or the form's (rules may be numbered)
|
|
27
|
+
def initialize(data = {})
|
|
28
|
+
data = data.to_h.deep_stringify_keys
|
|
29
|
+
rules = data['rules']
|
|
30
|
+
rules = rules.values if rules.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
@match = MATCHES.include?(data['match']) ? data['match'] : 'all'
|
|
33
|
+
@rules = Array(rules).filter_map do |rule|
|
|
34
|
+
rule = rule.to_h.stringify_keys
|
|
35
|
+
next if rule['variable'].blank? || !OPERATORS.include?(rule['operator'])
|
|
36
|
+
|
|
37
|
+
Rule.new(variable: rule['variable'].strip, operator: rule['operator'], value: rule['value'].to_s.strip, value_to: rule['value_to'].to_s.strip)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_h
|
|
42
|
+
{ 'match' => match, 'rules' => rules.map { |rule| rule.to_h.stringify_keys } }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def empty?
|
|
46
|
+
rules.empty?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Whether the context a setting is read with meets them: its assigns, or
|
|
50
|
+
# a Lookup of them, to share between the refinements of one read
|
|
51
|
+
def met?(assigns)
|
|
52
|
+
return true if empty?
|
|
53
|
+
|
|
54
|
+
lookup = assigns.is_a?(Lookup) ? assigns : Lookup.new(assigns)
|
|
55
|
+
results = rules.lazy.map { |rule| compare(rule, lookup.value(rule.variable)) }
|
|
56
|
+
(match == 'any') ? results.any? : results.all?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The value of a variable in a context. Plain hashes are walked here, and
|
|
60
|
+
# a record or drop at the end of a path is its id, so rules on ids and
|
|
61
|
+
# scalars never build a drop. A path into a record or drop goes through
|
|
62
|
+
# Liquid, built once, which only calls what a drop allows: a record
|
|
63
|
+
# without a drop (to_liquid) gives nothing.
|
|
64
|
+
class Lookup
|
|
65
|
+
def initialize(assigns)
|
|
66
|
+
@assigns = assigns.to_h.deep_stringify_keys
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def value(variable)
|
|
70
|
+
value = at(variable)
|
|
71
|
+
value = at("#{variable}_id") if value.nil?
|
|
72
|
+
identify(value)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def at(variable)
|
|
78
|
+
names = variable.split('.')
|
|
79
|
+
value = @assigns
|
|
80
|
+
names.each do |name|
|
|
81
|
+
case value
|
|
82
|
+
when Hash then value = value[name]
|
|
83
|
+
when nil then return nil
|
|
84
|
+
else return liquid(variable)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
value
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def liquid(variable)
|
|
91
|
+
@context ||= Liquid::Context.new([@assigns])
|
|
92
|
+
value = @context[variable]
|
|
93
|
+
value.respond_to?(:to_liquid) ? value.to_liquid : value
|
|
94
|
+
rescue StandardError
|
|
95
|
+
nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# A record or drop is its id, also in a collection
|
|
99
|
+
def identify(value)
|
|
100
|
+
case value
|
|
101
|
+
when Array then value.map { |element| identify(element) }
|
|
102
|
+
when Liquid::Drop then value.invoke_drop('id')
|
|
103
|
+
else (value.respond_to?(:to_key) && value.respond_to?(:id)) ? value.id : value
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
private
|
|
109
|
+
|
|
110
|
+
def compare(rule, actual)
|
|
111
|
+
case rule.operator
|
|
112
|
+
when 'is_empty' then empty_value?(actual)
|
|
113
|
+
when 'isnt_empty' then !empty_value?(actual)
|
|
114
|
+
when 'is' then equal?(actual, rule.value)
|
|
115
|
+
when 'isnt' then !equal?(actual, rule.value)
|
|
116
|
+
when 'matches' then text(actual).include?(rule.value.downcase)
|
|
117
|
+
when 'does_not_match' then !text(actual).include?(rule.value.downcase)
|
|
118
|
+
when 'gt' then order(actual, rule.value)&.positive?
|
|
119
|
+
when 'gteq' then order(actual, rule.value)&.>=(0)
|
|
120
|
+
when 'lt' then order(actual, rule.value)&.negative?
|
|
121
|
+
when 'lteq' then order(actual, rule.value)&.<=(0)
|
|
122
|
+
when 'between'
|
|
123
|
+
from = order(actual, rule.value)
|
|
124
|
+
to = order(actual, rule.value_to)
|
|
125
|
+
from && to && from >= 0 && to <= 0
|
|
126
|
+
end || false
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def empty_value?(actual)
|
|
130
|
+
actual.nil? || (actual.respond_to?(:empty?) && actual.empty?)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# A collection is one of its elements; true and false also as yes or ja
|
|
134
|
+
def equal?(actual, value)
|
|
135
|
+
return actual.any? { |element| equal?(element, value) } if actual.is_a?(Array)
|
|
136
|
+
return actual == TRUE_VALUES.include?(value.downcase) if [true, false].include?(actual)
|
|
137
|
+
|
|
138
|
+
order(actual, value)&.zero? || text(actual) == value.downcase
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def text(actual)
|
|
142
|
+
Array(actual).join(', ').downcase
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# actual <=> value, with value as a number, date or time when actual is
|
|
146
|
+
def order(actual, value)
|
|
147
|
+
other = case actual
|
|
148
|
+
when Numeric then BigDecimal(value)
|
|
149
|
+
when ActiveSupport::TimeWithZone, Time, DateTime then Time.zone.parse(value)
|
|
150
|
+
when Date then Date.parse(value)
|
|
151
|
+
when String then value
|
|
152
|
+
end
|
|
153
|
+
other.nil? ? nil : actual <=> other
|
|
154
|
+
rescue ArgumentError, TypeError
|
|
155
|
+
nil
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# A more specific value of a setting: its value applies when its conditions
|
|
5
|
+
# (Vario::Conditions) are met, the first enabled one by position
|
|
6
|
+
class Refinement < ApplicationRecord
|
|
7
|
+
belongs_to :setting, inverse_of: :refinements
|
|
8
|
+
|
|
9
|
+
positioned on: :setting
|
|
10
|
+
|
|
11
|
+
before_validation :serialize_value
|
|
12
|
+
|
|
13
|
+
delegate :definition, to: :setting, allow_nil: true
|
|
14
|
+
|
|
15
|
+
# Its value, as the setting's type
|
|
16
|
+
def value
|
|
17
|
+
definition ? definition.read(self[:value]) { setting.settable } : self[:value]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Its conditions, read once
|
|
21
|
+
def rules
|
|
22
|
+
@rules = nil unless @rules_for.equal?(conditions)
|
|
23
|
+
@rules_for = conditions
|
|
24
|
+
@rules ||= Conditions.new(conditions)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A record is its id (the jsonb column would take it as JSON)
|
|
28
|
+
def value=(value)
|
|
29
|
+
super(value.is_a?(ActiveRecord::Base) ? value.id : value)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def serialize_value
|
|
35
|
+
self[:value] = definition.serialize(self[:value]) if definition
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/app/models/vario/setting.rb
CHANGED
|
@@ -4,18 +4,75 @@ module Vario
|
|
|
4
4
|
class Setting < ApplicationRecord
|
|
5
5
|
attr_reader :settable_setting, :type, :default, :description
|
|
6
6
|
|
|
7
|
-
has_paper_trail if respond_to?(:has_paper_trail)
|
|
8
|
-
|
|
9
7
|
belongs_to :settable, polymorphic: true, optional: true
|
|
10
|
-
|
|
8
|
+
has_many :refinements, -> { order(:position) }, inverse_of: :setting
|
|
9
|
+
accepts_nested_attributes_for :refinements, allow_destroy: true
|
|
10
|
+
|
|
11
11
|
validate :validate_levels
|
|
12
12
|
|
|
13
13
|
after_initialize :configure, :levels # pre-init the levels
|
|
14
|
+
before_validation :serialize_value
|
|
14
15
|
before_save :update_level_data
|
|
15
16
|
|
|
16
17
|
scope :for_group, ->(group_name) { where('name ilike ?', "#{group_name}.%") }
|
|
17
18
|
scope :without_group, -> { where.not('name ilike ?', "%.%") }
|
|
18
19
|
|
|
20
|
+
# How the setting is defined, in its settable's topics (Vario::Topic)
|
|
21
|
+
def definition
|
|
22
|
+
return unless settable_type && name
|
|
23
|
+
|
|
24
|
+
@definition = nil unless @definition_for == [settable_type, name]
|
|
25
|
+
@definition_for = [settable_type, name]
|
|
26
|
+
@definition ||= Vario::Topic.definition(settable_type.safe_constantize, name)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The settable's own value, as the setting's type
|
|
30
|
+
def value
|
|
31
|
+
definition ? definition.read(self[:value]) { settable } : self[:value]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A record is its id (the jsonb column would take it as JSON)
|
|
35
|
+
def value=(value)
|
|
36
|
+
super(value.is_a?(ActiveRecord::Base) ? value.id : value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The value on exactly a context (1.x's keys: customer_id: 5), as a
|
|
40
|
+
# refinement with an is rule per key: without keys its own value. A new
|
|
41
|
+
# refinement goes first, as 1.x's levels did; nothing removes it.
|
|
42
|
+
#
|
|
43
|
+
# setting.value_on!({ 'customer_id' => '5' }, 'false')
|
|
44
|
+
def value_on!(context, value)
|
|
45
|
+
transaction do
|
|
46
|
+
next update!(value: value) if context.empty?
|
|
47
|
+
|
|
48
|
+
save! if new_record?
|
|
49
|
+
refinement = refinement_on(context)
|
|
50
|
+
if Types.blank?(value)
|
|
51
|
+
refinement&.destroy!
|
|
52
|
+
else
|
|
53
|
+
refinement ||= refinements.build(position: :first, conditions: { 'match' => 'all', 'rules' => rules_on(context) })
|
|
54
|
+
refinement.update!(value: value)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The refinement whose rules are exactly an is per key of a context
|
|
60
|
+
def refinement_on(context)
|
|
61
|
+
expected = Setting.equal_rules(rules_on(context))
|
|
62
|
+
refinements.find do |refinement|
|
|
63
|
+
conditions = refinement.conditions.to_h.stringify_keys
|
|
64
|
+
conditions['match'] == 'all' && Setting.equal_rules(conditions['rules']) == expected
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def rules_on(context)
|
|
69
|
+
context.map { |key, value| { 'variable' => definition.variable_for(key), 'operator' => 'is', 'value' => value.to_s } }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.equal_rules(rules)
|
|
73
|
+
Array(rules).map { |rule| rule.to_h.stringify_keys.values_at('variable', 'operator', 'value').tap { |values| values[2] = values[2].to_s } }.sort
|
|
74
|
+
end
|
|
75
|
+
|
|
19
76
|
def group_name
|
|
20
77
|
return nil if name.blank?
|
|
21
78
|
|
|
@@ -165,6 +222,10 @@ module Vario
|
|
|
165
222
|
|
|
166
223
|
private
|
|
167
224
|
|
|
225
|
+
def serialize_value
|
|
226
|
+
self[:value] = definition.serialize(self[:value]) if definition
|
|
227
|
+
end
|
|
228
|
+
|
|
168
229
|
###
|
|
169
230
|
# Fill blank values for which we've got a value_proc
|
|
170
231
|
#
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# Who may see a topic of a settable and change its settings: everyone,
|
|
5
|
+
# unless the app says otherwise, for all topics (config.topic_policy) or
|
|
6
|
+
# for one (a policy named after it):
|
|
7
|
+
#
|
|
8
|
+
# class SettingsPolicy < Vario::TopicPolicy
|
|
9
|
+
# def show?
|
|
10
|
+
# settable == Current.account
|
|
11
|
+
# end
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# class ModulesSettingsPolicy < SettingsPolicy
|
|
15
|
+
# def show?
|
|
16
|
+
# super && user.super_user?
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
class TopicPolicy
|
|
20
|
+
attr_reader :user, :record
|
|
21
|
+
|
|
22
|
+
delegate :settable, :topic, to: :record
|
|
23
|
+
|
|
24
|
+
def initialize(user, record)
|
|
25
|
+
@user = user
|
|
26
|
+
@record = record
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def show?
|
|
30
|
+
true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def update?
|
|
34
|
+
show?
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<%# A card: its title and actions over its tabs, or its content. Locals: name (its id), title, icon, card
|
|
2
|
+
(Vario::Card, with the tabs and actions), body (the content of a card without tabs) %>
|
|
3
|
+
<div class="vario-card" id="<%= name %>" data-controller="vario--tabs">
|
|
4
|
+
<div class="vario-card__header">
|
|
5
|
+
<% if icon %>
|
|
6
|
+
<i class="vario-card__icon <%= icon %>"></i>
|
|
7
|
+
<% end %>
|
|
8
|
+
<div class="vario-card__title"><%= title %></div>
|
|
9
|
+
<% if card.actions.any? %>
|
|
10
|
+
<div class="vario-card__actions"><%= safe_join(card.actions) %></div>
|
|
11
|
+
<% end %>
|
|
12
|
+
</div>
|
|
13
|
+
<% if card.tabs.any? %>
|
|
14
|
+
<div class="vario-card__tabs" role="tablist">
|
|
15
|
+
<% card.tabs.each_with_index do |tab, index| %>
|
|
16
|
+
<button type="button" role="tab" class="vario-card__tab" id="<%= name %>_<%= tab.id %>_tab" aria-controls="<%= name %>_<%= tab.id %>" aria-selected="<%= index.zero? %>" data-vario--tabs-target="tab" data-action="vario--tabs#select"><%= tab.title %></button>
|
|
17
|
+
<% end %>
|
|
18
|
+
</div>
|
|
19
|
+
<% card.tabs.each_with_index do |tab, index| %>
|
|
20
|
+
<%= tag.div tab.content, role: 'tabpanel', id: "#{name}_#{tab.id}", class: ('vario-card__body' if tab.padding), hidden: !index.zero?, 'aria-labelledby': "#{name}_#{tab.id}_tab", data: { 'vario--tabs-target': 'panel' } %>
|
|
21
|
+
<% end %>
|
|
22
|
+
<% else %>
|
|
23
|
+
<div class="vario-card__body"><%= body %></div>
|
|
24
|
+
<% end %>
|
|
25
|
+
</div>
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<%# What applies: a context to choose, and for every setting what it is in that context, and why. A test bench for the refinements, as saved %>
|
|
2
|
+
<% variables = Vario::ContextVariables.new(topic.topic.context) %>
|
|
3
|
+
<section class="vario-applies" id="applies" aria-labelledby="applies_title">
|
|
4
|
+
<h2 id="applies_title"><%= t('vario.pages.applies.title') %></h2>
|
|
5
|
+
<p class="vario-applies__intro"><%= t('vario.pages.applies.intro') %></p>
|
|
6
|
+
|
|
7
|
+
<%= form_with url: topic_path(topic.key), method: :get, builder: FormComponents::FormBuilder, scope: :applies, class: 'vario-applies__form', data: { turbo: false } do |form| %>
|
|
8
|
+
<% topic.topic.context.each_key do |root| %>
|
|
9
|
+
<% label = variables.describe(root).first %>
|
|
10
|
+
<% model = variables.model(root) %>
|
|
11
|
+
<% selected = params.dig(:applies, root) %>
|
|
12
|
+
<% if model %>
|
|
13
|
+
<%= form.select root, Vario::Topic.records_of(model, settable).map { |record| [Vario::Topic.label_of(record), record.id.to_s] }, { include_blank: true, selected: selected, label: label } %>
|
|
14
|
+
<% else %>
|
|
15
|
+
<%= form.text_field root, value: selected, label: label %>
|
|
16
|
+
<% end %>
|
|
17
|
+
<% end %>
|
|
18
|
+
<%= form.submit t('vario.pages.applies.show'), class: 'vario-button', name: nil %>
|
|
19
|
+
<% end %>
|
|
20
|
+
|
|
21
|
+
<% if applies %>
|
|
22
|
+
<table class="vario-applies__table">
|
|
23
|
+
<thead>
|
|
24
|
+
<tr>
|
|
25
|
+
<th scope="col"><%= t('vario.pages.applies.setting') %></th>
|
|
26
|
+
<th scope="col"><%= t('vario.pages.applies.value') %></th>
|
|
27
|
+
<th scope="col"><%= t('vario.pages.applies.why') %></th>
|
|
28
|
+
</tr>
|
|
29
|
+
</thead>
|
|
30
|
+
<tbody>
|
|
31
|
+
<% topic.topic.definitions.each_value do |definition| %>
|
|
32
|
+
<% resolution = settable.explain(definition.name, applies) %>
|
|
33
|
+
<tr>
|
|
34
|
+
<th scope="row"><%= definition.label %></th>
|
|
35
|
+
<td><%= definition.human(resolution.value, settable).presence || t('vario.pages.applies.nothing') %></td>
|
|
36
|
+
<td>
|
|
37
|
+
<% if resolution.refinement %>
|
|
38
|
+
<%= t('vario.pages.applies.refinement', priority: resolution.refinement.position,
|
|
39
|
+
summary: vario_refinement_summary(resolution.refinement, definition, settable, Vario::ContextVariables.new(definition.context)).split(' → ').first) %>
|
|
40
|
+
<% else %>
|
|
41
|
+
<%= t(resolution.source == :value ? 'vario.pages.applies.value_source' : 'vario.pages.applies.default') %>
|
|
42
|
+
<% end %>
|
|
43
|
+
</td>
|
|
44
|
+
</tr>
|
|
45
|
+
<% end %>
|
|
46
|
+
</tbody>
|
|
47
|
+
</table>
|
|
48
|
+
<% end %>
|
|
49
|
+
</section>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<%# The settable's topics, each with its settings as links to their section: those of the current topic, or, searched, those whose label or description matches %>
|
|
2
|
+
<nav class="vario-topics__nav" aria-label="<%= t('vario.pages.nav.label') %>" data-controller="vario--search">
|
|
3
|
+
<div class="vario-topics__find">
|
|
4
|
+
<i class="fa-regular fa-magnifying-glass vario-topics__find-icon" aria-hidden="true"></i>
|
|
5
|
+
<input type="search" class="vario-topics__search" placeholder="<%= t('vario.pages.nav.search') %>" aria-label="<%= t('vario.pages.nav.search') %>" data-action="input->vario--search#filter keydown.esc->vario--search#clear" data-vario--search-target="input">
|
|
6
|
+
</div>
|
|
7
|
+
<ul class="vario-topics__list">
|
|
8
|
+
<% topics.each do |topic| %>
|
|
9
|
+
<% is_current = topic == current %>
|
|
10
|
+
<li data-vario--search-target="topic" data-keywords="<%= topic.title %>" <%= 'data-current' if is_current %>>
|
|
11
|
+
<%= link_to topic_path(topic.key), class: 'vario-topics__link', aria: { current: (is_current ? 'page' : nil) } do %>
|
|
12
|
+
<i class="<%= topic.topic.icon || 'fa-regular fa-sliders' %> fa-fw vario-topics__icon" aria-hidden="true"></i>
|
|
13
|
+
<span data-vario--search-target="label"><%= topic.title %></span>
|
|
14
|
+
<% end %>
|
|
15
|
+
<ul class="vario-topics__settings">
|
|
16
|
+
<% topic.topic.definitions.each_value do |definition| %>
|
|
17
|
+
<li <%= 'hidden' unless is_current %> data-vario--search-target="setting" data-keywords="<%= [definition.label, definition.description].compact.join(' ') %>">
|
|
18
|
+
<%= link_to topic_path(topic.key, anchor: "setting_#{definition.key}"), class: 'vario-topics__setting' do %>
|
|
19
|
+
<span data-vario--search-target="label"><%= definition.label %></span>
|
|
20
|
+
<% end %>
|
|
21
|
+
</li>
|
|
22
|
+
<% end %>
|
|
23
|
+
</ul>
|
|
24
|
+
</li>
|
|
25
|
+
<% end %>
|
|
26
|
+
</ul>
|
|
27
|
+
<p class="vario-topics__none" hidden data-vario--search-target="none"><%= t('vario.pages.nav.none') %></p>
|
|
28
|
+
</nav>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<%# A setting's refinements: the first enabled one whose conditions are met gives its value. In order of priority, dragged by their handles; closed to a line until opened %>
|
|
2
|
+
<% variables = Vario::ContextVariables.new(definition.context) %>
|
|
3
|
+
<% urls = { variables_url: variables_topic_path(topic.key, setting: definition.key),
|
|
4
|
+
records_url: records_topic_path(topic.key, setting: definition.key) } %>
|
|
5
|
+
<div class="vario-refinements" data-controller="vario--refinements vario--sortable"
|
|
6
|
+
data-action="vario--sortable:sorted->vario--refinements#renumber nested-form:add->vario--refinements#renumber nested-form:remove->vario--refinements#renumber">
|
|
7
|
+
<p class="vario-refinements__head">
|
|
8
|
+
<%= t('vario.pages.refinements.title') %>
|
|
9
|
+
(<span data-vario--refinements-target="count"><%= setting.refinements.reject(&:marked_for_destruction?).size %></span>)
|
|
10
|
+
<span class="vario-refinements__hint"><%= t('vario.pages.refinements.hint') %></span>
|
|
11
|
+
</p>
|
|
12
|
+
<%= f.nested_form :refinements, wrapper_selector: '.vario-refinement', new_record: Vario::Refinement.new, add_label: t('vario.pages.refinements.add') do |nf| %>
|
|
13
|
+
<% nf.with_item do |rf| %>
|
|
14
|
+
<% refinement = rf.object %>
|
|
15
|
+
<% open = refinement.new_record? || refinement.errors.any? %>
|
|
16
|
+
<div class="vario-refinement" data-vario--sortable-target="item" data-vario--refinements-target="item" data-new-record="<%= refinement.new_record? %>">
|
|
17
|
+
<%= rf.hidden_field :position, data: { 'vario--refinements-target': 'position' } %>
|
|
18
|
+
<%= rf.hidden_field :_destroy %>
|
|
19
|
+
<div class="vario-refinement__bar">
|
|
20
|
+
<button type="button" class="vario-refinement__grip" data-vario--sortable-target="handle" title="<%= t('vario.pages.refinements.drag') %>" aria-label="<%= t('vario.pages.refinements.drag') %>">
|
|
21
|
+
<span aria-hidden="true">⠿</span>
|
|
22
|
+
<span data-vario--refinements-target="priority"><%= refinement.position %></span>
|
|
23
|
+
</button>
|
|
24
|
+
<button type="button" class="vario-refinement__summary" aria-expanded="<%= open %>" data-action="vario--refinements#toggle">
|
|
25
|
+
<%= refinement.new_record? ? t('vario.pages.refinements.new') : vario_refinement_summary(refinement, definition, settable, variables) %>
|
|
26
|
+
</button>
|
|
27
|
+
<%= rf.check_box :enabled, label: t('vario.pages.refinements.enabled') %>
|
|
28
|
+
<button type="button" class="vario-refinement__remove" data-action="form-components--nested-form#remove" title="<%= t('vario.pages.refinements.remove') %>" aria-label="<%= t('vario.pages.refinements.remove') %>">×</button>
|
|
29
|
+
</div>
|
|
30
|
+
<div class="vario-refinement__detail" <%= 'hidden' unless open %>>
|
|
31
|
+
<%= vario_conditions(rf, definition, settable, variables, **urls) %>
|
|
32
|
+
<div class="vario-refinement__then">
|
|
33
|
+
<span><%= t('vario.pages.refinements.then') %></span>
|
|
34
|
+
<%= vario_value_field(rf, definition, settable, label: t('vario.pages.refinements.value'), hint: false) %>
|
|
35
|
+
</div>
|
|
36
|
+
<%= rf.text_field :note, label: t('vario.pages.refinements.note') %>
|
|
37
|
+
</div>
|
|
38
|
+
</div>
|
|
39
|
+
<% end %>
|
|
40
|
+
<% end %>
|
|
41
|
+
</div>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<%# A setting: its label and description beside its own value and, when it has a context, its refinements %>
|
|
2
|
+
<%= fields_for "settings[#{definition.key}]", setting, builder: FormComponents::FormBuilder do |f| %>
|
|
3
|
+
<%= f.fieldset definition.label, description: definition.description, legend_position: :beside, class: 'vario-setting', id: "setting_#{definition.key}" do %>
|
|
4
|
+
<%= vario_value_field(f, definition, settable) %>
|
|
5
|
+
<% if definition.context.any? %>
|
|
6
|
+
<%= render 'vario/topics/refinements', f: f, definition: definition, setting: setting, settable: settable, topic: topic %>
|
|
7
|
+
<% end %>
|
|
8
|
+
<% end %>
|
|
9
|
+
<% end %>
|