Add database support and model for tips
This commit is contained in:
18
app/models/exercise_tip.rb
Normal file
18
app/models/exercise_tip.rb
Normal file
@ -0,0 +1,18 @@
|
||||
# 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
|
||||
errors.add :parent_exercise_tip, I18n.t('activerecord.errors.messages.together', attribute: I18n.t('activerecord.attributes.exercise_tip.tip')) unless ExerciseTip.exists?(exercise: exercise, tip: parent_exercise_tip)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user