From e7eaba79633a9df094f5ca816934777e98c39b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Wed, 12 Aug 2020 12:08:21 +0300 Subject: [PATCH] Allow nil values for settings --- app/models/setting_entry.rb | 6 +++--- ...0200812090409_change_setting_entry_value_to_allow_nil.rb | 5 +++++ db/structure.sql | 5 +++-- 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20200812090409_change_setting_entry_value_to_allow_nil.rb diff --git a/app/models/setting_entry.rb b/app/models/setting_entry.rb index 35933af25..b439187d5 100644 --- a/app/models/setting_entry.rb +++ b/app/models/setting_entry.rb @@ -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 diff --git a/db/migrate/20200812090409_change_setting_entry_value_to_allow_nil.rb b/db/migrate/20200812090409_change_setting_entry_value_to_allow_nil.rb new file mode 100644 index 000000000..227e0bf85 --- /dev/null +++ b/db/migrate/20200812090409_change_setting_entry_value_to_allow_nil.rb @@ -0,0 +1,5 @@ +class ChangeSettingEntryValueToAllowNil < ActiveRecord::Migration[6.0] + def change + change_column :setting_entries, :value, :string, null: true + end +end diff --git a/db/structure.sql b/db/structure.sql index 39c891326..45b48733f 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2269,7 +2269,7 @@ CREATE TABLE public.schema_migrations ( CREATE TABLE public.setting_entries ( id bigint 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, format character varying NOT NULL, creator_str character varying, @@ -4779,6 +4779,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200630081231'), ('20200714115338'), ('20200807110611'), -('20200811074839'); +('20200811074839'), +('20200812090409');