Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle case when there are no records for time review

commit 45e71e1766b0a4f304168b23ebfc4ebddee99f0b 1 parent 5174589
@sachac authored
View
8 app/controllers/time_controller.rb
@@ -47,13 +47,7 @@ def graph
params[:end] ||= Time.zone.now.strftime('%Y-%m-%d')
prepare_filters [:date_range]
@range = Time.zone.parse(params[:start]).to_date..Time.zone.parse(params[:end]).to_date
- entries = current_account.records.activities.where('end_timestamp >= ? AND timestamp < ?', @range.begin, @range.end).order('timestamp').includes(:record_category)
- # Adjust the last entry and the first entry as needed
- entries.first.timestamp = [@range.begin, entries.first.timestamp].max
- entries.first.duration = entries.first.end_timestamp - entries.first.timestamp
- entries.last.end_timestamp = [@range.end, entries.last.timestamp || Time.zone.now, Time.zone.now].min
- entries.last.duration = entries.last.end_timestamp - entries.last.timestamp
-
+ entries = Record.get_entries_for_time_range(current_account, @range)
@records = Record.prepare_graph(@range, entries)
unsorted = RecordCategory.summarize(:key => :date, :range => @range, :records => entries, :zoom => :daily, :user => current_account, :tree => :individual)[:rows]
@categories = current_account.record_categories.index_by(&:id)
View
20 app/helpers/application_helper.rb
@@ -172,14 +172,18 @@ def active_menu(crumb)
def duration(seconds, total = 0, display_type = nil)
display_type ||= (params && params[:display_type]) ? params[:display_type] : 'time'
- if seconds and seconds > 0
- case display_type
- when 'time'
- "%d" % (seconds.to_f / 1.hour) + (":%02d" % ((seconds.to_f % 1.hour) / 1.minute))
- when 'decimal'
- "%.1f" % ((seconds.to_f / 1.hour).to_f.round(1))
- when 'percentage'
- "%0.1f%%" % (100.0 * seconds / total)
+ if seconds
+ if seconds > 0
+ case display_type
+ when 'time'
+ "%d" % (seconds.to_f / 1.hour) + (":%02d" % ((seconds.to_f % 1.hour) / 1.minute))
+ when 'decimal'
+ "%.1f" % ((seconds.to_f / 1.hour).to_f.round(1))
+ when 'percentage'
+ "%0.1f%%" % (100.0 * seconds / total)
+ end
+ else
+ "0"
end
end
end
View
12 app/models/record.rb
@@ -222,6 +222,18 @@ def self.refresh_from_tap_log(user, file)
list
end
+ def self.get_entries_for_time_range(user, range)
+ entries = user.records.activities.where('end_timestamp >= ? AND timestamp < ?', range.begin, range.end).order('timestamp').includes(:record_category)
+ # Adjust the last entry and the first entry as needed
+ if entries.count > 0 then
+ entries.first.timestamp = [range.begin, entries.first.timestamp].max
+ entries.first.duration = entries.first.end_timestamp - entries.first.timestamp
+ entries.last.end_timestamp = [range.end, entries.last.timestamp || Time.zone.now, Time.zone.now].min
+ entries.last.duration = entries.last.end_timestamp - entries.last.timestamp
+ end
+ entries
+ end
+
# Return an array of [date => [[start time, end time, category], [start time, end time, category]]]
# Pre-fill colors
def self.prepare_graph(range, records)
View
1  app/models/record_category.rb
@@ -25,6 +25,7 @@ def add_data
# other - each entry counts only towards its own category
def self.roll_up_records(options = {})
summary = Hash.new { |h,k| h[k] = Hash.new { |h2,k2| h2[k2] = Hash.new { |h3,k3| h3[k3] = 0 } } }
+ summary[:total][:total][:total] = 0
records = options[:records]
user = options[:user]
max = Time.now
View
19 spec/controllers/time_controller_spec.rb
@@ -8,6 +8,25 @@
after do
Timecop.return
end
+ context "when logged in, but without data" do
+ before do
+ @user = create(:user, :confirmed)
+ sign_in @user
+ end
+ describe 'GET review' do
+ it "does not result in errors" do
+ get :review, start: Time.zone.now - 1.week, end: Time.zone.now - 1.day
+ assigns(:zoom).should == :daily
+ assigns(:summary)[:total][:total][:total].should == 0
+ end
+ end
+ describe 'GET graph' do
+ it "displays records" do
+ get :graph, start: Time.zone.now - 1.week, end: Time.zone.now.midnight
+ assigns(:total).should == "0"
+ end
+ end
+ end
context "when logged in" do
before do
@user = create(:user, :confirmed)
Please sign in to comment.
Something went wrong with that request. Please try again.