From: Jean-Philippe Lang Date: Mon, 20 Oct 2014 19:16:09 +0000 (+0000) Subject: Code cleanup, removed raw SQL queries. X-Git-Tag: 3.0.0~496 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cac6ebb6c5c09c1000598cd744ae8448b5db4a41;p=redmine.git Code cleanup, removed raw SQL queries. git-svn-id: http://svn.redmine.org/redmine/trunk@13461 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/issue.rb b/app/models/issue.rb index dd6361e9e..5f8a0af2c 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1182,58 +1182,61 @@ class Issue < ActiveRecord::Base end end - # Extracted from the ReportsController. def self.by_tracker(project) - count_and_group_by(:project => project, - :field => 'tracker_id', - :joins => Tracker.table_name) + count_and_group_by(:project => project, :association => :tracker) end def self.by_version(project) - count_and_group_by(:project => project, - :field => 'fixed_version_id', - :joins => Version.table_name) + count_and_group_by(:project => project, :association => :fixed_version) end def self.by_priority(project) - count_and_group_by(:project => project, - :field => 'priority_id', - :joins => IssuePriority.table_name) + count_and_group_by(:project => project, :association => :priority) end def self.by_category(project) - count_and_group_by(:project => project, - :field => 'category_id', - :joins => IssueCategory.table_name) + count_and_group_by(:project => project, :association => :category) end def self.by_assigned_to(project) - count_and_group_by(:project => project, - :field => 'assigned_to_id', - :joins => User.table_name) + count_and_group_by(:project => project, :association => :assigned_to) end def self.by_author(project) - count_and_group_by(:project => project, - :field => 'author_id', - :joins => User.table_name) + count_and_group_by(:project => project, :association => :author) end def self.by_subproject(project) - ActiveRecord::Base.connection.select_all("select s.id as status_id, - s.is_closed as closed, - #{Issue.table_name}.project_id as project_id, - count(#{Issue.table_name}.id) as total - from - #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s - where - #{Issue.table_name}.status_id=s.id - and #{Issue.table_name}.project_id = #{Project.table_name}.id - and #{visible_condition(User.current, :project => project, :with_subprojects => true)} - and #{Issue.table_name}.project_id <> #{project.id} - group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any? - end - # End ReportsController extraction + r = count_and_group_by(:project => project, :with_subprojects => true, :association => :project) + r.reject {|r| r["project_id"] == project.id.to_s} + end + + # Query generator for selecting groups of issue counts for a project + # based on specific criteria + # + # Options + # * project - Project to search in. + # * with_subprojects - Includes subprojects issues if set to true. + # * association - Symbol. Association for grouping. + def self.count_and_group_by(options) + assoc = reflect_on_association(options[:association]) + select_field = assoc.foreign_key + + Issue. + visible(User.current, :project => options[:project], :with_subprojects => options[:with_subprojects]). + joins(:status). + group(:status_id, :is_closed, select_field). + count. + map do |columns, total| + status_id, is_closed, field_value = columns + { + "status_id" => status_id.to_s, + "closed" => is_closed.to_s, + select_field => field_value.to_s, + "total" => total.to_s + } + end + end # Returns a scope of projects that user can assign the issue to def allowed_target_projects(user=User.current) @@ -1565,32 +1568,4 @@ class Issue < ActiveRecord::Base @assigned_to_was = nil @previous_assigned_to_id = nil end - - # Query generator for selecting groups of issue counts for a project - # based on specific criteria - # - # Options - # * project - Project to search in. - # * field - String. Issue field to key off of in the grouping. - # * joins - String. The table name to join against. - def self.count_and_group_by(options) - project = options.delete(:project) - select_field = options.delete(:field) - joins = options.delete(:joins) - - where = "#{Issue.table_name}.#{select_field}=j.id" - - ActiveRecord::Base.connection.select_all("select s.id as status_id, - s.is_closed as closed, - j.id as #{select_field}, - count(#{Issue.table_name}.id) as total - from - #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j - where - #{Issue.table_name}.status_id=s.id - and #{where} - and #{Issue.table_name}.project_id=#{Project.table_name}.id - and #{visible_condition(User.current, :project => project)} - group by s.id, s.is_closed, j.id") - end end