vario 1.0.9 → 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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +68 -1
  3. data/Rakefile +0 -4
  4. data/app/assets/stylesheets/vario/application.css +316 -0
  5. data/app/assets/tailwind/vario/engine.css +6 -0
  6. data/app/controllers/vario/application_controller.rb +9 -2
  7. data/app/controllers/vario/topics_controller.rb +131 -0
  8. data/app/controllers/vario/values_controller.rb +27 -0
  9. data/app/javascript/vario/application.js +18 -0
  10. data/app/javascript/vario/controllers/refinements_controller.js +29 -0
  11. data/app/javascript/vario/controllers/search_controller.js +63 -0
  12. data/app/javascript/vario/controllers/sortable_controller.js +61 -0
  13. data/app/javascript/vario/controllers/tabs_controller.js +19 -0
  14. data/app/javascript/vario/controllers/topic_controller.js +40 -0
  15. data/app/models/vario/conditions.rb +158 -0
  16. data/app/models/vario/refinement.rb +38 -0
  17. data/app/models/vario/setting.rb +64 -3
  18. data/app/policies/vario/topic_policy.rb +37 -0
  19. data/app/views/vario/shared/_card.html.erb +25 -0
  20. data/app/views/vario/topics/_applies.html.erb +49 -0
  21. data/app/views/vario/topics/_nav.html.erb +28 -0
  22. data/app/views/vario/topics/_refinements.html.erb +41 -0
  23. data/app/views/vario/topics/_setting.html.erb +9 -0
  24. data/app/views/vario/topics/index.html.erb +3 -0
  25. data/app/views/vario/topics/show.html.erb +31 -0
  26. data/app/views/vario/values/_form.html.erb +20 -0
  27. data/config/importmap.rb +4 -0
  28. data/config/locales/en.yml +50 -0
  29. data/config/locales/nl.yml +50 -0
  30. data/config/routes.rb +11 -6
  31. data/db/migrate/20261118190000_add_value_to_vario_settings.rb +8 -0
  32. data/db/migrate/20261118190100_create_vario_refinements.rb +19 -0
  33. data/lib/generators/vario/topics_from_config/topics_from_config_generator.rb +189 -0
  34. data/lib/tasks/vario_tasks.rake +30 -0
  35. data/lib/vario/action_view_helpers.rb +119 -16
  36. data/lib/vario/active_record_helpers.rb +3 -2
  37. data/lib/vario/api.rb +49 -60
  38. data/lib/vario/backup.rb +50 -0
  39. data/lib/vario/card.rb +27 -0
  40. data/lib/vario/config.rb +36 -11
  41. data/lib/vario/context_variables.rb +69 -0
  42. data/lib/vario/converter.rb +113 -0
  43. data/lib/vario/engine.rb +27 -0
  44. data/lib/vario/migrator.rb +83 -0
  45. data/lib/vario/resolver.rb +71 -0
  46. data/lib/vario/settable.rb +41 -23
  47. data/lib/vario/settable_topic.rb +37 -0
  48. data/lib/vario/settings_reader.rb +45 -15
  49. data/lib/vario/topic.rb +299 -0
  50. data/lib/vario/topic_reader.rb +42 -0
  51. data/lib/vario/types.rb +64 -0
  52. data/lib/vario/version.rb +1 -1
  53. data/lib/vario.rb +16 -0
  54. metadata +116 -34
  55. data/app/controllers/vario/levels_controller.rb +0 -94
  56. data/app/controllers/vario/settings_controller.rb +0 -35
  57. data/app/views/vario/levels/_form.html.slim +0 -53
  58. data/app/views/vario/levels/create.js.erb +0 -4
  59. data/app/views/vario/levels/destroy.js.erb +0 -4
  60. data/app/views/vario/levels/update.js.erb +0 -4
  61. data/app/views/vario/settings/_levels.html.slim +0 -22
  62. data/app/views/vario/settings/_new_level.html.slim +0 -7
  63. data/app/views/vario/settings/_settings.html.slim +0 -24
  64. data/app/views/vario/settings/index.html.slim +0 -28
  65. data/app/views/vario/settings/levels.html.slim +0 -1
  66. data/app/views/vario/settings/show.html.slim +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72dda152204470710230814ed7b164322aeeed9081a6d0a92474210ce184253e
