Class: ProjectQuery

Inherits:
Query show all
Defined in:
app/models/project_query.rb

Overview

Redmine - project management software Copyright © 2006- 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.

Constant Summary

Constants inherited from Query

Query::VISIBILITY_PRIVATE, Query::VISIBILITY_PUBLIC, Query::VISIBILITY_ROLES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Query

add_available_column, #add_available_filter, #add_filter, #add_filter_error, #add_filters, #add_short_filter, #all_projects, #all_projects_values, #as_params, #assigned_to_values, #author_values, #available_block_columns, #available_filters, #available_filters_as_json, #available_inline_columns, #available_totalable_columns, #block_columns, build_from_params, #column_names=, #columns, #css_classes, #default_totalable_names, #delete_available_filter, #display_type=, #editable_by?, #fixed_version_values, #group_by_column, #group_by_sort_order, #group_by_statement, #groupable_columns, #grouped?, #has_column?, #has_custom_field_column?, #has_default_columns?, #has_filter?, #inline_columns, #is_global?, #is_private?, #is_public?, #issue_custom_fields, #issue_statuses_values, #label_for, #operator_for, operators_labels, #principals, #project_custom_fields, #project_statement, #project_values, #queried_table_name, #result_count_by_group, #sort_clause, #sort_criteria, #sort_criteria=, #sort_criteria_key, #sort_criteria_order, #sortable_columns, #statement, #subproject_values, #time_entry_custom_fields, #total_by_group_for, #total_for, #totalable_columns, #totalable_names, #totalable_names=, #totals, #totals_by_group, #trackers, #type_for, #users, #validate_query_filters, #value_for, #values_for, visible, #visible?, #watcher_values

Methods included from Redmine::SubclassFactory

included

Methods inherited from ApplicationRecord

human_attribute_name

Constructor Details

#initialize(attributes = nil, *args) ⇒ ProjectQuery

Returns a new instance of ProjectQuery.



56
57
58
59
# File 'app/models/project_query.rb', line 56

def initialize(attributes=nil, *args)
  super(attributes)
  self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
end

Instance Attribute Details

#admin_projectsObject

Returns the value of attribute admin_projects.



21
22
23
# File 'app/models/project_query.rb', line 21

def admin_projects
  @admin_projects
end

Class Method Details

.default(project: nil, user: User.current) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/project_query.rb', line 44

def self.default(project: nil, user: User.current)
  if user&.logged? && (query_id = user.pref.default_project_query).present?
    query = find_by(id: query_id)
    return query if query&.visible?
  end
  if (query_id = Setting.default_project_query).present?
    query = find_by(id: query_id)
    return query if query&.visibility == VISIBILITY_PUBLIC
  end
  nil
end

Instance Method Details

#available_columnsObject



92
93
94
95
96
97
98
99
# File 'app/models/project_query.rb', line 92

def available_columns
  return @available_columns if @available_columns

  @available_columns = self.class.available_columns.dup
  @available_columns += project_custom_fields.visible.
                          map {|cf| QueryCustomFieldColumn.new(cf)}
  @available_columns
end

#available_display_typesObject



101
102
103
104
105
106
107
# File 'app/models/project_query.rb', line 101

def available_display_types
  if self.admin_projects
    ['list']
  else
    ['board', 'list']
  end
end

#base_scopeObject



138
139
140
141
142
143
144
# File 'app/models/project_query.rb', line 138

def base_scope
  if self.admin_projects
    Project.where(statement)
  else
    Project.visible.where(statement)
  end
end

#build_from_params(params, defaults = {}) ⇒ Object



86
87
88
89
90
# File 'app/models/project_query.rb', line 86

def build_from_params(params, defaults={})
  query = super
  query.admin_projects = params[:admin_projects]
  query
end

#default_columns_namesObject



126
127
128
# File 'app/models/project_query.rb', line 126

def default_columns_names
  @default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
end

#default_display_typeObject



130
131
132
# File 'app/models/project_query.rb', line 130

def default_display_type
  Setting.project_list_display_type
end

#default_sort_criteriaObject



134
135
136
# File 'app/models/project_query.rb', line 134

def default_sort_criteria
  [[]]
end

#display_typeObject



109
110
111
112
113
114
115
# File 'app/models/project_query.rb', line 109

def display_type
  if self.admin_projects
    'list'
  else
    super
  end
end

#initialize_available_filtersObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/project_query.rb', line 61

def initialize_available_filters
  add_available_filter(
    "status",
    :type => :list, :values => lambda {project_statuses_values}
  )
  add_available_filter(
    "id",
    :type => :list, :values => lambda {project_values}, :label => :field_project
  )
  add_available_filter "name", :type => :text
  add_available_filter "description", :type => :text
  add_available_filter(
    "parent_id",
    :type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
  )
  add_available_filter(
    "is_public",
    :type => :list,
    :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
  )
  add_available_filter "created_on", :type => :date_past
  add_available_filter "updated_on", :type => :date_past
  add_custom_fields_filters(project_custom_fields)
end

#project_statuses_valuesObject



117
118
119
120
121
122
123
124
# File 'app/models/project_query.rb', line 117

def project_statuses_values
  values = super
  if self.admin_projects
    values << [l(:project_status_archived), Project::STATUS_ARCHIVED.to_s]
    values << [l(:project_status_scheduled_for_deletion), Project::STATUS_SCHEDULED_FOR_DELETION.to_s]
  end
  values
end

#result_countObject

Returns the project count



147
148
149
150
151
# File 'app/models/project_query.rb', line 147

def result_count
  base_scope.count
rescue ::ActiveRecord::StatementInvalid => e
  raise StatementInvalid, e.message
end

#results_scope(options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/project_query.rb', line 153

def results_scope(options={})
  order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)

  order_option << "#{Project.table_name}.lft ASC"
  scope = base_scope.
    order(order_option).
    joins(joins_for_order_statement(order_option.join(',')))

  if has_custom_field_column?
    scope = scope.preload(:custom_values)
  end

  if has_column?(:parent_id)
    scope = scope.preload(:parent)
  end

  if has_column?(:last_activity_date)
    Project.load_last_activity_date(scope)
  end

  scope
end