Create SettingEntry model

This commit is contained in:
Karl Erik Õunapuu 2020-08-11 11:57:51 +03:00
parent 21d246cc3e
commit a0c21d005e
3 changed files with 136 additions and 1 deletions

View file

@ -0,0 +1,52 @@
class SettingEntry < ApplicationRecord
validates :code, presence: true, uniqueness: true
validates :value, presence: true
validates :format, presence: true
validate :valid_value_format
VALUE_FORMATS = {
string: :string_format,
integer: :integer_format,
boolean: :boolean_format,
hash: :hash_format,
array: :array_format,
}.with_indifferent_access.freeze
def valid_value_format
formats = VALUE_FORMATS.with_indifferent_access
errors.add(:format, :invalid) unless formats.keys.any? format
end
def string_format
value
end
def integer_format
value.to_i
end
def boolean_format
value == 'true'
end
def hash_format
JSON.parse(value)
end
def array_format
JSON.parse(value).to_a
end
def retrieve
method = VALUE_FORMATS[format]
send(method)
end
def self.method_missing(method, *args)
super(method, *args)
rescue NoMethodError
raise NoMethodError if method.to_s.include? '='
SettingEntry.find_by!(code: method.to_s).retrieve
end
end

View file

@ -0,0 +1,16 @@
class CreateSettingEntries < ActiveRecord::Migration[6.0]
def change
create_table :setting_entries do |t|
t.string :code, null: false, index: { unique: true }
t.string :value, null: false
t.string :group, null: false
t.string :format, null: false
# Versioning related
t.string :creator_str
t.string :updator_str
t.timestamps
end
end
end

View file

@ -31,6 +31,14 @@ COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
CREATE EXTENSION IF NOT EXISTS btree_gist WITH SCHEMA public;
--
-- Name: EXTENSION btree_gist; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION btree_gist IS 'support for indexing common datatypes in GiST';
--
-- Name: citext; Type: EXTENSION; Schema: -; Owner: -
--
@ -2254,6 +2262,42 @@ CREATE TABLE public.schema_migrations (
);
--
-- Name: setting_entries; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE public.setting_entries (
id bigint NOT NULL,
code character varying NOT NULL,
value character varying NOT NULL,
"group" character varying NOT NULL,
format character varying NOT NULL,
creator_str character varying,
updator_str character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);
--
-- Name: setting_entries_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.setting_entries_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: setting_entries_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.setting_entries_id_seq OWNED BY public.setting_entries.id;
--
-- Name: settings; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -2855,6 +2899,13 @@ ALTER TABLE ONLY public.registrars ALTER COLUMN id SET DEFAULT nextval('public.r
ALTER TABLE ONLY public.reserved_domains ALTER COLUMN id SET DEFAULT nextval('public.reserved_domains_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.setting_entries ALTER COLUMN id SET DEFAULT nextval('public.setting_entries_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -3321,6 +3372,14 @@ ALTER TABLE ONLY public.reserved_domains
ADD CONSTRAINT reserved_domains_pkey PRIMARY KEY (id);
--
-- Name: setting_entries_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY public.setting_entries
ADD CONSTRAINT setting_entries_pkey PRIMARY KEY (id);
--
-- Name: settings_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -3993,6 +4052,13 @@ CREATE INDEX index_registrant_verifications_on_created_at ON public.registrant_v
CREATE INDEX index_registrant_verifications_on_domain_id ON public.registrant_verifications USING btree (domain_id);
--
-- Name: index_setting_entries_on_code; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_setting_entries_on_code ON public.setting_entries USING btree (code);
--
-- Name: index_settings_on_thing_type_and_thing_id_and_var; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -4712,6 +4778,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200610090110'),
('20200630081231'),
('20200714115338'),
('20200807110611');
('20200807110611'),
('20200811074839');