2023-07-26 21:29:13 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ResultOrderableElement < ApplicationRecord
|
2023-08-11 20:48:20 +08:00
|
|
|
validates :position, uniqueness: { scope: :result }
|
2023-07-26 21:29:13 +08:00
|
|
|
validate :check_result_relations
|
|
|
|
|
|
|
|
belongs_to :result, inverse_of: :result_orderable_elements, touch: true
|
|
|
|
belongs_to :orderable, polymorphic: true, inverse_of: :result_orderable_element
|
2023-10-04 19:59:23 +08:00
|
|
|
acts_as_list scope: :result, top_of_list: 0, sequential_updates: true
|
2023-07-26 21:29:13 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-08-11 20:48:20 +08:00
|
|
|
def check_result_relations
|
|
|
|
if result != orderable.result
|
2023-07-26 21:29:13 +08:00
|
|
|
errors.add(
|
|
|
|
:step_orderable_element,
|
|
|
|
I18n.t('activerecord.errors.models.result_orderable_element.attributes.result.wrong_result')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|