Skip to content

Commit

Permalink
Refactor how weekly mileage is calculated.
Browse files Browse the repository at this point in the history
* Add distance_in_miles column to activities table
  • Loading branch information
Steve Polito committed Aug 26, 2020
1 parent c02605f commit 9237bd0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
38 changes: 14 additions & 24 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Activity < ApplicationRecord
has_rich_text :description

before_validation :calculate_duration
before_save :calculate_distance_in_miles
before_save :calculate_pace
after_save :create_or_update_total
after_destroy :subract_from_total
Expand All @@ -33,24 +34,25 @@ def require_unit_if_distance_set
errors.add(:base, "Please select a unit") if self.distance.present? && self.unit.nil?
end

def calculate_pace
if self.unit.present? && self.distance.present? && self.duration.present?
def calculate_distance_in_miles
if self.unit.present? && self.distance.present?
case self.unit
when "miles"
self.calculated_pace = self.duration / self.distance
self.distance_in_miles = self.distance
when "kilometers"
converted_distance = self.distance * 0.6213712
self.calculated_pace = self.duration / converted_distance
self.distance_in_miles = self.distance * 0.6213712
when "meters"
converted_distance = self.distance * 0.0006213711985
self.calculated_pace = self.duration / converted_distance
self.distance_in_miles = self.distance * 0.0006213711985
when "yards"
converted_distance = self.distance * 0.0005681818239083977
self.calculated_pace = self.duration / converted_distance
self.distance_in_miles = self.distance * 0.0005681818239083977
end
end
end

def calculate_pace
self.calculated_pace = self.duration / self.distance_in_miles if self.distance_in_miles.present?
end

def calculate_duration
calculated_duration = 0

Expand All @@ -66,23 +68,11 @@ def create_or_update_total
year = self.date.to_date.cwyear
starting_on = Date.commercial(year, week)

original_duration = self.duration

case self.unit
when "miles"
converted_distance = self.distance
when "kilometers"
converted_distance = self.distance * 0.6213712
when "meters"
converted_distance = self.distance * 0.0006213711985
when "yards"
converted_distance = self.distance * 0.0005681818239083977
end

@total = Total.find_or_initialize_by(user: self.user, starting_on: starting_on, range: "week")
@activities = Activity.where("date >= ?", starting_on).where("date <= ?", starting_on.end_of_week).where(user: self.user)

total_distance = converted_distance + @total.distance unless converted_distance.nil?
total_duration = original_duration + @total.duration unless original_duration.nil?
total_distance = @activities.sum(:distance_in_miles) unless @activities.empty?
total_duration = @activities.sum(:duration) unless @activities.empty?

@total.distance = total_distance unless total_distance.nil?
@total.duration = total_duration unless total_duration.nil?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDistanceInMilesToActivities < ActiveRecord::Migration[6.0]
def change
add_column :activities, :distance_in_miles, :decimal, precision: 5, scale: 2
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_08_22_121917) do
ActiveRecord::Schema.define(version: 2020_08_26_101427) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -60,6 +60,7 @@
t.integer "hours"
t.integer "minutes"
t.integer "seconds"
t.decimal "distance_in_miles", precision: 5, scale: 2
t.index ["user_id"], name: "index_activities_on_user_id"
end

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/activities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ confirmed_user_activity_<%= i %>:
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
distance_in_miles: <%= rand(1..26) %>
<% end %>

<% 1.upto(180) do |i| %>
Expand All @@ -20,6 +21,7 @@ another_confirmed_user_activity_<%= i %>:
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
distance_in_miles: <%= rand(1..26) %>
<% end %>

confirmed_user_with_searchable_activity_1:
Expand All @@ -29,6 +31,7 @@ confirmed_user_with_searchable_activity_1:
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
distance_in_miles: <%= rand(1..26) %>
unit: miles
difficulty: easy
category: run
Expand All @@ -40,6 +43,7 @@ confirmed_user_with_searchable_activity_2:
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
distance_in_miles: <%= rand(1..26) %>
unit: miles
difficulty: moderate
category: long_run
Expand All @@ -51,6 +55,7 @@ confirmed_user_with_searchable_activity_3:
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
distance_in_miles: <%= rand(1..26) %>
unit: miles
difficulty: hard
category: workout
Expand All @@ -62,6 +67,7 @@ confirmed_user_with_searchable_activity_4:
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
distance_in_miles: <%= rand(1..26) %>
unit: miles
difficulty: hard
category: race
9 changes: 3 additions & 6 deletions test/models/activity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,23 @@ def setup
@activity.unit = :kilometers
@activity.duration = 1800
@activity.save
converted_distance = @activity.distance * 0.6213712
pace = @activity.duration / converted_distance
pace = @activity.duration / @activity.distance_in_miles

assert_equal pace, @activity.reload.calculated_pace

@activity.distance = 800
@activity.unit = :meters
@activity.duration = 120
@activity.save
converted_distance = @activity.distance * 0.0006213711985
pace = @activity.duration / converted_distance
pace = @activity.duration / @activity.distance_in_miles

assert_equal pace, @activity.reload.calculated_pace

@activity.distance = 600
@activity.unit = :yards
@activity.duration = 90
@activity.save
converted_distance = @activity.distance * 0.0005681818239083977
pace = @activity.duration / converted_distance
pace = @activity.duration / @activity.distance_in_miles

assert_equal pace, @activity.reload.calculated_pace
end
Expand Down

0 comments on commit 9237bd0

Please sign in to comment.