summaryrefslogtreecommitdiff
path: root/app/models/routine.rb
blob: 2eb008c67a3d121bffc88209704a7eb9f3b7f7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Routine < ApplicationRecord
  belongs_to :program
  has_many :recommendations, inverse_of: :routine
  has_many :exercises, through: :recommendations

  def slug
    name.parameterize
  end

  def to_param
    slug
  end

  def to_s
    name
  end

  def add_exercise(exercise, sets: 5, repetitions: 5, duration: nil)
    repetitions = 1 if duration.present?
    recommendations.create!(
      duration: duration,
      exercise: exercise,
      repetitions: repetitions,
      sets: sets,
    ) unless exercises.include?(exercise)
  end

  def next_routine
    program.next_routine_after(self)
  end

  def prepare_sets_for(user, workout)
    exercises.primary.each do |exercise|
      program.prepare_sets_for(user, exercise).each do |set|
        workout.add_set(set)
      end
    end
    workout
  end
end