4
- data.tar.gz: e2d28169ff7af1e04a77d58139f0d39670c9122b6ee1659dd42b9a39c34d19be
3
+ metadata.gz: 47db0998a9fd4d0c9f8ce3385e57099b51bd720e16ff5ab847a4f9afc29435ac
4
+ data.tar.gz: dc30b5caf02ac85aa844ad56f0f7df5cc1713f91afc6dce287d6527ae1c74925
5
5
  SHA512:
6
- metadata.gz: c5f1ae8b6f91f32014784631fce9093e0e43126a6e7802047f2762689091decd73d79adaeefeee9e1e401346379d630aafdf28d8b64a0f945e8a70995bad64ba
7
- data.tar.gz: f7bbfe84ef93f50fd63062ab48a1cb789a97911f799c365a190a80a4427ecf550d4cb4262e7db1f63b38cb3bb4a4afce3abce0e06dd7744a7d9442049adeeb91
6
+ metadata.gz: f756d915b2c46e08be622c1f71904091e97ddf4ff6bbc71efd256623617d8cc8e0a9e5957455eb4441946ec46c481eb0426cf269a5d80138f62d7f5eacabcb00
7
+ data.tar.gz: 2bbd8f2ff1075c989dad7cce7a54a2282d30f2dda1eb818e3b399ac306b6499b7b924102f646c6e27f9f64a0502325608465bd6a1fd9e977edc0cb8d7c1ddff7
data/README.md CHANGED
@@ -26,12 +26,79 @@ Next add vario to your routes.rb:
26
26
  mount Vario::Engine, at: "/settings", as: "vario"
27
27
  ```
28
28
 
29
+ The settings pages show the topics of the current settable, which you set in the initializer:
30
+
31
+ ```ruby
32
+ Vario.setup do |config|
33
+ config.current_settable = -> { Current.account }
34
+ end
35
+ ```
36
+
29
37
  Then add a menu entry, or link to the settings page:
30
38
 
31
39
  ```ruby
