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

View file

@ -0,0 +1,5 @@
class ChangeSettingEntryValueToAllowNil < ActiveRecord::Migration[6.0]
def change
change_column :setting_entries, :value, :string, null: true
end
end

View file

@ -2269,7 +2269,7 @@ CREATE TABLE public.schema_migrations (
CREATE TABLE public.setting_entries ( CREATE TABLE public.setting_entries (
id bigint NOT NULL, id bigint NOT NULL,
code character varying NOT NULL, code character varying NOT NULL,
value character varying DEFAULT ''::character varying NOT NULL, value character varying DEFAULT ''::character varying,
"group" character varying NOT NULL, "group" character varying NOT NULL,
format character varying NOT NULL, format character varying NOT NULL,
creator_str character varying, creator_str character varying,
@ -4779,6 +4779,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200630081231'), ('20200630081231'),
('20200714115338'), ('20200714115338'),
('20200807110611'), ('20200807110611'),
('20200811074839'); ('20200811074839'),
('20200812090409');