1
0
Fork 0

Add model RelationStatus#transitions

This commit is contained in:
Alex Kotov 2019-09-21 22:13:14 +05:00
parent 62017b4813
commit 9ec984c782
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 25 additions and 1 deletions

View file

@ -4,6 +4,16 @@ class RelationStatus < ApplicationRecord
CODENAME_RE = /\A[a-z][a-z0-9]*(_[a-z0-9]+)*\z/.freeze CODENAME_RE = /\A[a-z][a-z0-9]*(_[a-z0-9]+)*\z/.freeze
FORMAT_RE = /\A[^[:space:]]+(.*[^[:space:]]+)?\z/.freeze FORMAT_RE = /\A[^[:space:]]+(.*[^[:space:]]+)?\z/.freeze
################
# Associations #
################
has_many :transitions,
class_name: 'RelationTransition',
inverse_of: :from_status,
foreign_key: :from_status_id,
dependent: :restrict_with_exception
############### ###############
# Validations # # Validations #
############### ###############

View file

@ -9,6 +9,7 @@ class RelationTransition < ApplicationRecord
belongs_to :from_status, belongs_to :from_status,
class_name: 'RelationStatus', class_name: 'RelationStatus',
inverse_of: :transitions,
optional: true optional: true
belongs_to :to_status, belongs_to :to_status,

View file

@ -11,6 +11,16 @@ RSpec.describe RelationStatus do
end end
end end
describe '#transitions' do
it do
is_expected.to \
have_many(:transitions)
.class_name('RelationTransition')
.inverse_of(:from_status)
.dependent(:restrict_with_exception)
end
end
describe '#codename' do describe '#codename' do
def allow_value(*) def allow_value(*)
super.for :codename super.for :codename

View file

@ -8,7 +8,10 @@ RSpec.describe RelationTransition do
describe '#from_status' do describe '#from_status' do
it do it do
is_expected.to \ is_expected.to \
belong_to(:from_status).class_name('RelationStatus').optional belong_to(:from_status)
.class_name('RelationStatus')
.inverse_of(:transitions)
.optional
end end
it { is_expected.not_to validate_presence_of :from_status } it { is_expected.not_to validate_presence_of :from_status }