summaryrefslogtreecommitdiff
path: root/app/models/exercise_set.rb
blob: 29a7f5a4fdda31d338f79daa341cab0ba02406b0 (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
41
42
43
44
45
46
47
48
49
50
51
52
class ExerciseSet < ApplicationRecord
  attribute :actual_repetitions, :integer
  attribute :actual_duration, :integer
  attribute :target_weight, :quantity
  belongs_to :exercise
  belongs_to :workout
  scope :for, ->(exercise) { where(exercise: exercise).in_order }
  scope :successful, -> { where("actual_repetitions = target_repetitions") }
  scope :work, -> { where(type: WorkSet.name) }
  scope :in_order, -> { order(:created_at) }

  def work?
    type == WorkSet.name
  end

  def warm_up?
    type == WarmUpSet.name
  end

  def weight_per_side
    remaining_weight = target_weight - 45.lbs
    if remaining_weight > 0
      "#{(remaining_weight / 2).pretty_print}/side"
    end
  end

  def success?
    actual_repetitions == target_repetitions
  end

  def failed?
    !success?
  end

  def started?
    !actual_repetitions.nil?
  end

  def to_hash
    {
      id: id,
      exercise_id: exercise.id,
      type: type,
      actual_duration: actual_duration,
      actual_repetitions: actual_repetitions,
      target_duration: target_duration,
      target_repetitions: target_repetitions,
      target_weight: target_weight.to_s,
      weight_per_side: weight_per_side,
    }
  end
end