]> source.dussan.org Git - redmine.git/commitdiff
Adds named scopes for time entries index.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 4 Dec 2011 22:49:46 +0000 (22:49 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 4 Dec 2011 22:49:46 +0000 (22:49 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8084 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/timelog_controller.rb
app/models/time_entry.rb

index 063df30ccd89d6512df555c21c3d2bcac0d406ad..719e0b8647f52dc5faff98366821bd1f92a20663 100644 (file)
@@ -41,55 +41,54 @@ class TimelogController < ApplicationController
                 'issue' => 'issue_id',
                 'hours' => 'hours'
 
-    cond = ARCondition.new
+    retrieve_date_range
+
+    scope = TimeEntry.visible.spent_between(@from, @to)
     if @issue
-      cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
+      scope = scope.on_issue(@issue)
     elsif @project
-      cond << @project.project_condition(Setting.display_subprojects_issues?)
+      scope = scope.on_project(@project, Setting.display_subprojects_issues?)
     end
 
-    retrieve_date_range
-    cond << ['spent_on BETWEEN ? AND ?', @from, @to]
-
     respond_to do |format|
       format.html {
         # Paginate results
-        @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
+        @entry_count = scope.count
         @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
-        @entries = TimeEntry.visible.find(:all,
-                                  :include => [:project, :activity, :user, {:issue => :tracker}],
-                                  :conditions => cond.conditions,
-                                  :order => sort_clause,
-                                  :limit  =>  @entry_pages.items_per_page,
-                                  :offset =>  @entry_pages.current.offset)
-        @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
+        @entries = scope.all(
+          :include => [:project, :activity, :user, {:issue => :tracker}],
+          :order => sort_clause,
+          :limit  =>  @entry_pages.items_per_page,
+          :offset =>  @entry_pages.current.offset
+        )
+        @total_hours = scope.sum(:hours).to_f
 
         render :layout => !request.xhr?
       }
       format.api  {
-        @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
+        @entry_count = scope.count
         @offset, @limit = api_offset_and_limit
-        @entries = TimeEntry.visible.find(:all,
-                                  :include => [:project, :activity, :user, {:issue => :tracker}],
-                                  :conditions => cond.conditions,
-                                  :order => sort_clause,
-                                  :limit  => @limit,
-                                  :offset => @offset)
+        @entries = scope.all(
+          :include => [:project, :activity, :user, {:issue => :tracker}],
+          :order => sort_clause,
+          :limit  => @limit,
+          :offset => @offset
+        )
       }
       format.atom {
-        entries = TimeEntry.visible.find(:all,
-                                 :include => [:project, :activity, :user, {:issue => :tracker}],
-                                 :conditions => cond.conditions,
-                                 :order => "#{TimeEntry.table_name}.created_on DESC",
-                                 :limit => Setting.feeds_limit.to_i)
+        entries = scope.all(
+          :include => [:project, :activity, :user, {:issue => :tracker}],
+          :order => "#{TimeEntry.table_name}.created_on DESC",
+          :limit => Setting.feeds_limit.to_i
+        )
         render_feed(entries, :title => l(:label_spent_time))
       }
       format.csv {
         # Export all entries
-        @entries = TimeEntry.visible.find(:all,
-                                  :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
-                                  :conditions => cond.conditions,
-                                  :order => sort_clause)
+        @entries = scope.all(
+          :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
+          :order => sort_clause
+        )
         send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
       }
     end
index 1afe54829bb03f30bd27f5d63651f9175852b95b..f4716d0ac4dd211774a7d0267d943ac562db7eb1 100644 (file)
@@ -45,7 +45,18 @@ class TimeEntry < ActiveRecord::Base
     :include => :project,
     :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
   }}
-
+  named_scope :on_issue, lambda {|issue| {
+    :include => :issue,
+    :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
+  }}
+  named_scope :on_project, lambda {|project, include_subprojects| {
+    :include => :project,
+    :conditions => project.project_condition(include_subprojects)
+  }}
+  named_scope :spent_between, lambda {|from, to| {
+    :conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]
+  }}
+  
   def after_initialize
     if new_record? && self.activity.nil?
       if default_activity = TimeEntryActivity.default