Skip to content
Permalink
Browse files
Refactor how weekly mileage is calculated.
* Add distance_in_miles column to activities table
  • Loading branch information
Steve Polito committed Aug 26, 2020
1 parent c02605f commit 9237bd086fc19b1c6964cc310b56e8949b71ba33
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
@@ -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
@@ -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

@@ -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?
@@ -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
@@ -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"
@@ -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

@@ -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| %>
@@ -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:
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 9237bd0

Please sign in to comment.