From 915bbcfaff8a5db56bc60b90fb690b7f4be3b555 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 28 Nov 2009 10:08:29 +0000 Subject: [PATCH] Move issues, journals, versions queries from IssuesController to Query model. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3103 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/issues_controller.rb | 64 +++++++++++----------------- app/models/query.rb | 49 +++++++++++++++++++++ 2 files changed, 73 insertions(+), 40 deletions(-) diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 2578b784b..63b174f89 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -61,26 +61,17 @@ class IssuesController < ApplicationController format.csv { limit = Setting.issues_export_limit.to_i } format.pdf { limit = Setting.issues_export_limit.to_i } end - @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement) + + @issue_count = @query.issue_count @issue_pages = Paginator.new self, @issue_count, limit, params['page'] - @issues = Issue.find :all, :order => [@query.group_by_sort_order, sort_clause].compact.join(','), - :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ], - :conditions => @query.statement, - :limit => limit, - :offset => @issue_pages.current.offset + @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], + :order => sort_clause, + :offset => @issue_pages.current.offset, + :limit => limit) + @issue_count_by_group = @query.issue_count_by_group + respond_to do |format| - format.html { - if @query.grouped? - # Retrieve the issue count by group - @issue_count_by_group = begin - Issue.count(:group => @query.group_by_statement, :include => [:status, :project], :conditions => @query.statement) - # Rails will raise an (unexpected) error if there's only a nil group value - rescue ActiveRecord::RecordNotFound - {nil => @issue_count} - end - end - render :template => 'issues/index.rhtml', :layout => !request.xhr? - } + format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } @@ -99,10 +90,8 @@ class IssuesController < ApplicationController sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h})) if @query.valid? - @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ], - :conditions => @query.statement, - :limit => 25, - :order => "#{Journal.table_name}.created_on DESC" + @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", + :limit => 25) end @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) render :layout => false, :content_type => 'application/atom+xml' @@ -360,20 +349,17 @@ class IssuesController < ApplicationController if @query.valid? events = [] # Issues that have start and due dates - events += Issue.find(:all, - :order => "start_date, due_date", - :include => [:tracker, :status, :assigned_to, :priority, :project], - :conditions => ["(#{@query.statement}) AND (((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] - ) + events += @query.issues(:include => [:tracker, :assigned_to, :priority], + :order => "start_date, due_date", + :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] + ) # Issues that don't have a due date but that are assigned to a version with a date - events += Issue.find(:all, - :order => "start_date, effective_date", - :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version], - :conditions => ["(#{@query.statement}) AND (((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] - ) + events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], + :order => "start_date, effective_date", + :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] + ) # Versions - events += Version.find(:all, :include => :project, - :conditions => ["(#{@query.project_statement}) AND effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) + events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) @gantt.events = events end @@ -401,12 +387,10 @@ class IssuesController < ApplicationController retrieve_query if @query.valid? events = [] - events += Issue.find(:all, - :include => [:tracker, :status, :assigned_to, :priority, :project], - :conditions => ["(#{@query.statement}) AND ((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt] - ) - events += Version.find(:all, :include => :project, - :conditions => ["(#{@query.project_statement}) AND effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt]) + events += @query.issues(:include => [:tracker, :assigned_to, :priority], + :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt] + ) + events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt]) @calendar.events = events end diff --git a/app/models/query.rb b/app/models/query.rb index 816c5c5a0..d15f7c3c7 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -389,6 +389,55 @@ class Query < ActiveRecord::Base (filters_clauses << project_statement).join(' AND ') end + # Returns the issue count + def issue_count + Issue.count(:include => [:status, :project], :conditions => statement) + end + + # Returns the issue count by group or nil if query is not grouped + def issue_count_by_group + if grouped? + begin + # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value + Issue.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) + rescue ActiveRecord::RecordNotFound + {nil => issue_count} + end + else + nil + end + end + + # Returns the issues + # Valid options are :order, :offset, :limit, :include, :conditions + def issues(options={}) + order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') + order_option = nil if order_option.blank? + + Issue.find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, + :conditions => Query.merge_conditions(statement, options[:conditions]), + :order => order_option, + :limit => options[:limit], + :offset => options[:offset] + end + + # Returns the journals + # Valid options are :order, :offset, :limit + def journals(options={}) + Journal.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], + :conditions => statement, + :order => options[:order], + :limit => options[:limit], + :offset => options[:offset] + end + + # Returns the versions + # Valid options are :conditions + def versions(options={}) + Version.find :all, :include => :project, + :conditions => Query.merge_conditions(project_statement, options[:conditions]) + end + private # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ -- 2.39.5