system_settings 0.9.0 → 0.9.1

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