Always return true/false for boolean Setting entries

This commit is contained in:
Karl Erik Õunapuu 2020-08-18 11:28:52 +03:00
parent 8683964d22
commit 1732d6551a

View file

@ -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 }