Skip to content

Commit

Permalink
Show daily time total on event bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
boone committed Oct 23, 2011
1 parent 6ba41d8 commit b15ddee
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* Move to Rails 3.1

* Show cumulative time for the current day in the top area, on the right
* Github-style page sliding when changing pages?
* what about personal projects or projects for small clients with no sub projects? how to make that clean and easy?
* Take secret token out of Git?
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ def handle_unverified_request
def current_events
@current_events = Event.current
end
helper_method :current_events

def time_today
@time_today = Event.time_today
end

helper_method :current_events, :time_today
end
7 changes: 7 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Event < ActiveRecord::Base
default_scope order('start DESC')
scope :current, where('end IS NULL').order('start DESC')
scope :completed, where('end IS NOT NULL').order('end DESC')
scope :today, where('start > ? AND start < ?', Time.now.beginning_of_day, Time.now.end_of_day)

validates_presence_of :project
validates_presence_of :start
Expand Down Expand Up @@ -45,4 +46,10 @@ def resume!
def expanded_title
"#{self.project.client.name}: #{self.project.title}"
end

def self.time_today
sum = 0.0
self.today.each { |e| sum += (e.end.nil? ? Time.now : e.end) - e.start }
sum
end
end
16 changes: 16 additions & 0 deletions app/views/events/_current.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
<strong>Started:</strong> <%= event.start.strftime('%H:%M') %><br />
<strong>Comment:</strong> <%= truncate(event.comment, :length => 30, :separator => ' ') %>
</div>
<div class="daily_total">
<div class="start_stop">
Daily Total
</div>
<div class="time">
<%= number_with_precision(time_today / 3600, :precision => 2) %>
</div>
</div>
<% end %>
<% else %>
<div class="running_time">
Expand All @@ -26,5 +34,13 @@
<div class="info">
<strong>Clock Stopped</strong>
</div>
<div class="daily_total">
<div class="start_stop">
Daily Total
</div>
<div class="time">
<%= number_with_precision(time_today / 3600, :precision => 2) %>
</div>
</div>
<% end %>

6 changes: 4 additions & 2 deletions public/stylesheets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ a:hover { text-decoration: underline; background-color: transparent; }
#content { padding: 15px auto; }
#status { height: 65px; font-size: 14px; }
#flash { color: green; margin-bottom: 10px; }
.running_time { width: 150px; text-align: center; padding: 0 10px 0 10px; margin: 0; float: left; }
.running_time, .daily_total { width: 150px; text-align: center; padding: 0 10px 0 10px; margin: 0; float: left; }
.daily_total { color: #555; }
.start_stop { padding: 0 0 5px 0; margin: 0; }
.daily_total .start_stop { font-style: italic; }
.time { font-size: 40px; line-height: 42px; }
.info { float: left; overflow: hidden; width: 520px; padding: 5px 10px 5px 0; white-space: nowrap; }
.info { float: left; overflow: hidden; width: 350px; padding: 5px 10px 5px 0; white-space: nowrap; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
#footer { font-size: 10px; color: #ddd; padding: 0 10px;}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,30 @@ class EventTest < ActiveSupport::TestCase
assert !event.valid?
assert event.errors[:base]
end

should 'compute zero for daily time when there are no events today' do
Factory.create(:event, :start => 1.day.ago.beginning_of_day, :end => 1.day.ago.beginning_of_day + 1.hour) # yesterday
assert_equal 0, Event.time_today
end

should 'compute correct daily time when there is no current event' do
Factory.create(:event, :start => 1.day.ago.beginning_of_day, :end => 1.day.ago.beginning_of_day + 1.hour) # yesterday
Factory.create(:event, :start => Time.now.beginning_of_day, :end => Time.now.beginning_of_day + 1.hour) # 1 hour
Factory.create(:event, :start => Time.now.beginning_of_day + 2.hours, :end => Time.now.beginning_of_day + 3.hours) # 1.hour
assert_equal 2, Event.today.count
assert_equal 2.hours, Event.time_today
end

should 'compute correct daily time when there is a current event' do
Factory.create(:event, :start => 1.day.ago.beginning_of_day, :end => 1.day.ago.beginning_of_day + 1.hour) # yesterday
Factory.create(:event, :start => Time.now.beginning_of_day, :end => Time.now.beginning_of_day + 1.hour) # 1 hour
Factory.create(:event, :start => Time.now.beginning_of_day, :end => nil)

assert_equal 2, Event.today.count

# Time.now calls at different stages will not be exact, so assert the results are the same to thousandths of an hour
expected_time = ((1.hour + (Time.now - Time.now.beginning_of_day)) / 3.6).to_i
actual_time = (Event.time_today / 3.6).to_i
assert_equal expected_time, actual_time
end
end

0 comments on commit b15ddee

Please sign in to comment.