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.
- 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 -94
- 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,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# What a setting of a settable is in a context, and why: the first enabled
|
|
5
|
+
# refinement (by position) whose conditions are met, else the settable's own
|
|
6
|
+
# value, else the default (given when it's read, else the definition's).
|
|
7
|
+
#
|
|
8
|
+
# Resolver.new(account).explain('shipping.cutoff_time', order: order)
|
|
9
|
+
# # => #<Vario::Resolution value="15:00", source=:refinement, refinement=...>
|
|
10
|
+
class Resolver
|
|
11
|
+
def initialize(settable, settings)
|
|
12
|
+
@settable = settable
|
|
13
|
+
@settings = settings
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def explain(name, context = {})
|
|
17
|
+
name = name.to_s
|
|
18
|
+
definition = Topic.definition(@settable, name)
|
|
19
|
+
raise UnknownSetting, "Setting '#{name}' is not defined for #{@settable.class.name}" unless definition
|
|
20
|
+
|
|
21
|
+
context = context.to_h.transform_keys(&:to_s)
|
|
22
|
+
default = context.key?('default') ? definition.cast(context.delete('default')) : definition.default_value
|
|
23
|
+
setting = @settings[name]
|
|
24
|
+
|
|
25
|
+
if setting
|
|
26
|
+
lookup = Conditions::Lookup.new(assigns(definition, context))
|
|
27
|
+
setting.refinements.each do |refinement|
|
|
28
|
+
next unless refinement.enabled? && !refinement.value.nil?
|
|
29
|
+
next unless refinement.rules.met?(lookup)
|
|
30
|
+
|
|
31
|
+
return Resolution.new(name: name, value: refinement.value, source: :refinement, refinement: refinement)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
return Resolution.new(name: name, value: setting.value, source: :value, refinement: nil) unless setting.value.nil?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
Resolution.new(name: name, value: default, source: :default, refinement: nil)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# The context the refinements look at: as given, with the default of the
|
|
43
|
+
# setting's context roots it hasn't or that are nil, as 1.x's value_proc
|
|
44
|
+
# (config.context_default), and 1.x's keys at the variables they are now
|
|
45
|
+
# (config.legacy_key)
|
|
46
|
+
def assigns(definition, context)
|
|
47
|
+
definition.context.each_key do |root|
|
|
48
|
+
default = Vario.config.context_defaults[root]
|
|
49
|
+
context[root] = default.respond_to?(:call) ? default.call : default if default && context[root].nil?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Vario.config.legacy_keys.each do |key, variable|
|
|
53
|
+
next unless context.key?(key)
|
|
54
|
+
|
|
55
|
+
*path, last = variable.split('.')
|
|
56
|
+
target = path.inject(context) do |hash, part|
|
|
57
|
+
break unless hash.is_a?(Hash)
|
|
58
|
+
|
|
59
|
+
hash[part] = (hash[part] || {}).then { |value| value.is_a?(Hash) ? value.transform_keys(&:to_s) : value }
|
|
60
|
+
end
|
|
61
|
+
target["#{last}_id"] = context[key] if target.is_a?(Hash) && !target.key?(last) && !target.key?("#{last}_id")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
context
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# A setting's value in a context and where it comes from: :refinement
|
|
69
|
+
# (refinement is the one), :value (the settable's own) or :default
|
|
70
|
+
Resolution = Data.define(:name, :value, :source, :refinement)
|
|
71
|
+
end
|
data/lib/vario/settable.rb
CHANGED
|
@@ -5,39 +5,57 @@ module Vario
|
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
@vario_setting_cache[name]&.value_for(context)
|
|
8
|
+
# What a setting is in a context, and why (Vario::Resolution)
|
|
9
|
+
#
|
|
10
|
+
# account.explain('shipping.cutoff_time', order: order).source # => :refinement
|
|
11
|
+
def explain(name, context = {})
|
|
12
|
+
Resolver.new(self, vario_settings_by_name).explain(name, context)
|
|
14
13
|
end
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
# Its settings' rows by name, with their refinements, loaded once
|
|
16
|
+
# (until reload)
|
|
17
|
+
def vario_settings_by_name
|
|
18
|
+
@vario_settings_by_name ||= public_send(vario_settings_association).includes(:refinements).index_by(&:name)
|
|
19
|
+
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
def reload(*)
|
|
22
|
+
@vario_settings_by_name = nil
|
|
23
|
+
super
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
if vario_settings_association == :settings
|
|
27
|
+
# Without arguments its rows (Vario::Setting); with a topic the
|
|
28
|
+
# values of its settings in a context:
|
|
29
|
+
#
|
|
30
|
+
# account.settings(:shipping, order: order).cutoff_time
|
|
31
|
+
def settings(topic = nil, context = {})
|
|
32
|
+
return super() if topic.nil?
|
|
28
33
|
|
|
29
|
-
|
|
30
|
-
vario_setting.save unless vario_setting.persisted?
|
|
34
|
+
TopicReader.new(self, topic, context)
|
|
31
35
|
end
|
|
32
36
|
end
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
# What a setting is in a context (1.x's way to read it, as explain)
|
|
39
|
+
#
|
|
40
|
+
# account.setting('shipping.cutoff_time', location_id: id, default: '13:00')
|
|
41
|
+
def setting(name, context = {})
|
|
42
|
+
explain(name, context).value
|
|
43
|
+
end
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
# The values of a topic's settings by their keys (1.x)
|
|
46
|
+
def settings_hash(name, context = {})
|
|
47
|
+
raise ArgumentError, 'Cannot request hash with a default' if context.key?(:default) || context.key?('default')
|
|
48
|
+
|
|
49
|
+
TopicReader.new(self, name, context).to_h.with_indifferent_access
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# From 1.x, which created settings when they were read; 2.0 creates none
|
|
53
|
+
def settings_save_unsaved; end
|
|
54
|
+
|
|
55
|
+
# From 1.x; settings are loaded once anyway
|
|
56
|
+
def settings_prepopulate_cache
|
|
57
|
+
vario_settings_by_name
|
|
58
|
+
nil
|
|
41
59
|
end
|
|
42
60
|
end
|
|
43
61
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# A topic of a settable: what is shown, changed and authorized (Pundit
|
|
5
|
+
# finds its policy, Vario::TopicPolicy)
|
|
6
|
+
#
|
|
7
|
+
# authorize Vario::SettableTopic.new(settable: account, topic: ShippingSettings), :update?
|
|
8
|
+
SettableTopic = Data.define(:settable, :topic) do
|
|
9
|
+
# The settable's topics the user may see (show?), for the pages and the
|
|
10
|
+
# host's menu (vario_menu_items)
|
|
11
|
+
def self.visible(settable, user)
|
|
12
|
+
Topic.for(settable).map { |topic| new(settable: settable, topic: topic) }.select { |topic| Pundit.policy!(user, topic).show? }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# The user a controller authorizes: the host's Pundit user, else its
|
|
16
|
+
# current user
|
|
17
|
+
def self.user_of(controller)
|
|
18
|
+
return controller.send(:pundit_user) if controller.respond_to?(:pundit_user, true)
|
|
19
|
+
|
|
20
|
+
controller.send(:current_user) if controller.respond_to?(:current_user, true)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The topic's own policy (ShippingSettingsPolicy), else the app's for all
|
|
24
|
+
# topics (config.topic_policy)
|
|
25
|
+
def policy_class
|
|
26
|
+
"#{topic.name}Policy".safe_constantize || Vario.config.topic_policy.constantize
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def key
|
|
30
|
+
topic.key
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def title
|
|
34
|
+
topic.title
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -2,34 +2,64 @@
|
|
|
2
2
|
|
|
3
3
|
module Vario
|
|
4
4
|
# Make vario settings accessible as readers on the object
|
|
5
|
+
#
|
|
6
|
+
# class Order < ApplicationRecord
|
|
7
|
+
# settings_reader Account, %w[shipping.cutoff_time shipping.partial_shipping]
|
|
8
|
+
# end
|
|
9
|
+
#
|
|
10
|
+
# order.cutoff_time # account.setting('shipping.cutoff_time', order: order, location: order.location, ...)
|
|
11
|
+
# order.partial_shipping?
|
|
12
|
+
#
|
|
13
|
+
# The context is the object for the setting's context roots it answers
|
|
14
|
+
# to: itself for a root that is its model (order), else what it has for
|
|
15
|
+
# the root (location, order_type) or its id (location_id). A value in the
|
|
16
|
+
# object's own settings hash (Order's settings jsonb) comes first.
|
|
5
17
|
module SettingsReader
|
|
6
18
|
extend ActiveSupport::Concern
|
|
7
19
|
|
|
8
20
|
class_methods do
|
|
9
21
|
def settings_reader(klass, settings)
|
|
10
|
-
|
|
11
|
-
|
|
22
|
+
Vario::Topic.when_loaded do
|
|
23
|
+
settings.each do |setting|
|
|
24
|
+
definition = Vario::Topic.definition(klass, setting)
|
|
25
|
+
raise UnknownSetting, "Setting '#{setting}' is not defined for #{klass.name}" unless definition
|
|
12
26
|
|
|
13
|
-
|
|
14
|
-
|
|
27
|
+
method_name = setting.split('.').last
|
|
28
|
+
method_name += '?' if definition.type == :boolean
|
|
15
29
|
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
define_method(method_name.to_sym) do
|
|
31
|
+
result = nil
|
|
18
32
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
33
|
+
if respond_to?(:settings) && self.settings.respond_to?(:dig)
|
|
34
|
+
result = self.settings.dig(*setting.split('.'))
|
|
35
|
+
end
|
|
22
36
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
if result.nil?
|
|
38
|
+
context = vario_reader_context(definition)
|
|
39
|
+
result = self.setting(setting, context) if respond_to?(:settings) && self.class.name == klass.name
|
|
40
|
+
result = send(klass.name.underscore.to_sym)&.setting(setting, context) if result.nil? && self.respond_to?(klass.name.underscore.to_sym)
|
|
41
|
+
end
|
|
28
42
|
|
|
29
|
-
|
|
43
|
+
result
|
|
44
|
+
end
|
|
30
45
|
end
|
|
31
46
|
end
|
|
32
47
|
end
|
|
33
48
|
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def vario_reader_context(definition)
|
|
53
|
+
definition.context.each_key.with_object({}) do |root, context|
|
|
54
|
+
model = root.camelize.safe_constantize
|
|
55
|
+
if model.is_a?(Class) && is_a?(model)
|
|
56
|
+
context[root] = self
|
|
57
|
+
elsif respond_to?(root)
|
|
58
|
+
context[root] = send(root)
|
|
59
|
+
elsif respond_to?("#{root}_id")
|
|
60
|
+
context["#{root}_id"] = send("#{root}_id")
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
34
64
|
end
|
|
35
65
|
end
|
data/lib/vario/topic.rb
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# The settings of one settings page, for one kind of settable, with what
|
|
5
|
+
# their refinements may look at. One class per topic, in app/settings/ of
|
|
6
|
+
# the app or an engine, with its texts in the locale files:
|
|
7
|
+
#
|
|
8
|
+
# class ShippingSettings < Vario::Topic
|
|
9
|
+
# settable Account
|
|
10
|
+
# icon 'fal fa-truck'
|
|
11
|
+
# context order: OrderDrop, location: LocationDrop, order_type: :string
|
|
12
|
+
#
|
|
13
|
+
# setting :cutoff_time, :time, default: '13:00'
|
|
14
|
+
# setting :default_shipping_method, ShippingMethod, context: %i[location]
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# nl:
|
|
18
|
+
# vario:
|
|
19
|
+
# topics:
|
|
20
|
+
# shipping:
|
|
21
|
+
# title: Verzending
|
|
22
|
+
# description: Hoe en wanneer orders de deur uit gaan.
|
|
23
|
+
# settings:
|
|
24
|
+
# cutoff_time:
|
|
25
|
+
# label: Uiterste allocatietijd
|
|
26
|
+
# description: ...
|
|
27
|
+
#
|
|
28
|
+
# The topic's key is its class name without Settings (shipping), a
|
|
29
|
+
# setting's name its key after the topic's (shipping.cutoff_time).
|
|
30
|
+
class Topic
|
|
31
|
+
TYPES = %i[boolean integer decimal string text time array hash].freeze
|
|
32
|
+
|
|
33
|
+
# A setting as a topic defines it
|
|
34
|
+
class Definition
|
|
35
|
+
attr_reader :topic, :key, :name, :type, :default, :choices
|
|
36
|
+
|
|
37
|
+
def initialize(topic, key, type, default: nil, choices: nil, context: nil, name: nil)
|
|
38
|
+
unless TYPES.include?(type) || type.is_a?(Class)
|
|
39
|
+
raise ArgumentError, "Unknown type #{type.inspect} for setting #{key} of #{topic.name}, one of #{TYPES.join(', ')} or a model"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@topic = topic
|
|
43
|
+
@key = key.to_s
|
|
44
|
+
@name = name&.to_s || "#{topic.key}.#{@key}"
|
|
45
|
+
@type = type
|
|
46
|
+
@default = default
|
|
47
|
+
@choices = choices
|
|
48
|
+
@context = context&.map(&:to_s)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# A model whose records are the setting's values (stored by id)
|
|
52
|
+
def model
|
|
53
|
+
type if type.is_a?(Class)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# A value as the setting's type, from a form, the database or 1.x; nil
|
|
57
|
+
# for nothing (the default applies). A list is a list even when empty:
|
|
58
|
+
# none, as 1.x read a cleared one ([""]), not nothing.
|
|
59
|
+
def cast(value)
|
|
60
|
+
return value_type.cast(value) if type == :array && value.is_a?(Array)
|
|
61
|
+
|
|
62
|
+
value_type.cast(value) unless Types.blank?(value)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# As stored, in jsonb: decimals as text, so they stay exact
|
|
66
|
+
def serialize(value)
|
|
67
|
+
value = cast(value)
|
|
68
|
+
value.is_a?(BigDecimal) ? value.to_s : value
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# A stored value as the setting's. 1.x's "all" of an array with
|
|
72
|
+
# choices is all of them, as they are now; the block gives the
|
|
73
|
+
# settable, for choices that depend on it.
|
|
74
|
+
def read(value, &settable)
|
|
75
|
+
value = cast(value)
|
|
76
|
+
return choice_values(settable&.call) if type == :array && choices && value&.any? { |element| element.to_s == 'all' }
|
|
77
|
+
|
|
78
|
+
value
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def default_value
|
|
82
|
+
cast(default)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The choices, a list of values or of [label, value]; a lambda gets the
|
|
86
|
+
# settable when it takes an argument
|
|
87
|
+
def choices_for(settable = nil)
|
|
88
|
+
list = choices.respond_to?(:call) ? (choices.arity.zero? ? choices.call : choices.call(settable)) : choices
|
|
89
|
+
Array(list)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def choice_values(settable = nil)
|
|
93
|
+
choices_for(settable).map { |choice| choice.is_a?(Array) ? choice.last : choice }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def value_type
|
|
97
|
+
@value_type ||= case type
|
|
98
|
+
when :boolean then ActiveModel::Type::Boolean.new
|
|
99
|
+
when :integer then ActiveModel::Type::Integer.new
|
|
100
|
+
when :decimal then ActiveModel::Type::Decimal.new
|
|
101
|
+
when :string, :text then ActiveModel::Type::String.new
|
|
102
|
+
when :time then Types::TimeOfDay.new
|
|
103
|
+
when :array then Types::List.new
|
|
104
|
+
when :hash then Types::Map.new
|
|
105
|
+
else Types::Reference.new(type)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# What its refinements may look at: the topic's context, or the part
|
|
110
|
+
# of it the setting names
|
|
111
|
+
def context
|
|
112
|
+
@context ? topic.context.slice(*@context) : topic.context
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The records a model setting's value can be, for a settable
|
|
116
|
+
def records(settable)
|
|
117
|
+
model ? Topic.records_of(model, settable) : []
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# A value as text, for people: a choice's label, a record's name
|
|
121
|
+
def human(value, settable = nil)
|
|
122
|
+
return if value.nil?
|
|
123
|
+
|
|
124
|
+
labels = choices_for(settable).to_h { |choice| choice.is_a?(Array) ? [choice.last.to_s, choice.first.to_s] : [choice.to_s, choice.to_s] }
|
|
125
|
+
case value
|
|
126
|
+
when Array then value.map { |element| labels[element.to_s] || element.to_s }.join(', ')
|
|
127
|
+
when Hash then value.to_yaml.delete_prefix("---\n").strip
|
|
128
|
+
when true, false then I18n.t(value ? 'vario.values.on' : 'vario.values.off')
|
|
129
|
+
else
|
|
130
|
+
if model
|
|
131
|
+
record = model.find_by(model.primary_key => value)
|
|
132
|
+
record ? Topic.label_of(record) : value.to_s
|
|
133
|
+
else
|
|
134
|
+
labels[value.to_s] || value.to_s
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# 1.x's keys it takes: for its context roots the root (order_type) or,
|
|
140
|
+
# for a drop or a model, its id (location_id), and the legacy keys of
|
|
141
|
+
# variables in those roots (customer_id)
|
|
142
|
+
def legacy_keys
|
|
143
|
+
keys = context.map { |root, type| type.is_a?(Symbol) ? root : "#{root}_id" }
|
|
144
|
+
keys + Vario.config.legacy_keys.select { |_key, variable| context.key?(variable.split('.').first) }.keys
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The variable a 1.x key is now: a legacy key's (config.legacy_key),
|
|
148
|
+
# location for location_id when location is a root, else the key
|
|
149
|
+
def variable_for(key)
|
|
150
|
+
key = key.to_s
|
|
151
|
+
root = key.delete_suffix('_id')
|
|
152
|
+
Vario.config.legacy_keys[key] || (key.end_with?('_id') && context.key?(root) ? root : key)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def label
|
|
156
|
+
I18n.t(:label, scope: i18n_scope, default: key.humanize)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def description
|
|
160
|
+
I18n.t(:description, scope: i18n_scope, default: nil)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
private
|
|
164
|
+
|
|
165
|
+
def i18n_scope
|
|
166
|
+
[:vario, :topics, topic.key, :settings, key]
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
class << self
|
|
171
|
+
# Every topic subclass, as they are loaded (see Vario::Engine)
|
|
172
|
+
def registered
|
|
173
|
+
Vario::Topic.instance_variable_get(:@registered) || Vario::Topic.instance_variable_set(:@registered, [])
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def inherited(subclass)
|
|
177
|
+
super
|
|
178
|
+
registered << subclass
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Drops the topics a reload replaced, and loads those in app/settings/
|
|
182
|
+
# of the app and its engines, which register themselves as they load
|
|
183
|
+
def load!
|
|
184
|
+
@loading = true
|
|
185
|
+
registered.select! { |topic| topic.name && topic.name.safe_constantize.equal?(topic) }
|
|
186
|
+
([Rails.application] + Rails::Engine.subclasses.map(&:instance)).uniq.each do |engine|
|
|
187
|
+
directory = engine.root.join('app/settings')
|
|
188
|
+
Rails.autoloaders.main.eager_load_dir(directory) if directory.directory?
|
|
189
|
+
end
|
|
190
|
+
@loading = false
|
|
191
|
+
@loaded = true
|
|
192
|
+
@when_loaded.shift.call while @when_loaded&.any?
|
|
193
|
+
ensure
|
|
194
|
+
@loading = false
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Runs the block once the topics are loaded: now, or when they are (a
|
|
198
|
+
# model may load before them, or while they load: a topic may name it)
|
|
199
|
+
def when_loaded(&block)
|
|
200
|
+
return yield if @loaded && !@loading
|
|
201
|
+
|
|
202
|
+
(@when_loaded ||= []) << block
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# The topics of a settable, a record or its class (or all of them),
|
|
206
|
+
# by title
|
|
207
|
+
def for(settable = nil)
|
|
208
|
+
topics_of(settable).sort_by(&:title)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# A settable's topic by its key
|
|
212
|
+
def find(settable, key)
|
|
213
|
+
topics_of(settable).find { |topic| topic.key == key.to_s }
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# A settable's setting by its name (shipping.cutoff_time)
|
|
217
|
+
def definition(settable, name)
|
|
218
|
+
topics_of(settable).each do |topic|
|
|
219
|
+
definition = topic.definitions[name.to_s]
|
|
220
|
+
return definition if definition
|
|
221
|
+
end
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# DSL
|
|
226
|
+
|
|
227
|
+
def settable(klass = nil)
|
|
228
|
+
@settable = klass.to_s if klass
|
|
229
|
+
@settable
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def icon(icon = nil)
|
|
233
|
+
@icon = icon if icon
|
|
234
|
+
@icon
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# The roots refinements may look at: drops (order: OrderDrop) or plain
|
|
238
|
+
# values (order_type: :string)
|
|
239
|
+
def context(roots = nil)
|
|
240
|
+
@context = roots.to_h { |name, root| [name.to_s, root] } if roots
|
|
241
|
+
@context || {}
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def key(key = nil)
|
|
245
|
+
@key = key.to_s if key
|
|
246
|
+
@key || name.to_s.demodulize.delete_suffix('Settings').underscore
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# type: one of TYPES, or a model; name: for a setting whose name isn't
|
|
250
|
+
# the topic's key and its own (from 1.x, hide_couriers)
|
|
251
|
+
def setting(key, type, **options)
|
|
252
|
+
definition = Definition.new(self, key, type, **options)
|
|
253
|
+
definitions[definition.name] = definition
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Its settings by name, in the order they are defined
|
|
257
|
+
def definitions
|
|
258
|
+
@definitions ||= {}
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def settable_class
|
|
262
|
+
settable&.safe_constantize
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# A model's records, as a setting's value or in a refinement's rule:
|
|
266
|
+
# its vario_choices(settable) when it has them (a class method or
|
|
267
|
+
# scope), else the settable's (where(account: settable)), else all
|
|
268
|
+
def records_of(model, settable)
|
|
269
|
+
return model.vario_choices(settable) if model.respond_to?(:vario_choices)
|
|
270
|
+
|
|
271
|
+
association = settable.class.model_name.singular
|
|
272
|
+
model.reflect_on_association(association) ? model.where(association => settable) : model.all
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# A record's name, for people
|
|
276
|
+
def label_of(record)
|
|
277
|
+
(record.try(:display_name) || record.try(:name) || record.to_s).to_s
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def title
|
|
281
|
+
I18n.t(:title, scope: [:vario, :topics, key], default: key.humanize)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def description
|
|
285
|
+
I18n.t(:description, scope: [:vario, :topics, key], default: nil)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
private
|
|
289
|
+
|
|
290
|
+
def topics_of(settable)
|
|
291
|
+
topics = registered.select(&:settable_class)
|
|
292
|
+
return topics unless settable
|
|
293
|
+
|
|
294
|
+
klass = settable.is_a?(Class) ? settable : settable.class
|
|
295
|
+
topics.select { |topic| klass <= topic.settable_class }
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# The values of a topic's settings for a settable in a context, by the
|
|
5
|
+
# settings' keys (a boolean's also with a question mark)
|
|
6
|
+
#
|
|
7
|
+
# reader = account.settings(:shipping, order: order)
|
|
8
|
+
# reader.cutoff_time # => "15:00"
|
|
9
|
+
# reader.partial_shipping? # => true
|
|
10
|
+
# reader[:cutoff_time]
|
|
11
|
+
class TopicReader
|
|
12
|
+
def initialize(settable, topic, context = {})
|
|
13
|
+
@settable = settable
|
|
14
|
+
@topic = topic.is_a?(Class) ? topic : Topic.find(settable, topic)
|
|
15
|
+
raise UnknownSetting, "Topic '#{topic}' is not defined for #{settable.class.name}" unless @topic
|
|
16
|
+
|
|
17
|
+
@context = context
|
|
18
|
+
@definitions = @topic.definitions.values.index_by(&:key)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def [](key)
|
|
22
|
+
definition = @definitions.fetch(key.to_s) { raise UnknownSetting, "Setting '#{key}' is not defined in #{@topic.name}" }
|
|
23
|
+
@settable.explain(definition.name, @context).value
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# All of them, by key
|
|
27
|
+
def to_h
|
|
28
|
+
@definitions.transform_values { |definition| @settable.explain(definition.name, @context).value }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def respond_to_missing?(method, include_private = false)
|
|
32
|
+
@definitions.key?(method.to_s.delete_suffix('?')) || super
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def method_missing(method, *args)
|
|
36
|
+
key = method.to_s.delete_suffix('?')
|
|
37
|
+
return super unless args.empty? && @definitions.key?(key)
|
|
38
|
+
|
|
39
|
+
self[key]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/vario/types.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vario
|
|
4
|
+
# The ActiveModel types of the setting types Rails has none for (see
|
|
5
|
+
# Topic::Definition#value_type). Each casts what a form sends, what is
|
|
6
|
+
# stored, and what 1.x stored (strings), to the setting's value.
|
|
7
|
+
module Types
|
|
8
|
+
# Nothing, a value whose setting uses its default
|
|
9
|
+
def self.blank?(value)
|
|
10
|
+
value.nil? || (value.respond_to?(:empty?) && (value.is_a?(String) ? value.strip.empty? : value.empty?))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# A time of day as "13:00", as 1.x stored it and Boxture reads it
|
|
14
|
+
class TimeOfDay < ActiveModel::Type::Value
|
|
15
|
+
def type = :time_of_day
|
|
16
|
+
|
|
17
|
+
def cast(value)
|
|
18
|
+
case value
|
|
19
|
+
when Time, DateTime, ActiveSupport::TimeWithZone then value.strftime('%H:%M')
|
|
20
|
+
when String
|
|
21
|
+
hours, minutes = value.strip.match(/\A(\d{1,2})[:.](\d{2})(?::\d{2})?\z/)&.captures&.map(&:to_i)
|
|
22
|
+
format('%02d:%02d', hours, minutes) if hours && hours < 24 && minutes < 60
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# An array; from 1.x also a text of values separated by commas or lines
|
|
28
|
+
class List < ActiveModel::Type::Value
|
|
29
|
+
def type = :list
|
|
30
|
+
|
|
31
|
+
def cast(value)
|
|
32
|
+
values = value.is_a?(String) ? value.split(/,|\n/).map(&:strip) : Array(value)
|
|
33
|
+
values.reject { |element| Types.blank?(element) }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A hash; from 1.x also YAML (or JSON)
|
|
38
|
+
class Map < ActiveModel::Type::Value
|
|
39
|
+
def type = :map
|
|
40
|
+
|
|
41
|
+
def cast(value)
|
|
42
|
+
value = YAML.safe_load(value) if value.is_a?(String)
|
|
43
|
+
value.to_h.deep_stringify_keys if value.respond_to?(:to_h) && !value.is_a?(Array)
|
|
44
|
+
rescue Psych::Exception
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A record of a model, by its id: a record is its id
|
|
50
|
+
class Reference < ActiveModel::Type::Value
|
|
51
|
+
def initialize(model)
|
|
52
|
+
super()
|
|
53
|
+
@model = model
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def type = :reference
|
|
57
|
+
|
|
58
|
+
def cast(value)
|
|
59
|
+
value = value.id if value.is_a?(@model)
|
|
60
|
+
@model.type_for_attribute(@model.primary_key).cast(value)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/vario/version.rb
CHANGED