32
- link_to("Settings", vario.settings_path(settable: Current&.account&.to_sgid(for: "Vario").to_s))
40
+ link_to("Settings", vario.topics_path)
41
+ ```
42
+
43
+ The pages render in your layout (`config.admin_layout`), with their topics listed beside the topic page. When your own menu lists the topics instead, set `config.layout = :host` and add them to it: `vario_menu_items` adds an item per topic the user may see, with a label, icon and link, and the topic's setting labels and descriptions as the link's `data-keywords`, for a menu that searches:
44
+
45
+ ```ruby
46
+ Menus::Builder.build(:settings) do |m|
47
+ m.group :settings do
48
+ vario_menu_items(m) # the current settable's; vario_menu_items(m, account) for another
49
+ end
50
+ end
51
+ ```
52
+
53
+ The settings pages need importmap-rails, Propshaft and Tailwind CSS 4 through tailwindcss-rails 4.3 or later (for engine support), [FormComponents](https://github.com/entdec/form_components) for their forms and [entdec-view_components](https://github.com/entdec/view_components) for the conditions editor, both dependencies from GitHub:
54
+
55
+ ```ruby
56
+ gem "form_components", github: "entdec/form_components"
57
+ gem "entdec-view_components", github: "entdec/view_components"
33
58
  ```
34
59
 
60
+ Import Vario's JavaScript, which uses your app's Stimulus application (`window.Stimulus`) or starts one, and register the controllers of FormComponents and entdec-view_components with it:
61
+
62
+ ```js
63
+ // app/javascript/application.js
64
+ import "vario"
65
+ import { registerFormComponents } from "form_components"
66
+ import { registerViewComponents } from "entdec/view_components"
67
+
68
+ registerFormComponents(window.Stimulus)
69
+ registerViewComponents(window.Stimulus)
70
+ ```
71
+
72
+ and add their styles to your Tailwind CSS:
73
+
74
+ ```css
75
+ /* app/assets/tailwind/application.css */
76
+ @import "../builds/tailwind/vario";
77
+ @import "../builds/tailwind/form_components";
78
+ @import "../builds/tailwind/entdec_view_components";
79
+ ```
80
+
81
+ The icons are [Font Awesome](https://fontawesome.com)'s, which your app loads.
82
+
83
+ ## Upgrading from 1.x
84
+
85
+ Generate topics from your 1.x initializer (`bin/rails generate vario:topics_from_config`), then convert the settings' levels to values and refinements:
86
+
87
+ ```sh
88
+ bin/rails vario:migrate:report # what it would do, per settable
89
+ bin/rails vario:backup FILE=vario_backup.json # all settings and refinements, as they are
90
+ bin/rails vario:migrate # convert
91
+ ```
92
+
93
+ To run the migration again from the same data, restore the backup first. Restoring replaces all settings and refinements by the backup's, exactly as they were: those added since are removed, those removed since come back, and changes since are lost.
94
+
95
+ ```sh
96
+ bin/rails vario:restore FILE=vario_backup.json
97
+ bin/rails vario:migrate
98
+ ```
99
+
100
+ `FORCE=1 bin/rails vario:migrate` converts again without a backup: it keeps the rows as they are now, but replaces their values and refinements by those converted from their levels.
101
+
35
102
  ## Installation
36
103
 
37
104
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -31,7 +31,3 @@ Rake::TestTask.new(:test) do |t|
31
31
  end
32
32
 
33
33
  task default: :test
34
-
35
- # # Adds the Auxilium semver task
36
- # spec = Gem::Specification.find_by_name 'auxilium'
37
- # # load "#{spec.gem_dir}/lib/tasks/semver.rake"
@@ -0,0 +1,316 @@
1
+ /* A card (shared/_card): a header with its icon, title and actions, over its tabs or its content */
2
+ .vario-card {
3
+ @apply overflow-hidden bg-white sm:rounded-lg sm:shadow dark:bg-gray-800;
4
+ }
5
+
6
+ .vario-card__header {
7
+ @apply flex flex-wrap items-center gap-4 border-b border-gray-200 bg-white px-4 py-5 sm:px-6 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300;
8
+ }
9
+
10
+ .vario-card__icon {
11
+ @apply shrink-0 text-primary-600 dark:text-gray-300;
12
+ }
13
+
14
+ .vario-card__title {
15
+ @apply min-w-0 flex-1 text-lg leading-6 font-medium text-gray-900 dark:text-white;
16
+ }
17
+
18
+ .vario-card__actions {
19
+ @apply flex shrink-0 flex-wrap gap-1;
20
+ }
21
+
22
+ .vario-card__tabs {
23
+ @apply flex gap-6 overflow-x-auto border-b border-gray-200 px-4 sm:px-6 dark:border-gray-700 dark:bg-gray-900;
24
+ }
25
+
26
+ .vario-card__tab {
27
+ @apply -mb-px shrink-0 cursor-pointer border-b-2 border-transparent px-1 py-4 text-sm font-medium whitespace-nowrap text-gray-500 hover:border-gray-300 hover:text-gray-700 dark:text-gray-300;
28
+ }
29
+
30
+ .vario-card__tab[aria-selected="true"] {
31
+ @apply border-primary-500 text-primary-600 dark:text-white;
32
+ }
33
+
34
+ .vario-card__body {
35
+ @apply px-6 py-6;
36
+ }
37
+
38
+ .vario-button {
39
+ @apply inline-flex cursor-pointer items-center justify-center rounded-md border border-gray-300 bg-white px-3 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200;
40
+ }
41
+
42
+ .vario-button--danger {
43
+ @apply border-red-300 text-red-700 hover:bg-red-50;
44
+ }
45
+
46
+ /* The settings of a group (settings/index) */
47
+ .vario-table {
48
+ @apply min-w-full divide-y divide-gray-200 text-sm dark:divide-gray-700;
49
+ }
50
+
51
+ .vario-table th {
52
+ @apply py-2 pr-4 text-left font-medium text-gray-500 dark:text-gray-400;
53
+ }
54
+
55
+ .vario-table td {
56
+ @apply py-2 pr-4 text-gray-900 dark:text-white;
57
+ }
58
+
59
+ .vario-table a {
60
+ @apply text-primary-600 hover:underline;
61
+ }
62
+
63
+ .vario-description {
64
+ @apply mb-4 text-sm text-gray-600 dark:text-gray-400;
65
+ }
66
+
67
+ /* A setting's levels (settings/_levels), each a form (levels/_form) */
68
+ .vario-levels {
69
+ @apply flex flex-col gap-2;
70
+ }
71
+
72
+ .vario-level {
73
+ @apply grid grid-cols-12 items-end gap-4 rounded-md bg-gray-50 p-3 dark:bg-gray-900;
74
+ }
75
+
76
+ .vario-level__title {
77
+ @apply col-span-12 text-sm font-medium text-gray-900 dark:text-white;
78
+ }
79
+
80
+ .vario-level__handle {
81
+ @apply col-span-1 cursor-grab self-center text-gray-400 hover:text-gray-600;
82
+ }
83
+
84
+ .vario-level__conditions {
85
+ @apply col-span-6 grid grid-cols-12 gap-4;
86
+ }
87
+
88
+ .vario-level__value {
89
+ @apply col-span-3;
90
+ }
91
+
92
+ .vario-level__buttons {
93
+ @apply col-span-2 flex gap-2;
94
+ }
95
+
96
+ .vario-button--primary {
97
+ @apply border-transparent bg-primary-600 text-white hover:bg-primary-500 dark:bg-primary-600 dark:text-white;
98
+ }
99
+
100
+ /* A settable's topics (topics/show): the list beside the topic's page.
101
+ Where the list and the topic's head stick in the host's scrolling area,
102
+ that area's height and its background (which covers what scrolls by above
103
+ the head) can be set by the host:
104
+ :root { --vario-sticky-top: 1rem; --vario-scroll-height: calc(100vh - 4rem); --vario-page-background: var(--color-gray-50) } */
105
+
106
+ .vario-topics {
107
+ @apply grid gap-6 md:grid-cols-[14rem_1fr];
108
+ }
109
+
110
+ /* config.layout = :host: the host's menu lists the topics */
111
+ .vario-topics--host {
112
+ @apply md:grid-cols-1;
113
+ }
114
+
115
+ .vario-topics__nav {
116
+ @apply flex flex-col gap-3 rounded-lg bg-white p-3 shadow-xs ring-1 ring-gray-900/5 md:sticky md:top-[var(--vario-sticky-top,0px)] md:max-h-[calc(var(--vario-scroll-height,100vh)-2*var(--vario-sticky-top,0px))] md:self-start md:overflow-y-auto [scrollbar-width:thin] dark:bg-gray-800 dark:ring-white/10 dark:[scrollbar-color:var(--color-gray-600)_transparent];
117
+ }
118
+
119
+ .vario-topics__find {
120
+ @apply relative;
121
+ }
122
+
123
+ .vario-topics__find-icon {
124
+ @apply pointer-events-none absolute inset-y-0 left-3 my-auto h-4 text-sm text-gray-400;
125
+ }
126
+
127
+ .vario-topics__search {
128
+ @apply w-full rounded-md border-0 py-1.5 pr-3 pl-9 text-sm text-gray-900 ring-1 ring-gray-300 ring-inset placeholder:text-gray-400 focus:ring-2 focus:ring-primary-600 dark:bg-white/5 dark:text-white dark:ring-white/10;
129
+ }
130
+
131
+ .vario-topics__list {
132
+ @apply flex flex-col gap-0.5;
133
+ }
134
+
135
+ .vario-topics__link {
136
+ @apply flex items-center gap-3 rounded-md px-2 py-1.5 text-sm font-medium text-gray-700 hover:bg-gray-50 hover:text-gray-900 dark:text-gray-300 dark:hover:bg-white/5 dark:hover:text-white;
137
+ }
138
+
139
+ .vario-topics__icon {
140
+ @apply shrink-0 text-gray-400;
141
+ }
142
+
143
+ .vario-topics__link[aria-current="page"] {
144
+ @apply bg-primary-50 font-semibold text-primary-700 dark:bg-white/10 dark:text-white;
145
+ }
146
+
147
+ .vario-topics__link[aria-current="page"] .vario-topics__icon {
148
+ @apply text-primary-600 dark:text-white;
149
+ }
150
+
151
+ /* Under a topic, its settings: those of the current one, or those found */
152
+ .vario-topics__settings {
153
+ @apply ml-[1.1rem] flex flex-col border-l border-gray-200 pl-3 dark:border-gray-700;
154
+ }
155
+
156
+ .vario-topics__settings:not(:has(> li:not([hidden]))) {
157
+ @apply hidden;
158
+ }
159
+
160
+ .vario-topics__setting {
161
+ @apply block rounded-md px-2 py-1 text-[0.8125rem] text-gray-500 hover:bg-gray-50 hover:text-gray-900 dark:text-gray-400 dark:hover:bg-white/5 dark:hover:text-white;
162
+ }
163
+
164
+ .vario-topics__nav mark {
165
+ @apply rounded-sm bg-yellow-100 px-px text-inherit dark:bg-yellow-500/30;
166
+ }
167
+
168
+ .vario-topics__none {
169
+ @apply px-3 text-sm text-gray-500;
170
+ }
171
+
172
+ .vario-topics__empty {
173
+ @apply text-sm text-gray-500;
174
+ }
175
+
176
+ .vario-topic {
177
+ @apply min-w-0;
178
+ }
179
+
180
+ .vario-topic__head {
181
+ @apply sticky top-[var(--vario-sticky-top,0px)] z-10 mb-6 flex flex-wrap items-start gap-4 rounded-t-lg border-b border-gray-200 bg-gray-100/90 px-4 py-4 backdrop-blur sm:px-6 dark:border-white/10 dark:bg-gray-900/90;
182
+ }
183
+
184
+ .vario-topic__head::before {
185
+ content: "";
186
+ position: absolute;
187
+ inset-inline: 0;
188
+ bottom: 100%;
189
+ height: var(--vario-sticky-top, 0px);
190
+ background: var(--vario-page-background, transparent);
191
+ }
192
+
193
+ .vario-topic__icon {
194
+ @apply mt-1 text-xl text-primary-600;
195
+ }
196
+
197
+ .vario-topic__title {
198
+ @apply min-w-0 flex-1;
199
+ }
200
+
201
+ .vario-topic__title h1 {
202
+ @apply text-xl font-semibold text-gray-900 dark:text-white;
203
+ }
204
+
205
+ .vario-topic__title p {
206
+ @apply mt-1 text-sm text-gray-500 dark:text-gray-400;
207
+ }
208
+
209
+ .vario-topic__savebar {
210
+ @apply flex items-center gap-2;
211
+ }
212
+
213
+ .vario-topic__unsaved {
214
+ @apply rounded-full bg-amber-100 px-2.5 py-0.5 text-xs font-medium text-amber-800;
215
+ }
216
+
217
+ .vario-topic__settings {
218
+ @apply flex flex-col gap-10;
219
+ }
220
+
221
+ /* Linked to from the search (#setting_<key>): below the sticky header */
222
+ .vario-setting {
223
+ @apply scroll-mt-28;
224
+ }
225
+
226
+ /* A setting's refinements (topics/_refinements) */
227
+ .vario-refinements {
228
+ @apply flex flex-col gap-2 border-t border-gray-100 pt-4 dark:border-white/10;
229
+ }
230
+
231
+ .vario-refinements__head {
232
+ @apply text-sm font-medium text-gray-900 dark:text-white;
233
+ }
234
+
235
+ .vario-refinements__hint {
236
+ @apply font-normal text-gray-500 before:content-['·_'] dark:text-gray-400;
237
+ }
238
+
239
+ .vario-refinement {
240
+ @apply mb-2 rounded-lg ring-1 ring-gray-200 dark:ring-white/10;
241
+ }
242
+
243
+ .vario-refinement__bar {
244
+ @apply flex items-center gap-3 px-3 py-2;
245
+ }
246
+
247
+ .vario-refinement__grip {
248
+ @apply flex cursor-grab items-center gap-1 text-sm text-gray-400 tabular-nums;
249
+ }
250
+
251
+ .vario-refinement__summary {
252
+ @apply min-w-0 flex-1 cursor-pointer truncate text-left text-sm text-gray-700 dark:text-gray-300;
253
+ }
254
+
255
+ .vario-refinement__remove {
256
+ @apply cursor-pointer px-1 text-lg leading-none text-gray-400 hover:text-red-600;
257
+ }
258
+
259
+ .vario-refinement__detail {
260
+ @apply flex flex-col gap-4 border-t border-gray-100 px-3 py-3 dark:border-white/10;
261
+ }
262
+
263
+ .vario-refinement__then {
264
+ @apply flex items-start gap-3 text-sm text-gray-500;
265
+ }
266
+
267
+ .vario-refinement__then > span {
268
+ @apply pt-2;
269
+ }
270
+
271
+ .vario-refinement__then > :last-child {
272
+ @apply flex-1;
273
+ }
274
+
275
+ /* What applies (topics/_applies) */
276
+ .vario-applies {
277
+ @apply mt-12 border-t border-gray-200 pt-8 dark:border-white/10;
278
+ }
279
+
280
+ .vario-applies h2 {
281
+ @apply text-base font-semibold text-gray-900 dark:text-white;
282
+ }
283
+
284
+ .vario-applies__intro {
285
+ @apply mt-1 text-sm text-gray-500 dark:text-gray-400;
286
+ }
287
+
288
+ .vario-applies__form {
289
+ @apply mt-4 grid items-end gap-4 sm:grid-cols-3;
290
+ }
291
+
292
+ .vario-applies__table {
293
+ @apply mt-6 w-full text-left text-sm;
294
+ }
295
+
296
+ .vario-applies__table th,
297
+ .vario-applies__table td {
298
+ @apply border-b border-gray-100 py-2 pr-4 align-top dark:border-white/10;
299
+ }
300
+
301
+ .vario-applies__table thead th {
302
+ @apply font-medium text-gray-500;
303
+ }
304
+
305
+ .vario-applies__table tbody th {
306
+ @apply font-medium text-gray-900 dark:text-white;
307
+ }
308
+
309
+ /* Settings on a page of the host's (values/_form) */
310
+ .vario-values {
311
+ @apply flex flex-col gap-8;
312
+ }
313
+
314
+ .vario-values__actions {
315
+ @apply flex justify-end;
316
+ }
@@ -0,0 +1,6 @@
1
+ /* Entry point for tailwindcss-rails v4 (Tailwind CSS v4).
2
+ The host app includes it with: @import "../builds/tailwind/vario";
3
+ Imported into the components layer so utility classes still override component styles */
4
+ @import "../../stylesheets/vario/application.css" layer(components);
5
+
6
+ @source "../../..";
@@ -8,8 +8,15 @@ module Vario
8
8
  method(:add_breadcrumbs).super_method&.call(*args)
9
9
  end
10
10
 
11
- def breadcrumb_settings_path(setting, options = {})
12
- settings_path(options.merge(settable: setting.settable.to_sgid(for: 'Vario').to_s))
11
+ private
12
+
13
+ # A topic of a settable's policy (Vario::TopicPolicy)
14
+ def policy(topic)
15
+ Pundit.policy!(vario_user, topic)
16
+ end
17
+
18
+ def vario_user
19
+ SettableTopic.user_of(self)
13
20
  end
14
21
  end
15
22
  end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vario
4
+ # A settable's topics (Vario::Topic), and a topic's settings on a page,
5
+ # saved as a whole: their own values and refinements
6
+ class TopicsController < ApplicationController
7
+ before_action :set_settable
8
+ before_action :set_topic, only: %i[show update variables records]
9
+ before_action :set_definition, only: %i[variables records]
10
+
11
+ def index
12
+ topic = @topics.first
13
+ return redirect_to topic_path(topic.key) if topic
14
+
15
+ render :index
16
+ end
17
+
18
+ # With applies[<root>]: what every setting is in that context, and why
19
+ def show
20
+ @settings = settings_by_key
21
+ @applies = applies_context if params[:applies]
22
+ end
23
+
24
+ def update
25
+ raise Pundit::NotAuthorizedError, "may not update #{@topic.key}" unless policy(@topic).update?
26
+
27
+ @settings = settings_by_key
28
+ settings_params.each do |key, attributes|
29
+ @settings[key]&.assign_attributes(attributes)
30
+ end
31
+
32
+ if save(@settings.values)
33
+ redirect_to topic_path(@topic.key), notice: t('vario.pages.update.saved')
34
+ else
35
+ flash.now[:alert] = t('vario.pages.update.not_saved')
36
+ render :show, status: :unprocessable_content
37
+ end
38
+ end
39
+
40
+ # GET variables?setting=<key>&path=order: as Liquidum's (variables_url)
41
+ def variables
42
+ render json: { path: params[:path].to_s, variables: ContextVariables.new(@definition.context).at(params[:path]) }
43
+ end
44
+
45
+ # GET records?setting=<key>&path=location: an object variable's records
46
+ # (records_url)
47
+ def records
48
+ model = ContextVariables.new(@definition.context).model(params[:path])
49
+ return head(:not_found) unless model
50
+
51
+ render json: { records: Topic.records_of(model, @settable).map { |record| { label: Topic.label_of(record), value: record.id.to_s } } }
52
+ end
53
+
54
+ private
55
+
56
+ # The context chosen in "What applies?": for an object root its record
57
+ # (in its drop, for a drop root), else the value as given
58
+ def applies_context
59
+ given = params.require(:applies).permit(*@topic.topic.context.keys).to_h.compact_blank
60
+ given.to_h do |root, value|
61
+ type = @topic.topic.context[root]
62
+ model = ContextVariables.new(@topic.topic.context).model(root)
63
+ record = model&.find_by(model.primary_key => value)
64
+ [root, type.is_a?(Class) && type < Liquid::Drop && record ? type.new(record) : record || value]
65
+ end
66
+ end
67
+
68
+ def set_definition
69
+ @definition = @topic.topic.definitions.values.find { |definition| definition.key == params[:setting] }
70
+ raise ActiveRecord::RecordNotFound, "Unknown setting #{params[:setting]}" unless @definition
71
+ end
72
+
73
+ # config.current_settable: the pages are always the current one's (account)
74
+ def set_settable
75
+ @settable = Vario.config.current_settable
76
+ raise ActiveRecord::RecordNotFound, 'No current settable' unless @settable
77
+
78
+ @topics = SettableTopic.visible(@settable, vario_user)
79
+ end
80
+
81
+ def set_topic
82
+ @topic = @topics.find { |topic| topic.key == params[:key] }
83
+ raise ActiveRecord::RecordNotFound, "Unknown topic #{params[:key]}" unless @topic
84
+ end
85
+
86
+ # Its settings' rows by key, new ones for those without
87
+ def settings_by_key
88
+ rows = @settable.public_send(@settable.vario_settings_association).includes(:refinements).where(name: @topic.topic.definitions.keys).index_by(&:name)
89
+ @topic.topic.definitions.values.to_h do |definition|
90
+ [definition.key, rows[definition.name] || @settable.public_send(@settable.vario_settings_association).build(name: definition.name)]
91
+ end
92
+ end
93
+
94
+ # Those changed, or new with a value or refinements, in one go
95
+ def save(settings)
96
+ settings = settings.select { |setting| setting.persisted? ? setting.changed_for_autosave? : (!setting.value.nil? || setting.refinements.any?) }
97
+ return false unless settings.map(&:valid?).all?
98
+
99
+ Setting.transaction { settings.each(&:save!) }
100
+ true
101
+ end
102
+
103
+ # settings[<key>][value], settings[<key>][refinements_attributes][<n>][...]
104
+ def settings_params
105
+ keys = @topic.topic.definitions.values.map(&:key)
106
+ settings = params.fetch(:settings, {})
107
+ keys.select { |key| settings.key?(key) }.to_h do |key|
108
+ [key, setting_params(settings.require(key))]
109
+ end
110
+ end
111
+
112
+ # A value is a setting's type (a list, a text, …), so as sent
113
+ def setting_params(setting)
114
+ setting = setting.to_unsafe_h
115
+ attributes = setting.slice('value')
116
+ return attributes unless setting['refinements_attributes']
117
+
118
+ attributes.merge('refinements_attributes' => setting['refinements_attributes'].values.map do |refinement|
119
+ attributes = refinement.slice('id', 'position', 'enabled', 'note', '_destroy', 'value')
120
+ refinement.key?('conditions') ? attributes.merge('conditions' => conditions(refinement['conditions'])) : attributes
121
+ end)
122
+ end
123
+
124
+ # As the conditions component sends them: match, rules by index
125
+ def conditions(conditions)
126
+ conditions = conditions.to_h
127
+ rules = conditions['rules'].to_h.values.map { |rule| rule.to_h.slice('variable', 'operator', 'value', 'value_to') }
128
+ { 'match' => conditions['match'].presence || 'all', 'rules' => rules.reject { |rule| rule['variable'].blank? } }
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vario
4
+ # Saves settings on a page of the host's (render_vario_settings): each the
5
+ # value on exactly the page's context (customer_id: 5), or without one its
6
+ # own value, then back to that page
7
+ class ValuesController < ApplicationController
8
+ def update
9
+ settable = GlobalID::Locator.locate_signed(params[:settable], for: 'Vario')
10
+ context = Vario.verifier.verified(params[:context].to_s)
11
+ raise ActiveRecord::RecordNotFound, 'Unknown settable' unless settable && context
12
+
13
+ Setting.transaction do
14
+ params.fetch(:values, {}).to_unsafe_h.each do |name, attributes|
15
+ definition = Topic.definition(settable, name)
16
+ raise UnknownSetting, "Setting '#{name}' is not defined for #{settable.class.name}" unless definition
17
+ raise Pundit::NotAuthorizedError, "may not update #{definition.topic.key}" unless policy(SettableTopic.new(settable: settable, topic: definition.topic)).update?
18
+
19
+ setting = settable.public_send(settable.vario_settings_association).find_or_initialize_by(name: name)
20
+ setting.value_on!(context, attributes['value'])
21
+ end
22
+ end
23
+
24
+ redirect_back_or_to topics_path, notice: t('vario.pages.update.saved')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ // Entry point. It registers vario's controllers with the host app's Stimulus
2
+ // application (window.Stimulus), which it starts if nothing has yet.
3
+
4
+ import { Application } from "@hotwired/stimulus"
5
+
6
+ import RefinementsController from "vario/controllers/refinements_controller"
7
+ import SearchController from "vario/controllers/search_controller"
8
+ import SortableController from "vario/controllers/sortable_controller"
9
+ import TabsController from "vario/controllers/tabs_controller"
10
+ import TopicController from "vario/controllers/topic_controller"
11
+
12
+ window.Stimulus ||= Application.start()
13
+
14
+ window.Stimulus.register("vario--refinements", RefinementsController)
15
+ window.Stimulus.register("vario--search", SearchController)
16
+ window.Stimulus.register("vario--sortable", SortableController)
17
+ window.Stimulus.register("vario--tabs", TabsController)
18
+ window.Stimulus.register("vario--topic", TopicController)
@@ -0,0 +1,29 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // A setting's refinements (topics/_refinements): each opens from its line,
4
+ // and their priorities follow their order, after a drag, an add or a remove.
5
+ // The form then has unsaved changes (vario--topic).
6
+ export default class extends Controller {
7
+ static targets = ["item", "position", "priority", "count"]
8
+
9
+ connect() {
10
+ this.renumber({ changed: false })
11
+ }
12
+
13
+ toggle(event) {
14
+ const button = event.currentTarget
15
+ const detail = button.closest("[data-vario--refinements-target='item']").querySelector(".vario-refinement__detail")
16
+ detail.hidden = !detail.hidden
17
+ button.setAttribute("aria-expanded", !detail.hidden)
18
+ }
19
+
20
+ renumber({ changed = true } = {}) {
21
+ const items = this.itemTargets.filter((item) => item.style.display !== "none" && !item.closest("template"))
22
+ items.forEach((item, index) => {
23
+ item.querySelector("[data-vario--refinements-target='position']").value = index + 1
24
+ item.querySelector("[data-vario--refinements-target='priority']").textContent = index + 1
25
+ })
26
+ this.countTargets.forEach((count) => (count.textContent = items.length))
27
+ if (changed === true) this.element.dispatchEvent(new Event("change", { bubbles: true }))
28
+ }
29
+ }