mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-11-09 16:01:30 +08:00
Merge pull request #4032 from okriuchykhin/ok_SCI_6712
Add models for step orderable elements, migrate step descriptions [SCI-6712]
This commit is contained in:
commit
e15a382e37
9 changed files with 252 additions and 1 deletions
|
|
@ -23,6 +23,7 @@ class Checklist < ApplicationRecord
|
|||
has_many :report_elements,
|
||||
inverse_of: :checklist,
|
||||
dependent: :destroy
|
||||
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
|
||||
|
||||
accepts_nested_attributes_for :checklist_items,
|
||||
reject_if: :all_blank,
|
||||
|
|
|
|||
|
|
@ -25,8 +25,10 @@ class Step < ApplicationRecord
|
|||
belongs_to :user, inverse_of: :steps
|
||||
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
|
||||
belongs_to :protocol, inverse_of: :steps, touch: true
|
||||
has_many :step_orderable_elements, inverse_of: :step, dependent: :destroy
|
||||
has_many :checklists, inverse_of: :step, dependent: :destroy
|
||||
has_many :step_comments, foreign_key: :associated_id, dependent: :destroy
|
||||
has_many :step_texts, inverse_of: :step, dependent: :destroy
|
||||
has_many :step_assets, inverse_of: :step, dependent: :destroy
|
||||
has_many :assets, through: :step_assets
|
||||
has_many :step_tables, inverse_of: :step, dependent: :destroy
|
||||
|
|
|
|||
32
app/models/step_orderable_element.rb
Normal file
32
app/models/step_orderable_element.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class StepOrderableElement < ApplicationRecord
|
||||
validates :position, uniqueness: { scope: :step }
|
||||
validate :check_step_relations
|
||||
|
||||
around_destroy :decrement_following_elements_positions
|
||||
|
||||
belongs_to :step, inverse_of: :step_oerderable_elements, touch: true
|
||||
belongs_to :orderable, polymorphic: true, inverse_of: :step_orderable_elements
|
||||
|
||||
private
|
||||
|
||||
def check_step_relations
|
||||
if step != orderable.step
|
||||
errors.add(
|
||||
:step_orderable_element,
|
||||
I18n.t('activerecord.errors.models.step_orderable_elements.attributes.step.wrong_step')
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def decrement_following_elements_positions
|
||||
step.with_lock do
|
||||
yield
|
||||
step.step_orderable_elements.where('position > ?', position).find_each do |step_orderable_element|
|
||||
step_orderable_element.position -= 1
|
||||
step_orderable_element.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -5,4 +5,5 @@ class StepTable < ApplicationRecord
|
|||
|
||||
belongs_to :step, inverse_of: :step_tables, touch: true
|
||||
belongs_to :table, inverse_of: :step_table
|
||||
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
|
||||
end
|
||||
|
|
|
|||
12
app/models/step_text.rb
Normal file
12
app/models/step_text.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class StepText < ApplicationRecord
|
||||
include TinyMceImages
|
||||
|
||||
auto_strip_attributes :text, nullify: false
|
||||
validates :text, presence: true
|
||||
validates :text, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
|
||||
|
||||
belongs_to :step, inverse_of: :step_texts, touch: true
|
||||
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
|
||||
end
|
||||
|
|
@ -145,6 +145,10 @@ en:
|
|||
attributes:
|
||||
file:
|
||||
too_big: "is too big"
|
||||
step_orderable_element:
|
||||
attributes:
|
||||
step:
|
||||
wrong_step: "connected to the wrong step"
|
||||
repository_snapshot:
|
||||
attributes:
|
||||
selected:
|
||||
|
|
|
|||
42
db/migrate/20220414095100_add_step_text_and_migrate_data.rb
Normal file
42
db/migrate/20220414095100_add_step_text_and_migrate_data.rb
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require File.expand_path('app/helpers/database_helper')
|
||||
|
||||
class AddStepTextAndMigrateData < ActiveRecord::Migration[6.1]
|
||||
include DatabaseHelper
|
||||
|
||||
def up
|
||||
create_table :step_texts do |t|
|
||||
t.references :step, null: false, index: true, foreign_key: true
|
||||
t.string :text
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_gin_index_without_tags :step_texts, :text
|
||||
|
||||
Step.where.not(description: nil).find_in_batches(batch_size: 100) do |steps|
|
||||
step_texts = []
|
||||
|
||||
steps.each do |step|
|
||||
step_texts << step.step_texts.new(text: step.description)
|
||||
end
|
||||
StepText.import(step_texts, validate: false)
|
||||
end
|
||||
|
||||
Step.joins(:tiny_mce_assets, :step_texts)
|
||||
.preload(:tiny_mce_assets, :step_texts).find_each do |step|
|
||||
step_text_id = step.step_texts.take.id
|
||||
step.tiny_mce_assets.update_all(object_type: 'StepText', object_id: step_text_id)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
StepText.joins(:tiny_mce_assets)
|
||||
.preload(:step, :tiny_mce_assets).find_each do |step_text|
|
||||
step_text.tiny_mce_assets.update_all(object_type: 'Step', object_id: step_text.step.id)
|
||||
end
|
||||
|
||||
drop_table :step_texts
|
||||
end
|
||||
end
|
||||
13
db/migrate/20220414143955_create_step_orderable_elements.rb
Normal file
13
db/migrate/20220414143955_create_step_orderable_elements.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateStepOrderableElements < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :step_orderable_elements do |t|
|
||||
t.references :step, null: false, index: true, foreign_key: true
|
||||
t.integer :position
|
||||
t.references :orderable, polymorphic: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
146
db/structure.sql
146
db/structure.sql
|
|
@ -2486,6 +2486,40 @@ CREATE SEQUENCE public.step_assets_id_seq
|
|||
ALTER SEQUENCE public.step_assets_id_seq OWNED BY public.step_assets.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.step_orderable_elements (
|
||||
id bigint NOT NULL,
|
||||
step_id bigint NOT NULL,
|
||||
"position" integer,
|
||||
orderable_type character varying,
|
||||
orderable_id bigint,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE public.step_orderable_elements_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE public.step_orderable_elements_id_seq OWNED BY public.step_orderable_elements.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_tables; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -2517,6 +2551,38 @@ CREATE SEQUENCE public.step_tables_id_seq
|
|||
ALTER SEQUENCE public.step_tables_id_seq OWNED BY public.step_tables.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.step_texts (
|
||||
id bigint NOT NULL,
|
||||
step_id bigint NOT NULL,
|
||||
text character varying,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE public.step_texts_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE public.step_texts_id_seq OWNED BY public.step_texts.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: steps; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -3813,6 +3879,13 @@ ALTER TABLE ONLY public.settings ALTER COLUMN id SET DEFAULT nextval('public.set
|
|||
ALTER TABLE ONLY public.step_assets ALTER COLUMN id SET DEFAULT nextval('public.step_assets_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_orderable_elements ALTER COLUMN id SET DEFAULT nextval('public.step_orderable_elements_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_tables id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -3820,6 +3893,13 @@ ALTER TABLE ONLY public.step_assets ALTER COLUMN id SET DEFAULT nextval('public.
|
|||
ALTER TABLE ONLY public.step_tables ALTER COLUMN id SET DEFAULT nextval('public.step_tables_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_texts ALTER COLUMN id SET DEFAULT nextval('public.step_texts_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: steps id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -4532,6 +4612,14 @@ ALTER TABLE ONLY public.step_assets
|
|||
ADD CONSTRAINT step_assets_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements step_orderable_elements_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_orderable_elements
|
||||
ADD CONSTRAINT step_orderable_elements_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_tables step_tables_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -4540,6 +4628,14 @@ ALTER TABLE ONLY public.step_tables
|
|||
ADD CONSTRAINT step_tables_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts step_texts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_texts
|
||||
ADD CONSTRAINT step_texts_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: steps steps_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -6244,6 +6340,20 @@ CREATE UNIQUE INDEX index_settings_on_type ON public.settings USING btree (type)
|
|||
CREATE INDEX index_step_assets_on_step_id_and_asset_id ON public.step_assets USING btree (step_id, asset_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_step_orderable_elements_on_orderable; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_step_orderable_elements_on_orderable ON public.step_orderable_elements USING btree (orderable_type, orderable_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_step_orderable_elements_on_step_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_step_orderable_elements_on_step_id ON public.step_orderable_elements USING btree (step_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_step_tables_on_step_id_and_table_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -6251,6 +6361,20 @@ CREATE INDEX index_step_assets_on_step_id_and_asset_id ON public.step_assets USI
|
|||
CREATE UNIQUE INDEX index_step_tables_on_step_id_and_table_id ON public.step_tables USING btree (step_id, table_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_step_texts_on_step_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_step_texts_on_step_id ON public.step_texts USING btree (step_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_step_texts_on_text; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_step_texts_on_text ON public.step_texts USING gin (public.trim_html_tags((text)::text) public.gin_trgm_ops);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_steps_on_created_at; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -6955,6 +7079,14 @@ ALTER TABLE ONLY public.my_modules
|
|||
ADD CONSTRAINT fk_rails_2c8021ee5f FOREIGN KEY (archived_by_id) REFERENCES public.users(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_texts fk_rails_2cc1715bcd; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_texts
|
||||
ADD CONSTRAINT fk_rails_2cc1715bcd FOREIGN KEY (step_id) REFERENCES public.steps(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: repository_text_values fk_rails_2d9a33cbb0; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -7923,6 +8055,14 @@ ALTER TABLE ONLY public.activities
|
|||
ADD CONSTRAINT fk_rails_d3946086d2 FOREIGN KEY (my_module_id) REFERENCES public.my_modules(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: step_orderable_elements fk_rails_d3d1eee15c; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.step_orderable_elements
|
||||
ADD CONSTRAINT fk_rails_d3d1eee15c FOREIGN KEY (step_id) REFERENCES public.steps(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: notifications fk_rails_d44c385bb8; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
|
@ -8370,4 +8510,8 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20220310105144'),
|
||||
('20220321122111'),
|
||||
('20220325101011'),
|
||||
('20220328164215');
|
||||
('20220328164215'),
|
||||
('20220414095100'),
|
||||
('20220414143955');
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue