Class: TimeEntryQuery
- Inherits:
-
Query
- Object
- ActiveRecord::Base
- Query
- TimeEntryQuery
show all
- Defined in:
- app/models/time_entry_query.rb
Overview
Redmine - project management software Copyright (C) 2006-2016
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) ⇒ Object
Accepts :from/:to params as shortcut filters.
-
#default_columns_names ⇒ Object
-
#default_totalable_names ⇒ Object
-
#initialize(attributes = nil, *args) ⇒ TimeEntryQuery
constructor
A new instance of TimeEntryQuery.
-
#initialize_available_filters ⇒ Object
-
#issue_custom_fields ⇒ Object
-
#joins_for_order_statement(order_options) ⇒ Object
-
#results_scope(options = {}) ⇒ Object
-
#sql_for_activity_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_status_id_field(field, operator, value) ⇒ Object
-
#sql_for_issue_tracker_id_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, #available_block_columns, #available_filters, #available_filters_as_json, #available_inline_columns, #available_totalable_columns, #block_columns, build_from_params, #column_names=, #columns, #delete_available_filter, #editable_by?, #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_private?, #is_public?, #label_for, #operator_for, operators_labels, #project_statement, #queried_table_name, #sort_criteria, #sort_criteria=, #sort_criteria_key, #sort_criteria_order, #sort_criteria_order_for, #sortable_columns, #statement, #total_by_group_for, #total_for, #totalable_columns, #totalable_names, #totalable_names=, #totals, #totals_by_group, #trackers, #type_for, #validate_query_filters, #value_for, #values_for, visible, #visible?
included
Constructor Details
#initialize(attributes = nil, *args) ⇒ TimeEntryQuery
Returns a new instance of TimeEntryQuery
36
37
38
39
40
|
# File 'app/models/time_entry_query.rb', line 36
def initialize(attributes=nil, *args)
super attributes
self.filters ||= {}
add_filter('spent_on', '*') unless filters.present?
end
|
Instance Method Details
#available_columns ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'app/models/time_entry_query.rb', line 114
def available_columns
return @available_columns if @available_columns
@available_columns = self.class.available_columns.dup
@available_columns += TimeEntryCustomField.visible.
map {|cf| QueryCustomFieldColumn.new(cf) }
@available_columns += issue_custom_fields.visible.
map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf, :totalable => false) }
@available_columns
end
|
#base_scope ⇒ Object
136
137
138
139
140
141
|
# File 'app/models/time_entry_query.rb', line 136
def base_scope
TimeEntry.visible.
joins(:project, :user).
joins("LEFT OUTER JOIN issues ON issues.id = time_entries.issue_id").
where(statement)
end
|
#build_from_params(params) ⇒ Object
Accepts :from/:to params as shortcut filters
215
216
217
218
219
220
221
222
223
224
225
|
# File 'app/models/time_entry_query.rb', line 215
def build_from_params(params)
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
124
125
126
127
128
129
130
|
# File 'app/models/time_entry_query.rb', line 124
def default_columns_names
@default_columns_names ||= begin
default_columns = [:spent_on, :user, :activity, :issue, :comments, :hours]
project.present? ? default_columns : [:project] | default_columns
end
end
|
#default_totalable_names ⇒ Object
132
133
134
|
# File 'app/models/time_entry_query.rb', line 132
def default_totalable_names
[:hours]
end
|
#initialize_available_filters ⇒ Object
42
43
44
45
46
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
|
# File 'app/models/time_entry_query.rb', line 42
def initialize_available_filters
add_available_filter "spent_on", :type => :date_past
principals = []
versions = []
if project
principals += project.principals.visible.sort
unless project.leaf?
subprojects = project.descendants.visible.to_a
if subprojects.any?
add_available_filter "subproject_id",
:type => :list_subprojects,
:values => subprojects.collect{|s| [s.name, s.id.to_s] }
principals += Principal.member_of(subprojects).visible
end
end
versions = project.shared_versions.to_a
else
if all_projects.any?
principals += Principal.member_of(all_projects).visible
project_values = []
if User.current.logged? && User.current.memberships.any?
project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
end
project_values += all_projects_values
add_available_filter("project_id",
:type => :list, :values => project_values
) unless project_values.empty?
end
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 => Tracker.sorted.map {|t| [t.name, t.id.to_s]})
add_available_filter("issue.status_id",
:type => :list,
:name => l("label_attribute_of_issue", :name => l(:field_status)),
:values => IssueStatus.sorted.map {|s| [s.name, s.id.to_s]})
add_available_filter("issue.fixed_version_id",
:type => :list,
:name => l("label_attribute_of_issue", :name => l(:field_fixed_version)),
:values => Version.sort_by_status(versions).collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s, l("version_status_#{s.status}")] })
principals.uniq!
principals.sort!
users = principals.select {|p| p.is_a?(User)}
users_values = []
users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
users_values += users.collect{|s| [s.name, s.id.to_s] }
add_available_filter("user_id",
:type => :list_optional, :values => users_values
) unless users_values.empty?
activities = (project ? project.activities : TimeEntryActivity.shared)
add_available_filter("activity_id",
:type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
) unless activities.empty?
add_available_filter "comments", :type => :text
add_available_filter "hours", :type => :float
add_custom_fields_filters(TimeEntryCustomField)
add_associations_custom_fields_filters :project
add_custom_fields_filters(issue_custom_fields, :issue)
add_associations_custom_fields_filters :user
end
|
#issue_custom_fields ⇒ Object
243
244
245
246
247
248
249
|
# File 'app/models/time_entry_query.rb', line 243
def issue_custom_fields
if project
project.all_issue_custom_fields
else
IssueCustomField.where(:is_for_all => true)
end
end
|
#joins_for_order_statement(order_options) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'app/models/time_entry_query.rb', line 227
def joins_for_order_statement(order_options)
joins = [super]
if order_options
if order_options.include?('issue_statuses')
joins << "LEFT OUTER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id"
end
if order_options.include?('trackers')
joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{Issue.table_name}.tracker_id"
end
end
joins.compact!
joins.any? ? joins.join(' ') : nil
end
|
#results_scope(options = {}) ⇒ Object
143
144
145
146
147
148
149
150
151
|
# File 'app/models/time_entry_query.rb', line 143
def results_scope(options={})
order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
base_scope.
order(order_option).
joins(joins_for_order_statement(order_option.join(','))).
includes(:activity).
references(:activity)
end
|
#sql_for_activity_id_field(field, operator, value) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
|
# File 'app/models/time_entry_query.rb', line 194
def sql_for_activity_id_field(field, operator, value)
condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
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_fixed_version_id_field(field, operator, value) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'app/models/time_entry_query.rb', line 176
def sql_for_issue_fixed_version_id_field(field, operator, value)
issue_ids = Issue.where(:fixed_version_id => value.first.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'app/models/time_entry_query.rb', line 158
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_status_id_field(field, operator, value) ⇒ Object
210
211
212
|
# File 'app/models/time_entry_query.rb', line 210
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_tracker_id_field(field, operator, value) ⇒ Object
206
207
208
|
# File 'app/models/time_entry_query.rb', line 206
def sql_for_issue_tracker_id_field(field, operator, value)
sql_for_field("tracker_id", operator, value, Issue.table_name, "tracker_id")
end
|
#total_for_hours(scope) ⇒ Object
Returns sum of all the spent hours
154
155
156
|
# File 'app/models/time_entry_query.rb', line 154
def total_for_hours(scope)
map_total(scope.sum(:hours)) {|t| t.to_f.round(2)}
end
|