mirror of
https://github.com/internetee/registry.git
synced 2025-07-22 18:56:05 +02:00
Always return true/false for boolean Setting entries
This commit is contained in:
parent
8683964d22
commit
1732d6551a
1 changed files with 12 additions and 2 deletions
|
@ -5,6 +5,7 @@ class SettingEntry < ApplicationRecord
|
||||||
validates :group, presence: true
|
validates :group, presence: true
|
||||||
validate :validate_value_format
|
validate :validate_value_format
|
||||||
validate :validate_code_is_not_using_reserved_name
|
validate :validate_code_is_not_using_reserved_name
|
||||||
|
before_update :replace_boolean_nil_with_false
|
||||||
|
|
||||||
VALUE_FORMATS = {
|
VALUE_FORMATS = {
|
||||||
string: :string_format,
|
string: :string_format,
|
||||||
|
@ -17,7 +18,10 @@ class SettingEntry < ApplicationRecord
|
||||||
|
|
||||||
def retrieve
|
def retrieve
|
||||||
method = VALUE_FORMATS[format]
|
method = VALUE_FORMATS[format]
|
||||||
value.blank? ? nil : send(method)
|
return false if format == 'boolean' && value.blank?
|
||||||
|
return if value.blank?
|
||||||
|
|
||||||
|
send(method)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.with_group(group_name)
|
def self.with_group(group_name)
|
||||||
|
@ -43,7 +47,13 @@ class SettingEntry < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Validators
|
# Hooks
|
||||||
|
def replace_boolean_nil_with_false
|
||||||
|
return unless format == 'boolean'
|
||||||
|
|
||||||
|
self.value = value == 'true' ? 'true' : 'false'
|
||||||
|
end
|
||||||
|
|
||||||
def validate_code_is_not_using_reserved_name
|
def validate_code_is_not_using_reserved_name
|
||||||
disallowed = []
|
disallowed = []
|
||||||
ActiveRecord::Base.instance_methods.sort.each { |m| disallowed << m.to_s }
|
ActiveRecord::Base.instance_methods.sort.each { |m| disallowed << m.to_s }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue