master
 1class Routine < ApplicationRecord
 2  belongs_to :program
 3  has_many :recommendations, inverse_of: :routine
 4  has_many :exercises, through: :recommendations
 5
 6  def slug
 7    name.parameterize
 8  end
 9
10  def to_param
11    slug
12  end
13
14  def to_s
15    name
16  end
17
18  def add_exercise(exercise, sets: 5, repetitions: 5, duration: nil)
19    repetitions = 1 if duration.present?
20    recommendations.create!(
21      duration: duration,
22      exercise: exercise,
23      repetitions: repetitions,
24      sets: sets,
25    ) unless exercises.include?(exercise)
26  end
27
28  def next_routine
29    program.next_routine_after(self)
30  end
31
32  def prepare_sets_for(user, workout)
33    exercises.primary.each do |exercise|
34      program.prepare_sets_for(user, exercise).each do |set|
35        workout.add_set(set)
36      end
37    end
38    workout
39  end
40end