master
 1class ExerciseSet < ApplicationRecord
 2  attribute :actual_repetitions, :integer
 3  attribute :actual_duration, :integer
 4  attribute :target_weight, :quantity
 5  belongs_to :exercise
 6  belongs_to :workout
 7  scope :for, ->(exercise) { where(exercise: exercise).in_order }
 8  scope :successful, -> { where("actual_repetitions = target_repetitions") }
 9  scope :work, -> { where(type: WorkSet.name) }
10  scope :in_order, -> { order(:created_at) }
11
12  def work?
13    type == WorkSet.name
14  end
15
16  def warm_up?
17    type == WarmUpSet.name
18  end
19
20  def weight_per_side
21    remaining_weight = target_weight - 45.lbs
22    if remaining_weight > 0
23      "#{(remaining_weight / 2).pretty_print}/side"
24    end
25  end
26
27  def success?
28    actual_repetitions == target_repetitions
29  end
30
31  def failed?
32    !success?
33  end
34
35  def started?
36    !actual_repetitions.nil?
37  end
38
39  def to_hash
40    {
41      id: id,
42      exercise_id: exercise.id,
43      type: type,
44      actual_duration: actual_duration,
45      actual_repetitions: actual_repetitions,
46      target_duration: target_duration,
47      target_repetitions: target_repetitions,
48      target_weight: target_weight.to_s,
49      weight_per_side: weight_per_side,
50    }
51  end
52end