lib/watch_tower/server/models/file.rb in watch_tower-0.0.0.1 vs lib/watch_tower/server/models/file.rb in watch_tower-0.0.1.beta1

- old
+ new

@@ -1,5 +1,7 @@ +# -*- encoding: utf-8 -*- + module WatchTower module Server class File < ::ActiveRecord::Base # Scopes default_scope order('files.elapsed_time DESC') @@ -16,9 +18,24 @@ has_many :durations, dependent: :destroy # Return the percent of this file def percent (elapsed_time * 100) / project.files.sum_elapsed_time + end + + # Return an ActiveRelation limited to a date range + # + # @param [String] From date + # @param [String] To date + # @return [ActiveRelation] + def self.date_range(from, to) + from = Date.strptime(from, '%m/%d/%Y') + to = Date.strptime(to, '%m/%d/%Y') + + joins(:durations => :file). + where('durations.date >= ?', from). + where('durations.date <= ?', to). + select('DISTINCT files.*') end # Returns the sum of all elapsed time # # @return [Integer] \ No newline at end of file