Commit 93d68f81 authored by jplang's avatar jplang
Browse files

Adds STI to Query model. Issue queries are now IssueQuery instances.

git-svn-id: https://svn.redmine.org/redmine/trunk@10964 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent fa7b5827
......@@ -32,9 +32,9 @@ class QueriesController < ApplicationController
@limit = per_page_option
end
@query_count = Query.visible.count
@query_count = IssueQuery.visible.count
@query_pages = Paginator.new self, @query_count, @limit, params['page']
@queries = Query.visible.all(:limit => @limit, :offset => @offset, :order => "#{Query.table_name}.name")
@queries = IssueQuery.visible.all(:limit => @limit, :offset => @offset, :order => "#{Query.table_name}.name")
respond_to do |format|
format.html { render :nothing => true }
......@@ -43,7 +43,7 @@ class QueriesController < ApplicationController
end
def new
@query = Query.new
@query = IssueQuery.new
@query.user = User.current
@query.project = @project
@query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
......@@ -51,7 +51,7 @@ class QueriesController < ApplicationController
end
def create
@query = Query.new(params[:query])
@query = IssueQuery.new(params[:query])
@query.user = User.current
@query.project = params[:query_is_for_all] ? nil : @project
@query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
......@@ -91,7 +91,7 @@ class QueriesController < ApplicationController
private
def find_query
@query = Query.find(params[:id])
@query = IssueQuery.find(params[:id])
@project = @query.project
render_403 unless @query.editable_by?(User.current)
rescue ActiveRecord::RecordNotFound
......
......@@ -184,7 +184,7 @@ module IssuesHelper
def sidebar_queries
unless @sidebar_queries
@sidebar_queries = Query.visible.all(
@sidebar_queries = IssueQuery.visible.all(
:order => "#{Query.table_name}.name ASC",
# Project specific queries and global queries
:conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
......
......@@ -122,21 +122,21 @@ module QueriesHelper
if !params[:query_id].blank?
cond = "project_id IS NULL"
cond << " OR project_id = #{@project.id}" if @project
@query = Query.find(params[:query_id], :conditions => cond)
@query = IssueQuery.find(params[:query_id], :conditions => cond)
raise ::Unauthorized unless @query.visible?
@query.project = @project
session[:query] = {:id => @query.id, :project_id => @query.project_id}
sort_clear
elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
# Give it a name, required to be valid
@query = Query.new(:name => "_")
@query = IssueQuery.new(:name => "_")
@query.project = @project
build_query_from_params
session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
else
# retrieve from session
@query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
@query ||= Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
@query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id]
@query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
@query.project = @project
end
end
......@@ -144,10 +144,10 @@ module QueriesHelper
def retrieve_query_from_session
if session[:query]
if session[:query][:id]
@query = Query.find_by_id(session[:query][:id])
@query = IssueQuery.find_by_id(session[:query][:id])
return unless @query
else
@query = Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
@query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
end
if session[:query].has_key?(:project_id)
@query.project_id = session[:query][:project_id]
......
# Redmine - project management software
# Copyright (C) 2006-2012 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class IssueQuery < Query
self.available_columns = [
QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
QueryColumn.new(:relations, :caption => :label_related_issues),
QueryColumn.new(:description, :inline => false)
]
scope :visible, lambda {|*args|
user = args.shift || User.current
base = Project.allowed_to_condition(user, :view_issues, *args)
user_id = user.logged? ? user.id : 0
includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
}
def initialize(attributes=nil, *args)
super attributes
self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
end
# Returns true if the query is visible to +user+ or the current user.
def visible?(user=User.current)
(project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
end
def available_filters
return @available_filters if @available_filters
@available_filters = {
"status_id" => {
:type => :list_status, :order => 0,
:values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] }
},
"tracker_id" => {
:type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] }
},
"priority_id" => {
:type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
},
"subject" => { :type => :text, :order => 8 },
"created_on" => { :type => :date_past, :order => 9 },
"updated_on" => { :type => :date_past, :order => 10 },
"start_date" => { :type => :date, :order => 11 },
"due_date" => { :type => :date, :order => 12 },
"estimated_hours" => { :type => :float, :order => 13 },
"done_ratio" => { :type => :integer, :order => 14 }
}
IssueRelation::TYPES.each do |relation_type, options|
@available_filters[relation_type] = {
:type => :relation, :order => @available_filters.size + 100,
:label => options[:name]
}
end
principals = []
if project
principals += project.principals.sort
unless project.leaf?
subprojects = project.descendants.visible.all
if subprojects.any?
@available_filters["subproject_id"] = {
:type => :list_subprojects, :order => 13,
:values => subprojects.collect{|s| [s.name, s.id.to_s] }
}
principals += Principal.member_of(subprojects)
end
end
else
if all_projects.any?
# members of visible projects
principals += Principal.member_of(all_projects)
# project filter
project_values = []
if User.current.logged? && User.current.memberships.any?
project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
end
project_values += all_projects_values
@available_filters["project_id"] = {
:type => :list, :order => 1, :values => project_values
} unless project_values.empty?
end
end
principals.uniq!
principals.sort!
users = principals.select {|p| p.is_a?(User)}
assigned_to_values = []
assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
assigned_to_values += (Setting.issue_group_assignment? ?
principals : users).collect{|s| [s.name, s.id.to_s] }
@available_filters["assigned_to_id"] = {
:type => :list_optional, :order => 4, :values => assigned_to_values
} unless assigned_to_values.empty?
author_values = []
author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
author_values += users.collect{|s| [s.name, s.id.to_s] }
@available_filters["author_id"] = {
:type => :list, :order => 5, :values => author_values
} unless author_values.empty?
group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
@available_filters["member_of_group"] = {
:type => :list_optional, :order => 6, :values => group_values
} unless group_values.empty?
role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
@available_filters["assigned_to_role"] = {
:type => :list_optional, :order => 7, :values => role_values
} unless role_values.empty?
if User.current.logged?
@available_filters["watcher_id"] = {
:type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]]
}
end
if project
# project specific filters
categories = project.issue_categories.all
unless categories.empty?
@available_filters["category_id"] = {
:type => :list_optional, :order => 6,
:values => categories.collect{|s| [s.name, s.id.to_s] }
}
end
versions = project.shared_versions.all
unless versions.empty?
@available_filters["fixed_version_id"] = {
:type => :list_optional, :order => 7,
:values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
}
end
add_custom_fields_filters(project.all_issue_custom_fields)
else
# global filters for cross project issue list
system_shared_versions = Version.visible.find_all_by_sharing('system')
unless system_shared_versions.empty?
@available_filters["fixed_version_id"] = {
:type => :list_optional, :order => 7,
:values => system_shared_versions.sort.collect{|s|
["#{s.project.name} - #{s.name}", s.id.to_s]
}
}
end
add_custom_fields_filters(IssueCustomField.where(:is_filter => true, :is_for_all => true).all)
end
add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
@available_filters["is_private"] = {
:type => :list, :order => 16,
:values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
}
end
Tracker.disabled_core_fields(trackers).each {|field|
@available_filters.delete field
}
@available_filters.each do |field, options|
options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, ''))
end
@available_filters
end
def available_columns
return @available_columns if @available_columns
@available_columns = self.class.available_columns.dup
@available_columns += (project ?
project.all_issue_custom_fields :
IssueCustomField.all
).collect {|cf| QueryCustomFieldColumn.new(cf) }
if User.current.allowed_to?(:view_time_entries, project, :global => true)
index = nil
@available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
index = (index ? index + 1 : -1)
# insert the column after estimated_hours or at the end
@available_columns.insert index, QueryColumn.new(:spent_hours,
:sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)",
:default_order => 'desc',
:caption => :label_spent_time
)
end
if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
@available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
end
disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
@available_columns.reject! {|column|
disabled_fields.include?(column.name.to_s)
}
@available_columns
end
# Returns the issue count
def issue_count
Issue.visible.count(:include => [:status, :project], :conditions => statement)
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end
# Returns the issue count by group or nil if query is not grouped
def issue_count_by_group
r = nil
if grouped?
begin
# Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement)
rescue ActiveRecord::RecordNotFound
r = {nil => issue_count}
end
c = group_by_column
if c.is_a?(QueryCustomFieldColumn)
r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
end
end
r
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
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?
issues = Issue.visible.where(options[:conditions]).all(
:include => ([:status, :project] + (options[:include] || [])).uniq,
:conditions => statement,
:order => order_option,
:joins => joins_for_order_statement(order_option),
:limit => options[:limit],
:offset => options[:offset]
)
if has_column?(:spent_hours)
Issue.load_visible_spent_hours(issues)
end
if has_column?(:relations)
Issue.load_visible_relations(issues)
end
issues
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end
# Returns the issues ids
def issue_ids(options={})
order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
order_option = nil if order_option.blank?
Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
:conditions => statement,
:order => order_option,
:joins => joins_for_order_statement(order_option),
:limit => options[:limit],
:offset => options[:offset]).find_ids
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end
# Returns the journals
# Valid options are :order, :offset, :limit
def journals(options={})
Journal.visible.all(
:include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}],
:conditions => statement,
:order => options[:order],
:limit => options[:limit],
:offset => options[:offset]
)
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end
# Returns the versions
# Valid options are :conditions
def versions(options={})
Version.visible.where(options[:conditions]).all(
:include => :project,
:conditions => project_statement
)
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end
end
......@@ -42,7 +42,7 @@ class Project < ActiveRecord::Base
has_many :issue_changes, :through => :issues, :source => :journals
has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
has_many :time_entries, :dependent => :delete_all
has_many :queries, :dependent => :delete_all
has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
has_many :documents, :dependent => :destroy
has_many :news, :dependent => :destroy, :include => :author
has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
......@@ -869,7 +869,7 @@ class Project < ActiveRecord::Base
# Copies queries from +project+
def copy_queries(project)
project.queries.each do |query|
new_query = ::Query.new
new_query = IssueQuery.new
new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
new_query.sort_criteria = query.sort_criteria if query.sort_criteria
new_query.project = self
......
......@@ -145,38 +145,10 @@ class Query < ActiveRecord::Base
}
class_attribute :available_columns
self.available_columns = [
QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
QueryColumn.new(:relations, :caption => :label_related_issues),
QueryColumn.new(:description, :inline => false)
]
scope :visible, lambda {|*args|
user = args.shift || User.current
base = Project.allowed_to_condition(user, :view_issues, *args)
user_id = user.logged? ? user.id : 0
includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
}
self.available_columns = []
def initialize(attributes=nil, *args)
super attributes
self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
@is_for_all = project.nil?
end
......@@ -211,11 +183,6 @@ class Query < ActiveRecord::Base
errors.add(:base, m)
end
# Returns true if the query is visible to +user+ or the current user.
def visible?(user=User.current)
(project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
end
def editable_by?(user)
return false unless user
# Admin can edit them all and regular users can edit their private queries
......@@ -233,143 +200,6 @@ class Query < ActiveRecord::Base
operators.inject({}) {|h, operator| h[operator.first] = l(operator.last); h}
end
def available_filters
return @available_filters if @available_filters
@available_filters = {
"status_id" => {
:type => :list_status, :order => 0,
:values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] }
},
"tracker_id" => {
:type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] }
},
"priority_id" => {
:type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
},
"subject" => { :type => :text, :order => 8 },
"created_on" => { :type => :date_past, :order => 9 },
"updated_on" => { :type => :date_past, :order => 10 },
"start_date" => { :type => :date, :order => 11 },
"due_date" => { :type => :date, :order => 12 },
"estimated_hours" => { :type => :float, :order => 13 },
"done_ratio" => { :type => :integer, :order => 14 }
}
IssueRelation::TYPES.each do |relation_type, options|
@available_filters[relation_type] = {
:type => :relation, :order => @available_filters.size + 100,
:label => options[:name]
}
end
principals = []
if project
principals += project.principals.sort
unless project.leaf?
subprojects = project.descendants.visible.all
if subprojects.any?
@available_filters["subproject_id"] = {
:type => :list_subprojects, :order => 13,
:values => subprojects.collect{|s| [s.name, s.id.to_s] }
}
principals += Principal.member_of(subprojects)
end
end
else
if all_projects.any?
# members of visible projects
principals += Principal.member_of(all_projects)
# project filter
project_values = []
if User.current.logged? && User.current.memberships.any?
project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
end
project_values += all_projects_values
@available_filters["project_id"] = {
:type => :list, :order => 1, :values => project_values
} unless project_values.empty?
end
end
principals.uniq!
principals.sort!
users = principals.select {|p| p.is_a?(User)}
assigned_to_values = []
assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
assigned_to_values += (Setting.issue_group_assignment? ?
principals : users).collect{|s| [s.name, s.id.to_s] }
@available_filters["assigned_to_id"] = {
:type => :list_optional, :order => 4, :values => assigned_to_values
} unless assigned_to_values.empty?
author_values = []
author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
author_values += users.collect{|s| [s.name, s.id.to_s] }
@available_filters["author_id"] = {
:type => :list, :order => 5, :values => author_values
} unless author_values.empty?
group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
@available_filters["member_of_group"] = {
:type => :list_optional, :order => 6, :values => group_values
} unless group_values.empty?
role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
@available_filters["assigned_to_role"] = {
:type => :list_optional, :order => 7, :values => role_values
} unless role_values.empty?
if User.current.logged?
@available_filters["watcher_id"] = {
:type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]]
}
end
if project
# project specific filters
categories = project.issue_categories.all
unless categories.empty?
@available_filters["category_id"] = {
:type => :list_optional, :order => 6,
:values => categories.collect{|s| [s.name, s.id.to_s] }
}
end
versions = project.shared_versions.all