Refactor get/set method for EntrySetting.var

This commit is contained in:
Karl Erik Õunapuu 2020-08-12 15:42:32 +03:00
parent 558faf4934
commit 37ae30b637

View file

@ -27,18 +27,20 @@ class SettingEntry < ApplicationRecord
def self.method_missing(method, *args)
super(method, *args)
rescue NoMethodError
if method.to_s[-1] == '='
stg_code = method.to_s.sub('=', '')
stg_value = args[0].to_s
SettingEntry.find_by!(code: stg_code).update(value: stg_value)
else
stg = SettingEntry.find_by(code: method.to_s)
stg ? stg.retrieve : nil
end
get_or_set(method.to_s, args[0])
end
# rubocop:enable Style/MissingRespondToMissing
# rubocop:enable Style/MethodMissingSuper
def self.get_or_set(method_name, arg)
if method_name[-1] == '='
SettingEntry.find_by!(code: method_name.sub('=', '')).update(value: arg.to_s)
else
stg = SettingEntry.find_by(code: method_name)
stg ? stg.retrieve : nil
end
end
# Validators
def valid_value_format
formats = VALUE_FORMATS.with_indifferent_access