Class: TimeEntryQuery
- Inherits:
-
Query
- Object
- ActiveRecord::Base
- ApplicationRecord
- Query
- TimeEntryQuery
- Defined in:
- app/models/time_entry_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 Method Summary collapse
- #available_columns ⇒ Object
- #base_scope ⇒ Object
-
#build_from_params(params, defaults = {}) ⇒ Object
Accepts :from/:to params as shortcut filters.
- #default_columns_names ⇒ Object
- #default_sort_criteria ⇒ Object
- #default_totalable_names ⇒ Object
-
#filtered_issue_id ⇒ Object
If a filter against a single issue is set, returns its id, otherwise nil.
-
#initialize(attributes = nil, *args) ⇒ TimeEntryQuery
constructor
A new instance of TimeEntryQuery.
- #initialize_available_filters ⇒ Object
- #joins_for_order_statement(order_options) ⇒ Object
- #results_scope(options = {}) ⇒ Object
- #sql_for_activity_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_category_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_fixed_version_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_parent_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_status_id_field(field, operator, value) ⇒ Object
- #sql_for_issue_subject_field(field, operator, value) ⇒ Object
- #sql_for_issue_tracker_id_field(field, operator, value) ⇒ Object
- #sql_for_project_status_field(field, operator, value, options = {}) ⇒ Object
- #sql_for_user_group_field(field, operator, value) ⇒ Object
- #sql_for_user_role_field(field, operator, value) ⇒ Object
-
#total_for_hours(scope) ⇒ Object
Returns sum of all the spent hours.
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_display_types, #available_filters, #available_filters_as_json, #available_inline_columns, #available_totalable_columns, #block_columns, build_from_params, #column_names=, #columns, #css_classes, default, #default_display_type, #delete_available_filter, #display_type, #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_statuses_values, #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
Methods inherited from ApplicationRecord
Constructor Details
#initialize(attributes = nil, *args) ⇒ TimeEntryQuery
Returns a new instance of TimeEntryQuery.
42 43 44 45 |
# File 'app/models/time_entry_query.rb', line 42 def initialize(attributes=nil, *args) super(attributes) self.filters ||= {'spent_on' => {:operator => "*", :values => []}} end |
Instance Method Details
#available_columns ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/time_entry_query.rb', line 131 def available_columns return @available_columns if @available_columns @available_columns = self.class.available_columns.dup @available_columns += time_entry_custom_fields.visible. map {|cf| QueryCustomFieldColumn.new(cf)} @available_columns += issue_custom_fields.visible. map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf, :totalable => false)} @available_columns += project_custom_fields.visible. map {|cf| QueryAssociationCustomFieldColumn.new(:project, cf)} @available_columns end |
#base_scope ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'app/models/time_entry_query.rb', line 166 def base_scope scope = TimeEntry.visible .joins(:project, :user) .includes(:activity) .references(:activity) .left_join_issue .where(statement) if Redmine::Database.mysql? && ActiveRecord::Base.connection.supports_optimizer_hints? # Provides MySQL with a hint to use a better join order and avoid slow response times scope.optimizer_hints('JOIN_ORDER(time_entries, projects, users)') else scope end end |
#build_from_params(params, defaults = {}) ⇒ Object
Accepts :from/:to params as shortcut filters
334 335 336 337 338 339 340 341 342 343 344 |
# File 'app/models/time_entry_query.rb', line 334 def build_from_params(params, defaults={}) super if params[:from].present? && params[:to].present? add_filter('spent_on', '><', [params[:from], params[:to]]) elsif params[:from].present? add_filter('spent_on', '>=', [params[:from]]) elsif params[:to].present? add_filter('spent_on', '<=', [params[:to]]) end self end |
#default_columns_names ⇒ Object
144 145 146 147 148 149 |
# File 'app/models/time_entry_query.rb', line 144 def default_columns_names @default_columns_names ||= begin default_columns = Setting.time_entry_list_defaults.symbolize_keys[:column_names].map(&:to_sym) project.present? ? default_columns : [:project] | default_columns end end |
#default_sort_criteria ⇒ Object
155 156 157 |
# File 'app/models/time_entry_query.rb', line 155 def default_sort_criteria [['spent_on', 'desc']] end |
#default_totalable_names ⇒ Object
151 152 153 |
# File 'app/models/time_entry_query.rb', line 151 def default_totalable_names Setting.time_entry_list_defaults.symbolize_keys[:totalable_names].map(&:to_sym) end |
#filtered_issue_id ⇒ Object
If a filter against a single issue is set, returns its id, otherwise nil.
160 161 162 163 164 |
# File 'app/models/time_entry_query.rb', line 160 def filtered_issue_id if value_for('issue_id').to_s =~ /\A(\d+)\z/ $1 end end |
#initialize_available_filters ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/time_entry_query.rb', line 47 def initialize_available_filters add_available_filter "spent_on", :type => :date_past add_available_filter( "project_id", :type => :list, :values => lambda {project_values} ) if project.nil? if project && !project.leaf? add_available_filter( "subproject_id", :type => :list_subprojects, :values => lambda {subproject_values}) end add_available_filter("issue_id", :type => :tree, :label => :label_issue) add_available_filter( "issue.tracker_id", :type => :list, :name => l("label_attribute_of_issue", :name => l(:field_tracker)), :values => lambda {trackers.map {|t| [t.name, t.id.to_s]}}) add_available_filter( "issue.parent_id", :type => :tree, :name => l("label_attribute_of_issue", :name => l(:field_parent_issue))) add_available_filter( "issue.status_id", :type => :list, :name => l("label_attribute_of_issue", :name => l(:field_status)), :values => lambda {issue_statuses_values}) add_available_filter( "issue.fixed_version_id", :type => :list, :name => l("label_attribute_of_issue", :name => l(:field_fixed_version)), :values => lambda {fixed_version_values}) add_available_filter( "issue.category_id", :type => :list_optional, :name => l("label_attribute_of_issue", :name => l(:field_category)), :values => lambda {project.issue_categories.pluck(:name, :id).map {|name, id| [name, id.to_s]}} ) if project add_available_filter( "issue.subject", :type => :text, :name => l("label_attribute_of_issue", :name => l(:field_subject)) ) add_available_filter( "user_id", :type => :list_optional, :values => lambda {} ) add_available_filter( "user.group", :type => :list_optional, :name => l("label_attribute_of_user", :name => l(:label_group)), :values => lambda {Group.givable.visible.pluck(:name, :id).map {|name, id| [name, id.to_s]}} ) add_available_filter( "user.role", :type => :list_optional, :name => l("label_attribute_of_user", :name => l(:field_role)), :values => lambda {Role.givable.pluck(:name, :id).map {|name, id| [name, id.to_s]}} ) add_available_filter( "author_id", :type => :list_optional, :values => lambda {} ) activities = (project ? project.activities : TimeEntryActivity.shared) add_available_filter( "activity_id", :type => :list, :values => activities.map {|a| [a.name, (a.parent_id || a.id).to_s]} ) add_available_filter( "project.status", :type => :list, :name => l(:label_attribute_of_project, :name => l(:field_status)), :values => lambda {project_statuses_values} ) if project.nil? || !project.leaf? add_available_filter "comments", :type => :text add_available_filter "hours", :type => :float add_custom_fields_filters(time_entry_custom_fields) add_associations_custom_fields_filters :project add_custom_fields_filters(issue_custom_fields, :issue) add_associations_custom_fields_filters :user end |
#joins_for_order_statement(order_options) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'app/models/time_entry_query.rb', line 346 def joins_for_order_statement() joins = [super] if if .include?('issue_statuses') joins << "LEFT OUTER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id" end if .include?('trackers') joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{Issue.table_name}.tracker_id" end if .include?('issue_categories') joins << "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{Issue.table_name}.category_id" end if .include?('versions') joins << "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{Issue.table_name}.fixed_version_id" end end joins.compact! joins.any? ? joins.join(' ') : nil end |
#results_scope(options = {}) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'app/models/time_entry_query.rb', line 182 def results_scope(={}) order_option = [group_by_sort_order, ([:order] || sort_clause)].flatten.reject(&:blank?) order_option << "#{TimeEntry.table_name}.id ASC" base_scope. order(order_option). joins(joins_for_order_statement(order_option.join(','))) end |
#sql_for_activity_id_field(field, operator, value) ⇒ Object
256 257 258 259 260 261 262 263 264 |
# File 'app/models/time_entry_query.rb', line 256 def sql_for_activity_id_field(field, operator, value) ids = value.map(&:to_i).join(',') table_name = Enumeration.table_name if operator == '=' "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))" else "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))" end end |
#sql_for_issue_category_id_field(field, operator, value) ⇒ Object
274 275 276 |
# File 'app/models/time_entry_query.rb', line 274 def sql_for_issue_category_id_field(field, operator, value) sql_for_field("category_id", operator, value, Issue.table_name, "category_id") end |
#sql_for_issue_fixed_version_id_field(field, operator, value) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'app/models/time_entry_query.rb', line 214 def sql_for_issue_fixed_version_id_field(field, operator, value) issue_ids = Issue.where(:fixed_version_id => value.map(&:to_i)).pluck(:id) case operator when "=" if issue_ids.any? "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})" else "1=0" end when "!" if issue_ids.any? "#{TimeEntry.table_name}.issue_id NOT IN (#{issue_ids.join(',')})" else "1=1" end end end |
#sql_for_issue_id_field(field, operator, value) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/models/time_entry_query.rb', line 196 def sql_for_issue_id_field(field, operator, value) case operator when "=" "#{TimeEntry.table_name}.issue_id = #{value.first.to_i}" when "~" issue = Issue.where(:id => value.first.to_i).first if issue && (issue_ids = issue.self_and_descendants.pluck(:id)).any? "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})" else "1=0" end when "!*" "#{TimeEntry.table_name}.issue_id IS NULL" when "*" "#{TimeEntry.table_name}.issue_id IS NOT NULL" end end |
#sql_for_issue_parent_id_field(field, operator, value) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'app/models/time_entry_query.rb', line 232 def sql_for_issue_parent_id_field(field, operator, value) case operator when "=" # accepts a comma separated list of ids parent_ids = value.first.to_s.scan(/\d+/).map(&:to_i).uniq issue_ids = Issue.where(:parent_id => parent_ids).pluck(:id) if issue_ids.present? "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})" else "1=0" end when "~" root_id, lft, rgt = Issue.where(:id => value.first.to_i).pick(:root_id, :lft, :rgt) issue_ids = Issue.where("#{Issue.table_name}.root_id = ? AND #{Issue.table_name}.lft > ? AND #{Issue.table_name}.rgt < ?", root_id, lft, rgt).pluck(:id) if root_id && lft && rgt if issue_ids.present? "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})" else "1=0" end else sql_for_field("parent_id", operator, value, Issue.table_name, "parent_id") end end |
#sql_for_issue_status_id_field(field, operator, value) ⇒ Object
270 271 272 |
# File 'app/models/time_entry_query.rb', line 270 def sql_for_issue_status_id_field(field, operator, value) sql_for_field("status_id", operator, value, Issue.table_name, "status_id") end |
#sql_for_issue_subject_field(field, operator, value) ⇒ Object
278 279 280 |
# File 'app/models/time_entry_query.rb', line 278 def sql_for_issue_subject_field(field, operator, value) sql_for_field("subject", operator, value, Issue.table_name, "subject") end |
#sql_for_issue_tracker_id_field(field, operator, value) ⇒ Object
266 267 268 |
# File 'app/models/time_entry_query.rb', line 266 def sql_for_issue_tracker_id_field(field, operator, value) sql_for_field("tracker_id", operator, value, Issue.table_name, "tracker_id") end |
#sql_for_project_status_field(field, operator, value, options = {}) ⇒ Object
282 283 284 |
# File 'app/models/time_entry_query.rb', line 282 def sql_for_project_status_field(field, operator, value, ={}) sql_for_field(field, operator, value, Project.table_name, "status") end |
#sql_for_user_group_field(field, operator, value) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/models/time_entry_query.rb', line 286 def sql_for_user_group_field(field, operator, value) if operator == '*' # Any group groups = Group.givable operator = '=' elsif operator == '!*' groups = Group.givable operator = '!' else groups = Group.where(:id => value).to_a end groups ||= [] members_of_groups = groups.inject([]) do |user_ids, group| user_ids + group.user_ids end.uniq.compact.sort.collect(&:to_s) '(' + sql_for_field('user_id', operator, members_of_groups, TimeEntry.table_name, "user_id", false) + ')' end |
#sql_for_user_role_field(field, operator, value) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/models/time_entry_query.rb', line 305 def sql_for_user_role_field(field, operator, value) case operator when "*", "!*" sw = operator == "!*" ? "NOT" : "" nl = operator == "!*" ? "#{TimeEntry.table_name}.user_id IS NULL OR" : "" subquery = "SELECT 1" + " FROM #{Member.table_name}" + " WHERE #{TimeEntry.table_name}.project_id = #{Member.table_name}.project_id AND #{Member.table_name}.user_id = #{TimeEntry.table_name}.user_id" "(#{nl} #{sw} EXISTS (#{subquery}))" when "=", "!" role_cond = if value.any? "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" else "1=0" end sw = operator == "!" ? 'NOT' : '' nl = operator == "!" ? "#{TimeEntry.table_name}.user_id IS NULL OR" : '' subquery = "SELECT 1" + " FROM #{Member.table_name} inner join #{MemberRole.table_name} on members.id = member_roles.member_id" + " WHERE #{TimeEntry.table_name}.project_id = #{Member.table_name}.project_id AND #{Member.table_name}.user_id = #{TimeEntry.table_name}.user_id AND #{role_cond}" "(#{nl} #{sw} EXISTS (#{subquery}))" end end |
#total_for_hours(scope) ⇒ Object
Returns sum of all the spent hours
192 193 194 |
# File 'app/models/time_entry_query.rb', line 192 def total_for_hours(scope) map_total(scope.sum(:hours)) {|t| t.to_f.round(2)} end |