Remove model RelationTransition
This commit is contained in:
parent
3cd9c9dae0
commit
50b3e28af1
12 changed files with 68 additions and 321 deletions
|
@ -7,18 +7,6 @@ class RelationStatus < ApplicationRecord
|
|||
|
||||
belongs_to :org_unit_kind, inverse_of: :relation_statuses
|
||||
|
||||
has_many :incoming_transitions,
|
||||
class_name: 'RelationTransition',
|
||||
inverse_of: :to_status,
|
||||
foreign_key: :to_status_id,
|
||||
dependent: :restrict_with_exception
|
||||
|
||||
has_many :outgoing_transitions,
|
||||
class_name: 'RelationTransition',
|
||||
inverse_of: :from_status,
|
||||
foreign_key: :from_status_id,
|
||||
dependent: :restrict_with_exception
|
||||
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RelationTransition < ApplicationRecord
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
||||
belongs_to :from_status,
|
||||
class_name: 'RelationStatus',
|
||||
inverse_of: :outgoing_transitions,
|
||||
optional: true
|
||||
|
||||
belongs_to :to_status,
|
||||
class_name: 'RelationStatus',
|
||||
inverse_of: :incoming_transitions
|
||||
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
||||
validates :name, good_small_text: true, uniqueness: true
|
||||
|
||||
validates :to_status, uniqueness: { scope: :from_status }
|
||||
|
||||
validate :statuses_are_not_equal
|
||||
|
||||
private
|
||||
|
||||
def statuses_are_not_equal
|
||||
return if from_status.nil? || to_status.nil? || from_status != to_status
|
||||
|
||||
errors.add :to_status
|
||||
end
|
||||
end
|
|
@ -5,43 +5,21 @@
|
|||
RelationStatus.model_name.human(count: 1),
|
||||
) %>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<dl>
|
||||
<dt><%= RelationStatus.human_attribute_name :org_unit_kind %></dt>
|
||||
<dd>
|
||||
<% if policy([:staff, @relation_status.org_unit_kind]).show? %>
|
||||
<%= link_to @relation_status.org_unit_kind.name,
|
||||
[:staff, @relation_status.org_unit_kind] %>
|
||||
<% else %>
|
||||
<%= @relation_status.org_unit_kind.name %>
|
||||
<% end %>
|
||||
</dd>
|
||||
<dl>
|
||||
<dt><%= RelationStatus.human_attribute_name :org_unit_kind %></dt>
|
||||
<dd>
|
||||
<% if policy([:staff, @relation_status.org_unit_kind]).show? %>
|
||||
<%= link_to @relation_status.org_unit_kind.name,
|
||||
[:staff, @relation_status.org_unit_kind] %>
|
||||
<% else %>
|
||||
<%= @relation_status.org_unit_kind.name %>
|
||||
<% end %>
|
||||
</dd>
|
||||
|
||||
<dt><%= RelationStatus.human_attribute_name :codename %></dt>
|
||||
<dd><%= @relation_status.codename %></dd>
|
||||
<dt><%= RelationStatus.human_attribute_name :codename %></dt>
|
||||
<dd><%= @relation_status.codename %></dd>
|
||||
|
||||
<dt><%= RelationStatus.human_attribute_name :name %></dt>
|
||||
<dd><%= @relation_status.name %></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<h4><%= RelationStatus.human_attribute_name :outgoing_transitions %></h5>
|
||||
|
||||
<ul class="list-group mt-3">
|
||||
<% @relation_status.outgoing_transitions.each do |relation_transition| %>
|
||||
<li class="list-group-item">
|
||||
<div class="d-flex justify-content-between">
|
||||
<%= relation_transition.name %>
|
||||
<small class="text-muted">
|
||||
<i class="fas fa-arrow-right"></i>
|
||||
<%= relation_transition.to_status.name %>
|
||||
</small>
|
||||
</div>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<dt><%= RelationStatus.human_attribute_name :name %></dt>
|
||||
<dd><%= @relation_status.name %></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
|
|
@ -116,7 +116,6 @@ en:
|
|||
org_unit_kind: Unit type
|
||||
codename: Codename
|
||||
name: Name
|
||||
outgoing_transitions: Outgoing transitions
|
||||
relationship:
|
||||
id: ID
|
||||
from_date: From date
|
||||
|
|
|
@ -116,7 +116,6 @@ ru:
|
|||
org_unit_kind: Тип подразделений
|
||||
codename: Кодовое имя
|
||||
name: Название
|
||||
outgoing_transitions: Исходящие переходы
|
||||
relationship:
|
||||
id: ID
|
||||
from_date: Дата начала
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
| supporter | Принять в сторонники | |
|
||||
supporter | active_member | Принять в члены | 1 month |
|
||||
supporter | exited_supporter | Выйти из сторонников | |
|
||||
supporter | excluded_supporter | Исключить из сторонников | |
|
||||
active_member | pending_member | Приостановить членство | |
|
||||
active_member | exited_member | Выйти из членов | |
|
||||
active_member | excluded_member | Исключить из членов | |
|
||||
pending_member | active_member | Возобновить членство | |
|
||||
exited_supporter | supporter | Принять в сторонники бывшего сторонника | |
|
||||
exited_member | supporter | Принять в сторонники бывшего члена | 1 year |
|
||||
excluded_supporter | supporter | Принять в сторонники исключённого сторонника | 3 years |
|
||||
excluded_member | supporter | Принять в сторонники исключённого члена | 3 years |
|
|
51
db/migrate/20191002113932_remove_relation_transitions.rb
Normal file
51
db/migrate/20191002113932_remove_relation_transitions.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RemoveRelationTransitions < ActiveRecord::Migration[6.0]
|
||||
include Partynest::Migration
|
||||
|
||||
def change
|
||||
remove_constraint :relation_transitions, :name, <<~SQL
|
||||
is_good_small_text(name)
|
||||
SQL
|
||||
|
||||
remove_constraint :relation_transitions, :statuses, <<~SQL
|
||||
from_status_id != to_status_id
|
||||
SQL
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<~SQL
|
||||
DROP INDEX
|
||||
index_relation_transitions_on_to_status_id_when_from_status_id_is_null
|
||||
SQL
|
||||
end
|
||||
|
||||
dir.down do
|
||||
execute <<~SQL
|
||||
CREATE UNIQUE INDEX
|
||||
index_relation_transitions_on_to_status_id_when_from_status_id_is_null
|
||||
ON relation_transitions
|
||||
USING btree
|
||||
(to_status_id)
|
||||
WHERE from_status_id IS NULL;
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
drop_table :relation_transitions do |t|
|
||||
t.timestamps null: false
|
||||
|
||||
t.references :from_status,
|
||||
null: true,
|
||||
foreign_key: { to_table: :relation_statuses }
|
||||
|
||||
t.references :to_status,
|
||||
null: false,
|
||||
foreign_key: { to_table: :relation_statuses }
|
||||
|
||||
t.string :name, null: false, index: { unique: true }
|
||||
|
||||
t.index %i[from_status_id to_status_id], unique: true
|
||||
end
|
||||
end
|
||||
end
|
17
db/seeds.rb
17
db/seeds.rb
|
@ -77,23 +77,6 @@ do |(org_unit_kind, codename, name)|
|
|||
end
|
||||
end
|
||||
|
||||
csv_foreach :relation_transitions \
|
||||
do |(from, to, name)|
|
||||
from.strip!
|
||||
to.strip!
|
||||
name.strip!
|
||||
|
||||
from_status = RelationStatus.find_by! codename: from unless from.empty?
|
||||
to_status = RelationStatus.find_by! codename: to
|
||||
|
||||
RelationTransition.where(
|
||||
from_status: from_status,
|
||||
to_status: to_status,
|
||||
).first_or_create! do |new_relation_transition|
|
||||
new_relation_transition.name = name
|
||||
end
|
||||
end
|
||||
|
||||
Rails.application.settings(:superuser).tap do |config|
|
||||
user = User.where(email: config[:email]).first_or_create! do |new_user|
|
||||
new_user.password = config[:password]
|
||||
|
|
104
db/structure.sql
104
db/structure.sql
|
@ -904,41 +904,6 @@ CREATE SEQUENCE public.relation_statuses_id_seq
|
|||
ALTER SEQUENCE public.relation_statuses_id_seq OWNED BY public.relation_statuses.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.relation_transitions (
|
||||
id bigint NOT NULL,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL,
|
||||
from_status_id bigint,
|
||||
to_status_id bigint NOT NULL,
|
||||
name character varying NOT NULL,
|
||||
CONSTRAINT name CHECK (public.is_good_small_text((name)::text)),
|
||||
CONSTRAINT statuses CHECK ((from_status_id <> to_status_id))
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE public.relation_transitions_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE public.relation_transitions_id_seq OWNED BY public.relation_transitions.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: relationships; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1195,13 +1160,6 @@ ALTER TABLE ONLY public.person_comments ALTER COLUMN id SET DEFAULT nextval('pub
|
|||
ALTER TABLE ONLY public.relation_statuses ALTER COLUMN id SET DEFAULT nextval('public.relation_statuses_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.relation_transitions ALTER COLUMN id SET DEFAULT nextval('public.relation_transitions_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relationships id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1342,14 +1300,6 @@ ALTER TABLE ONLY public.relation_statuses
|
|||
ADD CONSTRAINT relation_statuses_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions relation_transitions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.relation_transitions
|
||||
ADD CONSTRAINT relation_transitions_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relationships relationships_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1607,41 +1557,6 @@ CREATE UNIQUE INDEX index_relation_statuses_on_name ON public.relation_statuses
|
|||
CREATE INDEX index_relation_statuses_on_org_unit_kind_id ON public.relation_statuses USING btree (org_unit_kind_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relation_transitions_on_from_status_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_relation_transitions_on_from_status_id ON public.relation_transitions USING btree (from_status_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relation_transitions_on_from_status_id_and_to_status_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX index_relation_transitions_on_from_status_id_and_to_status_id ON public.relation_transitions USING btree (from_status_id, to_status_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relation_transitions_on_name; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX index_relation_transitions_on_name ON public.relation_transitions USING btree (name);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relation_transitions_on_to_status_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_relation_transitions_on_to_status_id ON public.relation_transitions USING btree (to_status_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relation_transitions_on_to_status_id_when_from_status_id_; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX index_relation_transitions_on_to_status_id_when_from_status_id_ ON public.relation_transitions USING btree (to_status_id) WHERE (from_status_id IS NULL);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_relationships_on_from_date; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1823,14 +1738,6 @@ ALTER TABLE ONLY public.users
|
|||
ADD CONSTRAINT fk_rails_61ac11da2b FOREIGN KEY (account_id) REFERENCES public.accounts(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions fk_rails_620f685310; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.relation_transitions
|
||||
ADD CONSTRAINT fk_rails_620f685310 FOREIGN KEY (to_status_id) REFERENCES public.relation_statuses(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: accounts fk_rails_777d10a224; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1887,14 +1794,6 @@ ALTER TABLE ONLY public.person_comments
|
|||
ADD CONSTRAINT fk_rails_a9c7b4ae11 FOREIGN KEY (account_id) REFERENCES public.accounts(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relation_transitions fk_rails_b61956945e; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.relation_transitions
|
||||
ADD CONSTRAINT fk_rails_b61956945e FOREIGN KEY (from_status_id) REFERENCES public.relation_statuses(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: relationships fk_rails_b943fd3c34; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1972,6 +1871,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20190930215223'),
|
||||
('20191001022049'),
|
||||
('20191001211809'),
|
||||
('20191002002101');
|
||||
('20191002002101'),
|
||||
('20191002113932');
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :some_relation_transition, class: RelationTransition do
|
||||
association :from_status, factory: :some_relation_status
|
||||
association :to_status, factory: :some_relation_status
|
||||
|
||||
name { Faker::Company.unique.name }
|
||||
end
|
||||
end
|
|
@ -20,28 +20,6 @@ RSpec.describe RelationStatus do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#incoming_transitions' do
|
||||
it do
|
||||
is_expected.to \
|
||||
have_many(:incoming_transitions)
|
||||
.class_name('RelationTransition')
|
||||
.inverse_of(:to_status)
|
||||
.with_foreign_key(:to_status_id)
|
||||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#outgoing_transitions' do
|
||||
it do
|
||||
is_expected.to \
|
||||
have_many(:outgoing_transitions)
|
||||
.class_name('RelationTransition')
|
||||
.inverse_of(:from_status)
|
||||
.with_foreign_key(:from_status_id)
|
||||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#codename' do
|
||||
def allow_value(*)
|
||||
super.for :codename
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe RelationTransition do
|
||||
subject { create :some_relation_transition }
|
||||
|
||||
describe '#from_status' do
|
||||
it do
|
||||
is_expected.to \
|
||||
belong_to(:from_status)
|
||||
.class_name('RelationStatus')
|
||||
.inverse_of(:outgoing_transitions)
|
||||
.optional
|
||||
end
|
||||
|
||||
it { is_expected.not_to validate_presence_of :from_status }
|
||||
end
|
||||
|
||||
describe '#to_status' do
|
||||
def allow_value(*)
|
||||
super.for :to_status
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to \
|
||||
belong_to(:to_status)
|
||||
.class_name('RelationStatus')
|
||||
.inverse_of(:incoming_transitions)
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to validate_presence_of(:to_status).with_message(:required)
|
||||
end
|
||||
|
||||
# Does not work with relations, but I don't want to use IDs.
|
||||
# Will wait while matcher will be upgraded.
|
||||
xit do
|
||||
is_expected.to \
|
||||
validate_uniqueness_of(:to_status_id).scoped_to(:from_status_id)
|
||||
end
|
||||
|
||||
it { is_expected.not_to allow_value subject.from_status }
|
||||
end
|
||||
|
||||
describe '#name' do
|
||||
def allow_value(*)
|
||||
super.for :name
|
||||
end
|
||||
|
||||
it { is_expected.to validate_presence_of :name }
|
||||
it { is_expected.to validate_uniqueness_of :name }
|
||||
|
||||
it do
|
||||
is_expected.to validate_length_of(:name).is_at_least(1).is_at_most(255)
|
||||
end
|
||||
|
||||
it { is_expected.not_to allow_value nil }
|
||||
it { is_expected.not_to allow_value '' }
|
||||
it { is_expected.not_to allow_value ' ' }
|
||||
|
||||
it { is_expected.to allow_value Faker::Name.name }
|
||||
it { is_expected.to allow_value Faker::Name.first_name }
|
||||
it { is_expected.to allow_value 'Foo Bar' }
|
||||
|
||||
it { is_expected.not_to allow_value ' Foo' }
|
||||
it { is_expected.not_to allow_value 'Foo ' }
|
||||
it { is_expected.not_to allow_value "\tFoo" }
|
||||
it { is_expected.not_to allow_value "Foo\t" }
|
||||
it { is_expected.not_to allow_value "\nFoo" }
|
||||
it { is_expected.not_to allow_value "Foo\n" }
|
||||
end
|
||||
end
|
Reference in a new issue