Skip to content
Browse files

Added number of days ridden/mo. and percentage of days.

  • Loading branch information...
1 parent a20fe42 commit 079dfeb5faed85903ec689532d5cd632cbc5aeab Stuart Fletcher committed Nov 13, 2011
Showing with 31 additions and 3 deletions.
  1. +3 −3 README
  2. +21 −0 app/controllers/rides_controller.rb
  3. +7 −0 app/views/rides/index.html.erb
View
6 README
@@ -74,6 +74,9 @@ DONE:
by bicycle and give a total.
* Having ALL rides on one page is getting unwieldy too. Should organize
by month or by month&bike or...
+* When filtering rides for a certain month, list # of days in month with
+ riding vs. number of days w/o riding. To get # of days in a month:
+ Date.civil(2012, 2, -1).day, or Date.civil(year_num, month_num, -1).day
TODO:
@@ -114,6 +117,3 @@ FEATURE TODO:
etc.
* Markdown (or something) for ride descriptions (& probably other more)
(maybe rdiscount: https://github.com/rtomayko/rdiscount)
-* When filtering rides for a certain month, list # of days in month with
- riding vs. number of days w/o riding. To get # of days in a month:
- Date.civil(2012, 2, -1).day, or Date.civil(year_num, month_num, -1).day
View
21 app/controllers/rides_controller.rb
@@ -14,13 +14,34 @@ def index
Array.new(3) # [nil, nil, nil]
end
+ # TODO: setting a default year is taken care of in the scope in the
+ # model, but maybe it's better to do it here and make the model
+ # dumber? NMW, for now I have to set a default year here TOO, which
+ # isn't great...
+ if !month.nil? && month > 0 && (year.nil? || year.zero?)
+ year = Date.today.year
+ end
+
@rides = @user.rides.reals.bike(bike).month_and_year(month, year).sort_by(&:date)
@services = @user.rides.reals.service.bike(bike).month_and_year(month, year).sort_by(&:date)
@rides_only = @rides - @services
# these could perhaps be simpler?
@rides_by_bike = @rides_only.map(&:bike).uniq.map {|b| [b.name, @rides_only.select {|r| r.bike_id == b.id}.size, @rides_only.select {|r| r.bike_id == b.id}.map(&:miles).sum]}
@services_by_bike = @services.map(&:bike).uniq.map {|b| [b.name, @services.select {|r| r.bike_id == b.id}.size]}
+
+ # count # of days/month that were ridden
+ @days_ridden, @total_days =
+ if year && month
+ days = if (year == Date.today.year && month == Date.today.month)
+ Date.today.day
+ else
+ Date.civil(year, month, -1).day
+ end
+ [@rides_only.map(&:date).uniq.size, days]
+ else
+ [nil, nil]
+ end
end
def show
View
7 app/views/rides/index.html.erb
@@ -49,4 +49,11 @@ Services: <%= @services.size %>
(<%= @services_by_bike.map {|s| "#{s.first}: #{s.last}"}.join(", ") %>)
</p>
+<%- if @days_ridden && @total_days -%>
+<p>
+<%= @days_ridden %> days ridden out of <%= @total_days %>
+(<%= nice_number(1.0 * @days_ridden / @total_days * 100) %>% of days)
+</p>
+<%- end -%>
+
<p><%= link_to "New Ride", new_ride_path %></p>

0 comments on commit 079dfeb

Please sign in to comment.
Something went wrong with that request. Please try again.