system_settings 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/system_settings/application_controller.rb +4 -4
- data/app/controllers/system_settings/settings_controller.rb +20 -22
- data/app/helpers/system_settings/application_helper.rb +3 -1
- data/app/models/system_settings/application_record.rb +2 -4
- data/app/models/system_settings/boolean_setting.rb +3 -5
- data/app/models/system_settings/configurator.rb +98 -100
- data/app/models/system_settings/decimal_list_setting.rb +3 -5
- data/app/models/system_settings/decimal_setting.rb +3 -5
- data/app/models/system_settings/errors/error.rb +2 -4
- data/app/models/system_settings/errors/not_found_error.rb +2 -4
- data/app/models/system_settings/errors/not_loaded_error.rb +2 -4
- data/app/models/system_settings/errors/settings_read_error.rb +2 -4
- data/app/models/system_settings/integer_list_setting.rb +3 -5
- data/app/models/system_settings/integer_setting.rb +3 -5
- data/app/models/system_settings/list_of_decimals_validator.rb +23 -25
- data/app/models/system_settings/list_of_integers_validator.rb +23 -25
- data/app/models/system_settings/list_of_strings_validator.rb +23 -25
- data/app/models/system_settings/setting.rb +3 -5
- data/app/models/system_settings/string_list_setting.rb +3 -5
- data/app/models/system_settings/string_setting.rb +6 -8
- data/app/models/system_settings/type/decimal_list.rb +27 -29
- data/app/models/system_settings/type/integer_list.rb +23 -25
- data/app/models/system_settings/type/string_list.rb +19 -21
- data/lib/system_settings/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 213bb372d94ea27ce951f424559b1e19812248cca9df0f66136d4776d9848fb8
|
4
|
+
data.tar.gz: 0530b07ab886e8e2515d957c839b37c1eb7817d90deb2e1e2996d10457c08efa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d4ea1661ba9b875628ae3ac32e26cb593607ce8e470bc651287d105e23889b6794a1db594dff754dc1fcf89f0118995b4dc544c1d4d9a8fbfc3391d20e532a6
|
7
|
+
data.tar.gz: 397e5ade494169a4ff92c6bab9abada406c4d5ae9b2e3725a1ac282ec2d7cb7a066f8f7c69a724826d1e20bd3dc97f4decf48a80c08c61c54ad8a80193bdfcea
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
class SystemSettings::ApplicationController < ActionController::Base
|
4
|
+
protect_from_forgery with: :exception
|
5
|
+
|
6
|
+
ActiveSupport.run_load_hooks(:system_settings_application_controller, self)
|
7
7
|
end
|
@@ -1,34 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
before_action :set_setting, only: [:edit, :show, :update]
|
3
|
+
class SystemSettings::SettingsController < SystemSettings::ApplicationController
|
4
|
+
RETURN_ATTRIBUTES = %w[id name type value description].freeze
|
5
|
+
before_action :set_setting, only: [:edit, :show, :update]
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def index
|
8
|
+
@settings = SystemSettings::Setting.order(:name)
|
9
|
+
end
|
11
10
|
|
12
|
-
|
11
|
+
def edit; end
|
13
12
|
|
14
|
-
|
13
|
+
def show; end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
15
|
+
def update
|
16
|
+
if @setting.update(setting_params)
|
17
|
+
redirect_to setting_path(@setting)
|
18
|
+
else
|
19
|
+
render :edit
|
22
20
|
end
|
21
|
+
end
|
23
22
|
|
24
|
-
|
23
|
+
private
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
def set_setting
|
26
|
+
@setting = SystemSettings::Setting.find(params[:id])
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
29
|
+
def setting_params
|
30
|
+
params.require(:setting).permit(:value)
|
33
31
|
end
|
34
32
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, inclusion: [true, false]
|
7
|
-
end
|
3
|
+
class SystemSettings::BooleanSetting < SystemSettings::Setting
|
4
|
+
attribute :value, :boolean
|
5
|
+
validates :value, inclusion: [true, false]
|
8
6
|
end
|
@@ -1,136 +1,134 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
payload[:items] = obj.items
|
16
|
-
end
|
3
|
+
class SystemSettings::Configurator
|
4
|
+
class << self
|
5
|
+
def from_file(file_name, kernel_class: Kernel)
|
6
|
+
file_name = file_name.to_path if file_name.respond_to?(:to_path)
|
7
|
+
raise SystemSettings::Errors::SettingsReadError, "The file name must either be a String or implement #to_path" unless file_name.is_a?(String)
|
8
|
+
raise SystemSettings::Errors::SettingsReadError, "#{file_name} file does not exist" unless File.exist?(file_name)
|
9
|
+
raise SystemSettings::Errors::SettingsReadError, "#{file_name} file not readable" unless File.readable?(file_name)
|
10
|
+
SystemSettings.instrument("system_settings.from_file", path: file_name) do |payload|
|
11
|
+
file_content = File.read(file_name)
|
12
|
+
new(kernel_class: kernel_class).tap do |obj|
|
13
|
+
obj.instance_eval(file_content, file_name, 1)
|
14
|
+
payload[:items] = obj.items
|
17
15
|
end
|
18
16
|
end
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
19
|
+
def purge
|
20
|
+
new.purge
|
23
21
|
end
|
22
|
+
end
|
24
23
|
|
25
|
-
|
24
|
+
attr_reader :items
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
26
|
+
def initialize(kernel_class: Kernel, &block)
|
27
|
+
@items = []
|
28
|
+
@kernel_class = kernel_class
|
29
|
+
return unless block_given?
|
30
|
+
if block.arity == 1
|
31
|
+
yield self
|
32
|
+
else
|
33
|
+
instance_exec(&block)
|
36
34
|
end
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
def string(name, value: nil, description: nil, &blk)
|
38
|
+
add(name, SystemSettings::StringSetting, value: value, description: description, &blk)
|
39
|
+
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
def string_list(name, value: nil, description: nil, &blk)
|
42
|
+
add(name, SystemSettings::StringListSetting, value: value || [], description: description, &blk)
|
43
|
+
end
|
45
44
|
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
def integer(name, value: nil, description: nil, &blk)
|
46
|
+
add(name, SystemSettings::IntegerSetting, value: value, description: description, &blk)
|
47
|
+
end
|
49
48
|
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
def integer_list(name, value: nil, description: nil, &blk)
|
50
|
+
add(name, SystemSettings::IntegerListSetting, value: value || [], description: description, &blk)
|
51
|
+
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
def boolean(name, value: nil, description: nil, &blk)
|
54
|
+
add(name, SystemSettings::BooleanSetting, value: value, description: description, &blk)
|
55
|
+
end
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
def decimal(name, value: nil, description: nil, &blk)
|
58
|
+
add(name, SystemSettings::DecimalSetting, value: value, description: description, &blk)
|
59
|
+
end
|
61
60
|
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
def decimal_list(name, value: nil, description: nil, &blk)
|
62
|
+
add(name, SystemSettings::DecimalListSetting, value: value || [], description: description, &blk)
|
63
|
+
end
|
65
64
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
create_or_update_item(item)
|
65
|
+
def persist(only: [])
|
66
|
+
SystemSettings.instrument("system_settings.persist", items: @items) do |payload|
|
67
|
+
if settings_table_exists?
|
68
|
+
SystemSettings::Setting.transaction do
|
69
|
+
if only.empty?
|
70
|
+
@items.each { |item| create_or_update_item(item) }
|
71
|
+
else
|
72
|
+
only.each do |wanted_name|
|
73
|
+
item = @items.find { |i| i[:name] == wanted_name } || begin
|
74
|
+
loaded_names = @items.empty? ? "(none)" : @items.map{ |i| i[:name] }.join("\n")
|
75
|
+
message = <<~MESSAGE.strip
|
76
|
+
Couldn't persist system setting #{wanted_name}. There are no items by this name. Could it be a typo?
|
77
|
+
|
78
|
+
Configurator has loaded following items:
|
79
|
+
#{loaded_names}
|
80
|
+
MESSAGE
|
81
|
+
raise(SystemSettings::Errors::NotLoadedError, message)
|
85
82
|
end
|
83
|
+
create_or_update_item(item)
|
86
84
|
end
|
87
85
|
end
|
88
|
-
payload[:success] = true
|
89
|
-
else
|
90
|
-
warn "SystemSettings: Settings table has not been created!"
|
91
|
-
payload[:success] = false
|
92
86
|
end
|
87
|
+
payload[:success] = true
|
88
|
+
else
|
89
|
+
warn "SystemSettings: Settings table has not been created!"
|
90
|
+
payload[:success] = false
|
93
91
|
end
|
94
92
|
end
|
93
|
+
end
|
95
94
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
95
|
+
def purge
|
96
|
+
SystemSettings.instrument("system_settings.purge") do |payload|
|
97
|
+
if settings_table_exists?
|
98
|
+
SystemSettings::Setting.delete_all
|
99
|
+
payload[:success] = true
|
100
|
+
else
|
101
|
+
payload[:success] = false
|
104
102
|
end
|
105
103
|
end
|
104
|
+
end
|
106
105
|
|
107
|
-
|
106
|
+
private
|
108
107
|
|
109
|
-
|
110
|
-
|
111
|
-
|
108
|
+
def warn(*args)
|
109
|
+
@kernel_class.warn(*args)
|
110
|
+
end
|
112
111
|
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
def settings_table_exists?
|
113
|
+
SystemSettings::Setting.table_exists?
|
114
|
+
end
|
116
115
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
def add(name, class_const, value:, description:)
|
117
|
+
value = yield(value) if block_given?
|
118
|
+
value = value.call if value.is_a?(Proc)
|
119
|
+
@items.push(name: name, class: class_const, value: value, description: description)
|
120
|
+
end
|
122
121
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
else
|
129
|
-
warn "SystemSettings: Type mismatch detected! Previously #{item[:name]} had type #{persisted_record.class.name} but you are loading #{item[:class].name}"
|
130
|
-
end
|
122
|
+
def create_or_update_item(item)
|
123
|
+
persisted_record = SystemSettings::Setting.find_by(name: item[:name])
|
124
|
+
if persisted_record
|
125
|
+
if persisted_record.class == item[:class]
|
126
|
+
persisted_record.update!(description: item[:description])
|
131
127
|
else
|
132
|
-
|
128
|
+
warn "SystemSettings: Type mismatch detected! Previously #{item[:name]} had type #{persisted_record.class.name} but you are loading #{item[:class].name}"
|
133
129
|
end
|
130
|
+
else
|
131
|
+
item[:class].create!(name: item[:name], value: item[:value], description: item[:description])
|
134
132
|
end
|
135
133
|
end
|
136
134
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, "system_settings/list_of_decimals": true
|
7
|
-
end
|
3
|
+
class SystemSettings::DecimalListSetting < SystemSettings::Setting
|
4
|
+
attribute :value, SystemSettings::Type::DecimalList.new(scale: 6)
|
5
|
+
validates :value, "system_settings/list_of_decimals": true
|
8
6
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, numericality: true
|
7
|
-
end
|
3
|
+
class SystemSettings::DecimalSetting < SystemSettings::Setting
|
4
|
+
attribute :value, :decimal, scale: 6
|
5
|
+
validates :value, numericality: true
|
8
6
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, "system_settings/list_of_integers": true
|
7
|
-
end
|
3
|
+
class SystemSettings::IntegerListSetting < SystemSettings::Setting
|
4
|
+
attribute :value, SystemSettings::Type::IntegerList.new(limit: 8)
|
5
|
+
validates :value, "system_settings/list_of_integers": true
|
8
6
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, numericality: { only_integer: true }
|
7
|
-
end
|
3
|
+
class SystemSettings::IntegerSetting < SystemSettings::Setting
|
4
|
+
attribute :value, :integer, limit: 8
|
5
|
+
validates :value, numericality: { only_integer: true }
|
8
6
|
end
|
@@ -1,37 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
SINGLE_REGEXP = /\A[+-]?\d+(?:\.\d+){0,1}\z/.freeze
|
3
|
+
class SystemSettings::ListOfDecimalsValidator < ActiveModel::EachValidator
|
4
|
+
LIST_REGEXP = /\A[+-]?\d+(?:\.\d+){0,1}(?:; *[+-]?\d+(?:\.\d+){0,1})*\z/.freeze
|
5
|
+
SINGLE_REGEXP = /\A[+-]?\d+(?:\.\d+){0,1}\z/.freeze
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
def validate_each(record, attr_name, value)
|
8
|
+
came_from_user = :"#{attr_name}_came_from_user?"
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
raw_value = record.read_attribute_before_type_cast(attr_name) if record.respond_to?(came_from_user) && record.public_send(came_from_user)
|
11
|
+
raw_value ||= value
|
13
12
|
|
14
|
-
|
13
|
+
raw_value = value if record_attribute_changed_in_place?(record, attr_name)
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
record.errors.add(attr_name, :not_a_list_of_decimals) unless matches_list_of_decimals_regexp?(raw_value)
|
16
|
+
end
|
18
17
|
|
19
|
-
|
18
|
+
private
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
def record_attribute_changed_in_place?(record, attr_name)
|
21
|
+
record.respond_to?(:attribute_changed_in_place?) &&
|
22
|
+
record.attribute_changed_in_place?(attr_name.to_s)
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
25
|
+
def matches_list_of_decimals_regexp?(raw_value)
|
26
|
+
case raw_value
|
27
|
+
when String
|
28
|
+
LIST_REGEXP.match?(raw_value)
|
29
|
+
when Array
|
30
|
+
raw_value.all? { |v| SINGLE_REGEXP.match?(v.to_s) }
|
31
|
+
else
|
32
|
+
false
|
35
33
|
end
|
36
34
|
end
|
37
35
|
end
|
@@ -1,37 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
SINGLE_REGEXP = /\A[+-]?\d+\z/.freeze
|
3
|
+
class SystemSettings::ListOfIntegersValidator < ActiveModel::EachValidator
|
4
|
+
LIST_REGEXP = /\A[+-]?\d+(?:; *[+-]?\d+)*\z/.freeze
|
5
|
+
SINGLE_REGEXP = /\A[+-]?\d+\z/.freeze
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
def validate_each(record, attr_name, value)
|
8
|
+
came_from_user = :"#{attr_name}_came_from_user?"
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
raw_value = record.read_attribute_before_type_cast(attr_name) if record.respond_to?(came_from_user) && record.public_send(came_from_user)
|
11
|
+
raw_value ||= value
|
13
12
|
|
14
|
-
|
13
|
+
raw_value = value if record_attribute_changed_in_place?(record, attr_name)
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
record.errors.add(attr_name, :not_a_list_of_integers) unless matches_list_of_integers_regexp?(raw_value)
|
16
|
+
end
|
18
17
|
|
19
|
-
|
18
|
+
private
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
def record_attribute_changed_in_place?(record, attr_name)
|
21
|
+
record.respond_to?(:attribute_changed_in_place?) &&
|
22
|
+
record.attribute_changed_in_place?(attr_name.to_s)
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
25
|
+
def matches_list_of_integers_regexp?(raw_value)
|
26
|
+
case raw_value
|
27
|
+
when String
|
28
|
+
LIST_REGEXP.match?(raw_value)
|
29
|
+
when Array
|
30
|
+
raw_value.all? { |v| SINGLE_REGEXP.match?(v.to_s) }
|
31
|
+
else
|
32
|
+
false
|
35
33
|
end
|
36
34
|
end
|
37
35
|
end
|
@@ -1,38 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
NON_WHITESPACE_REGEXP = /[^[:space:]]/.freeze
|
3
|
+
class SystemSettings::ListOfStringsValidator < ActiveModel::EachValidator
|
4
|
+
NON_WHITESPACE_REGEXP = /[^[:space:]]/.freeze
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
def validate_each(record, attr_name, value)
|
7
|
+
came_from_user = :"#{attr_name}_came_from_user?"
|
9
8
|
|
10
|
-
|
11
|
-
|
9
|
+
raw_value = record.read_attribute_before_type_cast(attr_name) if record.respond_to?(came_from_user) && record.public_send(came_from_user)
|
10
|
+
raw_value ||= value
|
12
11
|
|
13
|
-
|
12
|
+
raw_value = value if record_attribute_changed_in_place?(record, attr_name)
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
record.errors.add(attr_name, :not_a_list_of_strings) unless matches_list_of_strings_regexp?(raw_value)
|
15
|
+
end
|
17
16
|
|
18
|
-
|
17
|
+
private
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
def record_attribute_changed_in_place?(record, attr_name)
|
20
|
+
record.respond_to?(:attribute_changed_in_place?) &&
|
21
|
+
record.attribute_changed_in_place?(attr_name.to_s)
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
when Array
|
32
|
-
raw_value.all? { |v| NON_WHITESPACE_REGEXP.match?(v.to_s) }
|
33
|
-
else
|
34
|
-
false
|
24
|
+
def matches_list_of_strings_regexp?(raw_value)
|
25
|
+
case raw_value
|
26
|
+
when String
|
27
|
+
raw_value.split(SystemSettings::Type::StringList::DELIMITER_REGEXP).all? do |value|
|
28
|
+
NON_WHITESPACE_REGEXP.match?(value)
|
35
29
|
end
|
30
|
+
when Array
|
31
|
+
raw_value.all? { |v| NON_WHITESPACE_REGEXP.match?(v.to_s) }
|
32
|
+
else
|
33
|
+
false
|
36
34
|
end
|
37
35
|
end
|
38
36
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :name, presence: true, uniqueness: true
|
7
|
-
end
|
3
|
+
class SystemSettings::Setting < SystemSettings::ApplicationRecord
|
4
|
+
validates :type, presence: true
|
5
|
+
validates :name, presence: true, uniqueness: true
|
8
6
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, "system_settings/list_of_strings": true
|
7
|
-
end
|
3
|
+
class SystemSettings::StringListSetting < SystemSettings::Setting
|
4
|
+
attribute :value, SystemSettings::Type::StringList.new
|
5
|
+
validates :value, "system_settings/list_of_strings": true
|
8
6
|
end
|
@@ -1,13 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
validates :value, presence: true
|
3
|
+
class SystemSettings::StringSetting < SystemSettings::Setting
|
4
|
+
attribute :value, :string
|
5
|
+
validates :value, presence: true
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
7
|
+
def value=(original_value)
|
8
|
+
next_value = original_value.to_s.blank? ? nil : original_value.to_s.strip
|
9
|
+
super(next_value)
|
12
10
|
end
|
13
11
|
end
|
@@ -1,41 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module SystemSettings
|
4
|
-
|
5
|
-
|
6
|
-
SEPARATOR = ";"
|
3
|
+
module SystemSettings::Type
|
4
|
+
class DecimalList < ActiveModel::Type::Value
|
5
|
+
SEPARATOR = ";"
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def initialize(precision: nil, limit: nil, scale: nil)
|
8
|
+
super
|
9
|
+
@single_type = ActiveModel::Type::Decimal.new(precision: precision, limit: limit, scale: scale)
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def type
|
13
|
+
:decimal_list
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
16
|
+
def deserialize(value)
|
17
|
+
result = value.presence && JSON.parse(value)
|
18
|
+
if result.is_a?(Array)
|
19
|
+
result.map { |v| @single_type.cast(v) }
|
20
|
+
else
|
21
|
+
result
|
24
22
|
end
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
def serialize(value)
|
26
|
+
JSON.dump(value) unless value.nil?
|
27
|
+
end
|
29
28
|
|
30
|
-
|
29
|
+
private
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
31
|
+
def cast_value(value)
|
32
|
+
case value
|
33
|
+
when Array
|
34
|
+
value.map { |v| @single_type.cast(v) }
|
35
|
+
when String
|
36
|
+
value.split(SEPARATOR).map { |v| @single_type.cast(v) }
|
39
37
|
end
|
40
38
|
end
|
41
39
|
end
|
@@ -1,36 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module SystemSettings
|
4
|
-
|
5
|
-
|
6
|
-
SEPARATOR = ";"
|
3
|
+
module SystemSettings::Type
|
4
|
+
class IntegerList < ActiveModel::Type::Value
|
5
|
+
SEPARATOR = ";"
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def initialize(precision: nil, limit: nil, scale: nil)
|
8
|
+
super
|
9
|
+
@single_type = ActiveModel::Type::Integer.new(precision: precision, limit: limit, scale: scale)
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def type
|
13
|
+
:integer_list
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def deserialize(value)
|
17
|
+
value.presence && JSON.parse(value)
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
def serialize(value)
|
21
|
+
JSON.dump(value) unless value.nil?
|
22
|
+
end
|
24
23
|
|
25
|
-
|
24
|
+
private
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
26
|
+
def cast_value(value)
|
27
|
+
case value
|
28
|
+
when Array
|
29
|
+
value.map { |v| @single_type.cast(v) }
|
30
|
+
when String
|
31
|
+
value.split(SEPARATOR).map { |v| @single_type.cast(v) }
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
@@ -1,30 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module SystemSettings
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
3
|
+
module SystemSettings::Type
|
4
|
+
class StringList < ActiveModel::Type::Value
|
5
|
+
DELIMITER_REGEXP = /(?<=[^\\]);/.freeze
|
6
|
+
def type
|
7
|
+
:string_list
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def deserialize(value)
|
11
|
+
value.presence && JSON.parse(value)
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def serialize(value)
|
15
|
+
JSON.dump(value) unless value.nil?
|
16
|
+
end
|
18
17
|
|
19
|
-
|
18
|
+
private
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
20
|
+
def cast_value(value)
|
21
|
+
case value
|
22
|
+
when Array
|
23
|
+
value.map { |v| String(v).strip }
|
24
|
+
when String
|
25
|
+
value.split(DELIMITER_REGEXP).map(&:strip).map { |str| str.gsub("\\;", ";") }
|
28
26
|
end
|
29
27
|
end
|
30
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: system_settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Krists Ozols
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|