Implement label templates with default ZPL template [SCI-5902]

This commit is contained in:
Martin Artnik 2021-07-19 09:44:14 +02:00
parent 7940eb8465
commit a8f8ab8ba9
4 changed files with 110 additions and 2 deletions

View file

@ -351,7 +351,7 @@ Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
ExcludedMethods: ['describe', 'context']
IgnoredMethods: ['describe', 'context']
Metrics/ClassLength:
Enabled: false

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
class LabelTemplate < ApplicationRecord
enum language_type: { zpl: 0 }
validates :name, presence: true
validates :size, presence: true
validates :content, presence: true
def render(locals)
locals.reduce(content.dup) do |rendered_content, (key, value)|
rendered_content.gsub!(/\{\{#{key}\}\}/, value.to_s)
end
end
end

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
class LabelTemplate < ApplicationRecord
enum language_type: { zpl: 0 }
end
class CreateLabelTemplates < ActiveRecord::Migration[6.1]
def change
create_table :label_templates do |t|
t.string :name, null: false
t.text :content, null: false
t.integer :language_type, index: true
t.boolean :default, default: false, null: false
t.string :size
t.timestamps
end
LabelTemplate.create(
name: 'SciNote Item',
size: '1" x 0.5" / 25.4mm x 12.7mm',
language_type: :zpl,
default: true,
content:
<<~HEREDOC
^XA
^CF0,14
^FO10,10^FD{{item_id}}^FS
^FO10,10^BY1,2.0,20^BQN,2,3^FDMA{{item_id}}^FS
^FO80,33^FB100,4,0,L^FD{{item_name}}^FS^FS
^XZ
HEREDOC
)
end
end

View file

@ -555,6 +555,41 @@ CREATE SEQUENCE public.experiments_id_seq
ALTER SEQUENCE public.experiments_id_seq OWNED BY public.experiments.id;
--
-- Name: label_templates; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.label_templates (
id bigint NOT NULL,
name character varying NOT NULL,
content text NOT NULL,
language_type integer,
"default" boolean DEFAULT false NOT NULL,
size character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);
--
-- Name: label_templates_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.label_templates_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: label_templates_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.label_templates_id_seq OWNED BY public.label_templates.id;
--
-- Name: my_module_groups; Type: TABLE; Schema: public; Owner: -
--
@ -2974,6 +3009,13 @@ ALTER TABLE ONLY public.delayed_jobs ALTER COLUMN id SET DEFAULT nextval('public
ALTER TABLE ONLY public.experiments ALTER COLUMN id SET DEFAULT nextval('public.experiments_id_seq'::regclass);
--
-- Name: label_templates id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.label_templates ALTER COLUMN id SET DEFAULT nextval('public.label_templates_id_seq'::regclass);
--
-- Name: my_module_groups id; Type: DEFAULT; Schema: public; Owner: -
--
@ -3540,6 +3582,14 @@ ALTER TABLE ONLY public.experiments
ADD CONSTRAINT experiments_pkey PRIMARY KEY (id);
--
-- Name: label_templates label_templates_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.label_templates
ADD CONSTRAINT label_templates_pkey PRIMARY KEY (id);
--
-- Name: my_module_groups my_module_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -4391,6 +4441,13 @@ CREATE INDEX index_experiments_on_project_id ON public.experiments USING btree (
CREATE INDEX index_experiments_on_restored_by_id ON public.experiments USING btree (restored_by_id);
--
-- Name: index_label_templates_on_language_type; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_label_templates_on_language_type ON public.label_templates USING btree (language_type);
--
-- Name: index_my_module_groups_on_created_by_id; Type: INDEX; Schema: public; Owner: -
--
@ -7250,6 +7307,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210407143303'),
('20210410100006'),
('20210506125657'),
('20210622101238');
('20210622101238'),
('20210715125349'),
('20210716124649');