Allow nil values for settings

This commit is contained in:
Karl Erik Õunapuu 2020-08-12 12:08:21 +03:00
parent 49f11cce0b
commit e7eaba7963
3 changed files with 11 additions and 5 deletions

View file

@ -1,6 +1,5 @@
class SettingEntry < ApplicationRecord
validates :code, presence: true, uniqueness: true
validates :value, presence: true
validates :format, presence: true
validates :group, presence: true
validate :valid_value_format
@ -17,7 +16,7 @@ class SettingEntry < ApplicationRecord
def retrieve
method = VALUE_FORMATS[format]
send(method)
value.blank? ? nil : send(method)
end
def self.with_group(group_name)
@ -32,7 +31,8 @@ class SettingEntry < ApplicationRecord
stg_value = args[0].to_s
SettingEntry.find_by!(code: stg_code).update(value: stg_value)
else
SettingEntry.find_by!(code: method.to_s).retrieve
stg = SettingEntry.find_by(code: method.to_s)
stg ? stg.retrieve : nil
end
end