You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_query.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class ProjectQuery < Query
  19. self.queried_class = Project
  20. self.view_permission = :search_project
  21. validate do |query|
  22. # project must be blank for ProjectQuery
  23. errors.add(:project_id, :exclusion) if query.project_id.present?
  24. end
  25. self.available_columns = [
  26. QueryColumn.new(:name, :sortable => "#{Project.table_name}.name"),
  27. QueryColumn.new(:status, :sortable => "#{Project.table_name}.status"),
  28. QueryColumn.new(:short_description, :sortable => "#{Project.table_name}.description", :caption => :field_description),
  29. QueryColumn.new(:homepage, :sortable => "#{Project.table_name}.homepage"),
  30. QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
  31. QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
  32. QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
  33. QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
  34. ]
  35. def initialize(attributes=nil, *args)
  36. super attributes
  37. self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
  38. end
  39. def initialize_available_filters
  40. add_available_filter(
  41. "status",
  42. :type => :list, :values => lambda {project_statuses_values}
  43. )
  44. add_available_filter(
  45. "id",
  46. :type => :list, :values => lambda {project_values}, :label => :field_project
  47. )
  48. add_available_filter "name", :type => :text
  49. add_available_filter "description", :type => :text
  50. add_available_filter(
  51. "parent_id",
  52. :type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
  53. )
  54. add_available_filter(
  55. "is_public",
  56. :type => :list,
  57. :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
  58. )
  59. add_available_filter "created_on", :type => :date_past
  60. add_custom_fields_filters(project_custom_fields)
  61. end
  62. def available_columns
  63. return @available_columns if @available_columns
  64. @available_columns = self.class.available_columns.dup
  65. @available_columns += project_custom_fields.visible.
  66. map {|cf| QueryCustomFieldColumn.new(cf)}
  67. @available_columns
  68. end
  69. def available_display_types
  70. ['board', 'list']
  71. end
  72. def default_columns_names
  73. @default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
  74. end
  75. def default_display_type
  76. Setting.project_list_display_type
  77. end
  78. def default_sort_criteria
  79. [[]]
  80. end
  81. def base_scope
  82. Project.visible.where(statement)
  83. end
  84. def results_scope(options={})
  85. order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
  86. order_option << "#{Project.table_name}.lft ASC"
  87. scope = base_scope.
  88. order(order_option).
  89. joins(joins_for_order_statement(order_option.join(',')))
  90. if has_custom_field_column?
  91. scope = scope.preload(:custom_values)
  92. end
  93. if has_column?(:parent_id)
  94. scope = scope.preload(:parent)
  95. end
  96. scope
  97. end
  98. end