2020-07-18 00:04:09 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MyModuleStatus < ApplicationRecord
|
2022-03-25 22:38:15 +08:00
|
|
|
class MyModuleStatusTransitionError < StandardError
|
|
|
|
attr_reader :error
|
|
|
|
|
|
|
|
def initialize(error)
|
|
|
|
@error = error
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-18 00:04:09 +08:00
|
|
|
has_many :my_modules, dependent: :nullify
|
|
|
|
has_many :my_module_status_conditions, dependent: :destroy
|
|
|
|
has_many :my_module_status_consequences, dependent: :destroy
|
|
|
|
has_many :my_module_status_implications, dependent: :destroy
|
|
|
|
belongs_to :my_module_status_flow
|
|
|
|
belongs_to :created_by, class_name: 'User', optional: true
|
|
|
|
belongs_to :last_modified_by, class_name: 'User', optional: true
|
|
|
|
has_one :next_status, class_name: 'MyModuleStatus',
|
|
|
|
foreign_key: 'previous_status_id',
|
|
|
|
inverse_of: :previous_status,
|
|
|
|
dependent: :nullify
|
|
|
|
belongs_to :previous_status, class_name: 'MyModuleStatus', inverse_of: :next_status, optional: true
|
|
|
|
|
|
|
|
validates :name, presence: true, length: { minimum: Constants::NAME_MIN_LENGTH, maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
validates :color, presence: true
|
|
|
|
validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH }
|
|
|
|
validates :next_status, uniqueness: true, if: -> { next_status.present? }
|
|
|
|
validates :previous_status, uniqueness: true, if: -> { previous_status.present? }
|
|
|
|
validate :next_in_same_flow, if: -> { next_status.present? }
|
|
|
|
validate :previous_in_same_flow, if: -> { previous_status.present? }
|
|
|
|
|
2020-07-15 00:48:18 +08:00
|
|
|
def initial_status?
|
|
|
|
my_module_status_flow.initial_status == self
|
|
|
|
end
|
|
|
|
|
|
|
|
def final_status?
|
|
|
|
my_module_status_flow.final_status == self
|
|
|
|
end
|
2020-07-22 22:50:10 +08:00
|
|
|
|
2020-07-27 22:36:21 +08:00
|
|
|
def self.sort_by_position(order = :asc)
|
|
|
|
ordered_statuses, statuses = all.to_a.partition { |i| i.previous_status_id.nil? }
|
|
|
|
|
2021-07-23 17:56:28 +08:00
|
|
|
return [] if ordered_statuses.blank?
|
2020-07-27 22:36:21 +08:00
|
|
|
|
2021-07-23 17:56:28 +08:00
|
|
|
until statuses.blank?
|
2020-07-27 22:36:21 +08:00
|
|
|
next_element, statuses = statuses.partition { |i| ordered_statuses.last.id == i.previous_status_id }
|
2021-07-23 17:56:28 +08:00
|
|
|
if next_element.blank?
|
2020-07-27 22:36:21 +08:00
|
|
|
break
|
|
|
|
else
|
|
|
|
ordered_statuses.concat(next_element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ordered_statuses = ordered_statuses.reverse if order == :desc
|
|
|
|
ordered_statuses
|
|
|
|
end
|
|
|
|
|
2020-09-10 22:53:07 +08:00
|
|
|
def conditions_errors(my_module)
|
|
|
|
mm_copy = my_module.clone
|
|
|
|
mm_copy.errors.clear
|
2020-09-10 19:40:52 +08:00
|
|
|
|
2020-09-03 20:19:28 +08:00
|
|
|
my_module_status_conditions.each do |condition|
|
2020-09-10 22:53:07 +08:00
|
|
|
condition.call(mm_copy)
|
2020-09-03 20:19:28 +08:00
|
|
|
end
|
|
|
|
|
2020-09-10 22:53:07 +08:00
|
|
|
mm_copy.errors.messages&.values&.flatten
|
2020-09-03 20:19:28 +08:00
|
|
|
end
|
|
|
|
|
2023-08-24 20:52:11 +08:00
|
|
|
def light_color?
|
|
|
|
color == '#FFFFFF'
|
|
|
|
end
|
|
|
|
|
2020-07-18 00:04:09 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def next_in_same_flow
|
|
|
|
errors.add(:next_status, :different_flow) unless next_status.my_module_status_flow == my_module_status_flow
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_in_same_flow
|
|
|
|
errors.add(:previous_status, :different_flow) unless previous_status.my_module_status_flow == my_module_status_flow
|
|
|
|
end
|
|
|
|
end
|