Files
codeocean/app/models/exercise_tip.rb
2024-05-21 19:42:26 +02:00

23 lines
689 B
Ruby

# frozen_string_literal: true
class ExerciseTip < ApplicationRecord
belongs_to :exercise
belongs_to :tip
belongs_to :parent_exercise_tip, class_name: 'ExerciseTip', optional: true
attr_accessor :children
# Ensure no parent tip is set if current tip has rank == 1
validates :rank, exclusion: {in: [1]}, if: :parent_exercise_tip_id?
validate :tip_chain?, if: :parent_exercise_tip_id?
def tip_chain?
# Ensure each referenced parent exercise tip is set for this exercise
unless ExerciseTip.exists?(
exercise:, id: parent_exercise_tip
)
errors.add :parent_exercise_tip, :together, attribute: ExerciseTip.human_attribute_name('tip')
end
end
end