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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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. self.available_columns = [
  22. QueryColumn.new(:name, :sortable => "#{Project.table_name}.name"),
  23. QueryColumn.new(:status, :sortable => "#{Project.table_name}.status"),
  24. QueryColumn.new(:short_description, :sortable => "#{Project.table_name}.description", :caption => :field_description),
  25. QueryColumn.new(:homepage, :sortable => "#{Project.table_name}.homepage"),
  26. QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
  27. QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
  28. QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
  29. QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
  30. ]
  31. def initialize(attributes=nil, *args)
  32. super attributes
  33. self.filters ||= { 'status' => {:operator => "=", :values => ['1']} }
  34. end
  35. def initialize_available_filters
  36. add_available_filter "status",
  37. :type => :list, :values => lambda { project_statuses_values }
  38. add_available_filter("id",
  39. :type => :list, :values => lambda { project_values }, :label => :field_project
  40. )
  41. add_available_filter "name", :type => :text
  42. add_available_filter "description", :type => :text
  43. add_available_filter("parent_id",
  44. :type => :list_subprojects, :values => lambda { project_values }, :label => :field_parent
  45. )
  46. add_available_filter "is_public",
  47. :type => :list,
  48. :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
  49. add_available_filter "created_on", :type => :date_past
  50. add_custom_fields_filters(project_custom_fields)
  51. end
  52. def available_columns
  53. return @available_columns if @available_columns
  54. @available_columns = self.class.available_columns.dup
  55. @available_columns += ProjectCustomField.visible.
  56. map {|cf| QueryAssociationCustomFieldColumn.new(:project, cf) }
  57. @available_columns
  58. end
  59. def available_display_types
  60. ['board', 'list']
  61. end
  62. def default_columns_names
  63. @default_columns_names ||= [:name, :identifier, :short_description]
  64. end
  65. def default_sort_criteria
  66. [[]]
  67. end
  68. def base_scope
  69. Project.visible.where(statement)
  70. end
  71. def results_scope(options={})
  72. order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
  73. order_option << "#{Project.table_name}.id ASC"
  74. scope = base_scope.
  75. order(order_option).
  76. joins(joins_for_order_statement(order_option.join(',')))
  77. if has_custom_field_column?
  78. scope = scope.preload(:custom_values)
  79. end
  80. if has_column?(:parent_id)
  81. scope = scope.preload(:parent)
  82. end
  83. scope
  84. end
  85. end