--- /dev/null
+# 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
}
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
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
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
- 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
-
# Returns a representation of the available filters for JSON serialization
def available_filters_as_json
json = {}
label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
end
- def available_columns
- return @available_columns if @available_columns
- @available_columns = ::Query.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
-
def self.add_available_column(column)
self.available_columns << (column) if column.is_a?(QueryColumn)
end
filters_clauses.any? ? filters_clauses.join(' AND ') : nil
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
-
def sql_for_watcher_id_field(field, operator, value)
db_table = Watcher.table_name
"#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
:custom_fields_trackers
def test_custom_fields_for_all_projects_should_be_available_in_global_queries
- query = Query.new(:project => nil, :name => '_')
+ query = IssueQuery.new(:project => nil, :name => '_')
assert query.available_filters.has_key?('cf_1')
assert !query.available_filters.has_key?('cf_3')
end
def test_system_shared_versions_should_be_available_in_global_queries
Version.find(2).update_attribute :sharing, 'system'
- query = Query.new(:project => nil, :name => '_')
+ query = IssueQuery.new(:project => nil, :name => '_')
assert query.available_filters.has_key?('fixed_version_id')
assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'}
end
def test_project_filter_in_global_queries
- query = Query.new(:project => nil, :name => '_')
+ query = IssueQuery.new(:project => nil, :name => '_')
project_filter = query.available_filters["project_id"]
assert_not_nil project_filter
project_ids = project_filter[:values].map{|p| p[1]}
def test_query_should_allow_shared_versions_for_a_project_query
subproject_version = Version.find(4)
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s])
assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')")
end
def test_query_with_multiple_custom_fields
- query = Query.find(1)
+ query = IssueQuery.find(1)
assert query.valid?
assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')")
issues = find_issues_with_query(query)
end
def test_operator_none
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('fixed_version_id', '!*', [''])
query.add_filter('cf_1', '!*', [''])
assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL")
end
def test_operator_none_for_integer
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('estimated_hours', '!*', [''])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_none_for_date
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('start_date', '!*', [''])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_none_for_string_custom_field
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('cf_2', '!*', [''])
assert query.has_filter?('cf_2')
issues = find_issues_with_query(query)
end
def test_operator_all
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('fixed_version_id', '*', [''])
query.add_filter('cf_1', '*', [''])
assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL")
end
def test_operator_all_for_date
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('start_date', '*', [''])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_all_for_string_custom_field
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('cf_2', '*', [''])
assert query.has_filter?('cf_2')
issues = find_issues_with_query(query)
end
def test_numeric_filter_should_not_accept_non_numeric_values
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('estimated_hours', '=', ['a'])
assert query.has_filter?('estimated_hours')
def test_operator_is_on_float
Issue.update_all("estimated_hours = 171.2", "id=2")
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('estimated_hours', '=', ['171.20'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['12'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['-12'])
assert query.valid?
issues = find_issues_with_query(query)
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['12.7'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['-12.7'])
assert query.valid?
issues = find_issues_with_query(query)
CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['value1'])
issues = find_issues_with_query(query)
assert_equal [1, 3], issues.map(&:id).sort
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['value2'])
issues = find_issues_with_query(query)
assert_equal [1], issues.map(&:id).sort
CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '!', ['value1'])
issues = find_issues_with_query(query)
assert !issues.map(&:id).include?(1)
assert !issues.map(&:id).include?(3)
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter("cf_#{f.id}", '!', ['value2'])
issues = find_issues_with_query(query)
assert !issues.map(&:id).include?(1)
# is_private filter only available for those who can set issues private
User.current = User.find(2)
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
assert query.available_filters.key?('is_private')
query.add_filter("is_private", '=', ['1'])
# is_private filter only available for those who can set issues private
User.current = User.find(2)
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
assert query.available_filters.key?('is_private')
query.add_filter("is_private", '!', ['1'])
end
def test_operator_greater_than
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '>=', ['40'])
assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0")
find_issues_with_query(query)
end
def test_operator_greater_than_a_float
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('estimated_hours', '>=', ['40.5'])
assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5")
find_issues_with_query(query)
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '>=', ['8'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
end
def test_operator_lesser_than
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '<=', ['30'])
assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0")
find_issues_with_query(query)
def test_operator_lesser_than_on_custom_field
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '<=', ['30'])
assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30.0")
find_issues_with_query(query)
end
def test_operator_between
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '><', ['30', '40'])
assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement
find_issues_with_query(query)
def test_operator_between_on_custom_field
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '><', ['30', '40'])
assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30.0 AND 40.0", query.statement
find_issues_with_query(query)
end
def test_date_filter_should_not_accept_non_date_values
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('created_on', '=', ['a'])
assert query.has_filter?('created_on')
end
def test_date_filter_should_not_accept_invalid_date_values
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('created_on', '=', ['2011-01-34'])
assert query.has_filter?('created_on')
end
def test_relative_date_filter_should_not_accept_non_integer_values
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('created_on', '>t-', ['a'])
assert query.has_filter?('created_on')
end
def test_operator_date_equals
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('due_date', '=', ['2011-07-10'])
assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
find_issues_with_query(query)
end
def test_operator_date_lesser_than
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('due_date', '<=', ['2011-07-10'])
assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
find_issues_with_query(query)
end
def test_operator_date_greater_than
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('due_date', '>=', ['2011-07-10'])
assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement
find_issues_with_query(query)
end
def test_operator_date_between
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10'])
assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
find_issues_with_query(query)
def test_operator_in_more_than
Issue.find(7).update_attribute(:due_date, (Date.today + 15))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '>t+', ['15'])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_in_less_than
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '<t+', ['15'])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_in_the_next_days
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '><t+', ['15'])
issues = find_issues_with_query(query)
assert !issues.empty?
def test_operator_less_than_ago
Issue.find(7).update_attribute(:due_date, (Date.today - 3))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '>t-', ['3'])
issues = find_issues_with_query(query)
assert !issues.empty?
def test_operator_in_the_past_days
Issue.find(7).update_attribute(:due_date, (Date.today - 3))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '><t-', ['3'])
issues = find_issues_with_query(query)
assert !issues.empty?
def test_operator_more_than_ago
Issue.find(7).update_attribute(:due_date, (Date.today - 10))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '<t-', ['10'])
assert query.statement.include?("#{Issue.table_name}.due_date <=")
issues = find_issues_with_query(query)
def test_operator_in
Issue.find(7).update_attribute(:due_date, (Date.today + 2))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 't+', ['2'])
issues = find_issues_with_query(query)
assert !issues.empty?
def test_operator_ago
Issue.find(7).update_attribute(:due_date, (Date.today - 3))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 't-', ['3'])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_today
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 't', [''])
issues = find_issues_with_query(query)
assert !issues.empty?
end
def test_operator_this_week_on_date
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 'w', [''])
find_issues_with_query(query)
end
def test_operator_this_week_on_datetime
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('created_on', 'w', [''])
find_issues_with_query(query)
end
def test_operator_contains
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('subject', '~', ['uNable'])
assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'")
result = find_issues_with_query(query)
Date.stubs(:today).returns(Date.parse('2011-04-29'))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 'w', [''])
assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}"
I18n.locale = :en
Date.stubs(:today).returns(Date.parse('2011-04-29'))
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 'w', [''])
assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}"
end
def test_operator_does_not_contains
- query = Query.new(:project => Project.find(1), :name => '_')
+ query = IssueQuery.new(:project => Project.find(1), :name => '_')
query.add_filter('subject', '!~', ['uNable'])
assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'")
find_issues_with_query(query)
i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11))
group.users << user
- query = Query.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}})
+ query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}})
result = query.issues
assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id)
issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1)
issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'})
- query = Query.new(:name => '_', :project => Project.find(1))
+ query = IssueQuery.new(:name => '_', :project => Project.find(1))
filter = query.available_filters["cf_#{cf.id}"]
assert_not_nil filter
assert_include 'me', filter[:values].map{|v| v[1]}
def test_filter_my_projects
User.current = User.find(2)
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
filter = query.available_filters['project_id']
assert_not_nil filter
assert_include 'mine', filter[:values].map{|v| v[1]}
def test_filter_watched_issues
User.current = User.find(1)
- query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}})
+ query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}})
result = find_issues_with_query(query)
assert_not_nil result
assert !result.empty?
def test_filter_unwatched_issues
User.current = User.find(1)
- query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}})
+ query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}})
result = find_issues_with_query(query)
assert_not_nil result
assert !result.empty?
CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
filter_name = "project.cf_#{field.id}"
assert_include filter_name, query.available_filters.keys
query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
filter_name = "author.cf_#{field.id}"
assert_include filter_name, query.available_filters.keys
query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
filter_name = "assigned_to.cf_#{field.id}"
assert_include filter_name, query.available_filters.keys
query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
field = VersionCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
CustomValue.create!(:custom_field => field, :customized => Version.find(2), :value => 'Foo')
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
filter_name = "fixed_version.cf_#{field.id}"
assert_include filter_name, query.available_filters.keys
query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=', :values => ['1']}}
assert_equal [2, 3], find_issues_with_query(query).map(&:id).sort
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=', :values => ['2']}}
assert_equal [1], find_issues_with_query(query).map(&:id).sort
end
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
end
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=p', :values => ['2']}}
assert_equal [1, 2], find_issues_with_query(query).map(&:id).sort
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=p', :values => ['3']}}
assert_equal [1], find_issues_with_query(query).map(&:id).sort
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=p', :values => ['4']}}
assert_equal [], find_issues_with_query(query).map(&:id).sort
end
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
end
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '=!p', :values => ['1']}}
assert_equal [1], find_issues_with_query(query).map(&:id).sort
end
IssueRelation.create!(:relation_type => "relates", :issue_to => Project.find(2).issues.first, :issue_from => Issue.find(3))
end
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '!p', :values => ['2']}}
ids = find_issues_with_query(query).map(&:id).sort
assert_include 2, ids
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '!*', :values => ['']}}
ids = find_issues_with_query(query).map(&:id)
assert_equal [], ids & [1, 2, 3]
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
- query = Query.new(:name => '_')
+ query = IssueQuery.new(:name => '_')
query.filters = {"relates" => {:operator => '*', :values => ['']}}
assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort
end
def test_statement_should_be_nil_with_no_filters
- q = Query.new(:name => '_')
+ q = IssueQuery.new(:name => '_')
q.filters = {}
assert q.valid?
end
def test_default_columns
- q = Query.new
+ q = IssueQuery.new
assert q.columns.any?
assert q.inline_columns.any?
assert q.block_columns.empty?
end
def test_set_column_names
- q = Query.new
+ q = IssueQuery.new
q.column_names = ['tracker', :subject, '', 'unknonw_column']
assert_equal [:tracker, :subject], q.columns.collect {|c| c.name}
c = q.columns.first
end
def test_inline_and_block_columns
- q = Query.new
+ q = IssueQuery.new
q.column_names = ['subject', 'description', 'tracker']
assert_equal [:subject, :tracker], q.inline_columns.map(&:name)
end
def test_custom_field_columns_should_be_inline
- q = Query.new
+ q = IssueQuery.new
columns = q.available_columns.select {|column| column.is_a? QueryCustomFieldColumn}
assert columns.any?
assert_nil columns.detect {|column| !column.inline?}
end
def test_query_should_preload_spent_hours
- q = Query.new(:name => '_', :column_names => [:subject, :spent_hours])
+ q = IssueQuery.new(:name => '_', :column_names => [:subject, :spent_hours])
assert q.has_column?(:spent_hours)
issues = q.issues
assert_not_nil issues.first.instance_variable_get("@spent_hours")
end
def test_groupable_columns_should_include_custom_fields
- q = Query.new
+ q = IssueQuery.new
column = q.groupable_columns.detect {|c| c.name == :cf_1}
assert_not_nil column
assert_kind_of QueryCustomFieldColumn, column
field = CustomField.find(1)
field.update_attribute :multiple, true
- q = Query.new
+ q = IssueQuery.new
column = q.groupable_columns.detect {|c| c.name == :cf_1}
assert_nil column
end
def test_groupable_columns_should_include_user_custom_fields
cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'user')
- q = Query.new
+ q = IssueQuery.new
assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
end
def test_groupable_columns_should_include_version_custom_fields
cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'version')
- q = Query.new
+ q = IssueQuery.new
assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
end
def test_grouped_with_valid_column
- q = Query.new(:group_by => 'status')
+ q = IssueQuery.new(:group_by => 'status')
assert q.grouped?
assert_not_nil q.group_by_column
assert_equal :status, q.group_by_column.name
end
def test_grouped_with_invalid_column
- q = Query.new(:group_by => 'foo')
+ q = IssueQuery.new(:group_by => 'foo')
assert !q.grouped?
assert_nil q.group_by_column
assert_nil q.group_by_statement
def test_sortable_columns_should_sort_assignees_according_to_user_format_setting
with_settings :user_format => 'lastname_coma_firstname' do
- q = Query.new
+ q = IssueQuery.new
assert q.sortable_columns.has_key?('assigned_to')
assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to']
end
def test_sortable_columns_should_sort_authors_according_to_user_format_setting
with_settings :user_format => 'lastname_coma_firstname' do
- q = Query.new
+ q = IssueQuery.new
assert q.sortable_columns.has_key?('author')
assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author']
end
end
def test_sortable_columns_should_include_custom_field
- q = Query.new
+ q = IssueQuery.new
assert q.sortable_columns['cf_1']
end
field = CustomField.find(1)
field.update_attribute :multiple, true
- q = Query.new
+ q = IssueQuery.new
assert !q.sortable_columns['cf_1']
end
def test_default_sort
- q = Query.new
+ q = IssueQuery.new
assert_equal [], q.sort_criteria
end
def test_set_sort_criteria_with_hash
- q = Query.new
+ q = IssueQuery.new
q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']}
assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
end
def test_set_sort_criteria_with_array
- q = Query.new
+ q = IssueQuery.new
q.sort_criteria = [['priority', 'desc'], 'tracker']
assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
end
def test_create_query_with_sort
- q = Query.new(:name => 'Sorted')
+ q = IssueQuery.new(:name => 'Sorted')
q.sort_criteria = [['priority', 'desc'], 'tracker']
assert q.save
q.reload
end
def test_sort_by_string_custom_field_asc
- q = Query.new
+ q = IssueQuery.new
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
assert c
assert c.sortable
end
def test_sort_by_string_custom_field_desc
- q = Query.new
+ q = IssueQuery.new
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
assert c
assert c.sortable
end
def test_sort_by_float_custom_field_asc
- q = Query.new
+ q = IssueQuery.new
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
assert c
assert c.sortable
end
def test_invalid_query_should_raise_query_statement_invalid_error
- q = Query.new
+ q = IssueQuery.new
assert_raise Query::StatementInvalid do
q.issues(:conditions => "foo = 1")
end
end
def test_issue_count
- q = Query.new(:name => '_')
+ q = IssueQuery.new(:name => '_')
issue_count = q.issue_count
assert_equal q.issues.size, issue_count
end
end
def test_issue_count_by_association_group
- q = Query.new(:name => '_', :group_by => 'assigned_to')
+ q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
count_by_group = q.issue_count_by_group
assert_kind_of Hash, count_by_group
assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
end
def test_issue_count_by_list_custom_field_group
- q = Query.new(:name => '_', :group_by => 'cf_1')
+ q = IssueQuery.new(:name => '_', :group_by => 'cf_1')
count_by_group = q.issue_count_by_group
assert_kind_of Hash, count_by_group
assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
end
def test_issue_count_by_date_custom_field_group
- q = Query.new(:name => '_', :group_by => 'cf_8')
+ q = IssueQuery.new(:name => '_', :group_by => 'cf_8')
count_by_group = q.issue_count_by_group
assert_kind_of Hash, count_by_group
assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
def test_issue_count_with_nil_group_only
Issue.update_all("assigned_to_id = NULL")
- q = Query.new(:name => '_', :group_by => 'assigned_to')
+ q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
count_by_group = q.issue_count_by_group
assert_kind_of Hash, count_by_group
assert_equal 1, count_by_group.keys.size
end
def test_issue_ids
- q = Query.new(:name => '_')
+ q = IssueQuery.new(:name => '_')
order = "issues.subject, issues.id"
issues = q.issues(:order => order)
assert_equal issues.map(&:id), q.issue_ids(:order => order)
def test_label_for
set_language_if_valid 'en'
- q = Query.new
+ q = IssueQuery.new
assert_equal 'Assignee', q.label_for('assigned_to_id')
end
def test_label_for_fr
set_language_if_valid 'fr'
- q = Query.new
+ q = IssueQuery.new
s = "Assign\xc3\xa9 \xc3\xa0"
s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
assert_equal s, q.label_for('assigned_to_id')
developer = User.find(3)
# Public query on project 1
- q = Query.find(1)
+ q = IssueQuery.find(1)
assert q.editable_by?(admin)
assert q.editable_by?(manager)
assert !q.editable_by?(developer)
# Private query on project 1
- q = Query.find(2)
+ q = IssueQuery.find(2)
assert q.editable_by?(admin)
assert !q.editable_by?(manager)
assert q.editable_by?(developer)
# Private query for all projects
- q = Query.find(3)
+ q = IssueQuery.find(3)
assert q.editable_by?(admin)
assert !q.editable_by?(manager)
assert q.editable_by?(developer)
# Public query for all projects
- q = Query.find(4)
+ q = IssueQuery.find(4)
assert q.editable_by?(admin)
assert !q.editable_by?(manager)
assert !q.editable_by?(developer)
end
def test_visible_scope
- query_ids = Query.visible(User.anonymous).map(&:id)
+ query_ids = IssueQuery.visible(User.anonymous).map(&:id)
assert query_ids.include?(1), 'public query on public project was not visible'
assert query_ids.include?(4), 'public query for all projects was not visible'
context "#available_filters" do
setup do
- @query = Query.new(:name => "_")
+ @query = IssueQuery.new(:name => "_")
end
should "include users of visible projects in cross-project view" do
end
should "search assigned to for users in the group" do
- @query = Query.new(:name => '_')
+ @query = IssueQuery.new(:name => '_')
@query.add_filter('member_of_group', '=', [@group.id.to_s])
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}')"
end
should "search not assigned to any group member (none)" do
- @query = Query.new(:name => '_')
+ @query = IssueQuery.new(:name => '_')
@query.add_filter('member_of_group', '!*', [''])
# Users not in a group
end
should "search assigned to any group member (all)" do
- @query = Query.new(:name => '_')
+ @query = IssueQuery.new(:name => '_')
@query.add_filter('member_of_group', '*', [''])
# Only users in a group
should "return an empty set with = empty group" do
@empty_group = Group.generate!
- @query = Query.new(:name => '_')
+ @query = IssueQuery.new(:name => '_')
@query.add_filter('member_of_group', '=', [@empty_group.id.to_s])
assert_equal [], find_issues_with_query(@query)
should "return issues with ! empty group" do
@empty_group = Group.generate!
- @query = Query.new(:name => '_')
+ @query = IssueQuery.new(:name => '_')
@query.add_filter('member_of_group', '!', [@empty_group.id.to_s])
assert_find_issues_with_query_is_successful @query
end
should "search assigned to for users with the Role" do
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
assert_query_result [@issue1, @issue3], @query
other_project = Project.generate!
User.add_to_project(@developer, other_project, @manager_role)
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
assert_query_result [@issue1, @issue3], @query
should "return an empty set with empty role" do
@empty_role = Role.generate!
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s])
assert_query_result [], @query
end
should "search assigned to for users without the Role" do
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s])
assert_query_result [@issue2, @issue4, @issue5], @query
end
should "search assigned to for users not assigned to any Role (none)" do
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '!*', [''])
assert_query_result [@issue4, @issue5], @query
end
should "search assigned to for users assigned to any Role (all)" do
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '*', [''])
assert_query_result [@issue1, @issue2, @issue3], @query
should "return issues with ! empty role" do
@empty_role = Role.generate!
- @query = Query.new(:name => '_', :project => @project)
+ @query = IssueQuery.new(:name => '_', :project => @project)
@query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s])
assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query
end
end
end
-
end