-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathbase_query.rb
More file actions
268 lines (215 loc) · 6.85 KB
/
Copy pathbase_query.rb
File metadata and controls
268 lines (215 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# See COPYRIGHT and LICENSE files for more details.
#++
module Queries::BaseQuery
extend ActiveSupport::Concern
included do
include Queries::Filters::AvailableFilters
include Queries::Selects::AvailableSelects
include Queries::Orders::AvailableOrders
include Queries::GroupBys::AvailableGroupBys
include Queries::ValidSubset
include ActiveModel::Validations
validate :filters_valid,
:sortation_valid
validate :group_bys_valid, if: -> { respond_to?(:group_bys) }
end
class_methods do
def model
@model ||= name.demodulize.gsub("Query", "").constantize
end
def i18n_scope
:activerecord
end
# Also use the Query class' as a lookup ancestor so that error messages, etc can be shared.
# So if nothing is defined for the specific query class, we fall back to the generic query class.
#
# This is useful for error messages, because we can fall back to error messages, etc in
# activerecord.errors.models.query
def lookup_ancestors
super + [Query]
end
end
def results
if valid?
apply_orders(apply_filters(default_scope))
else
empty_scope
end
end
def groups
return nil if group_bys.empty?
return empty_scope unless valid?
apply_group_by(apply_filters(default_scope))
.select(*group_by_names, Arel.sql("COUNT(*)"))
end
# Keys are the plain value when grouping by a single attribute, and an array
# of values - one per group by - when grouping by several.
def group_values
groups_hash = groups.pluck(*group_by_names, Arel.sql("COUNT(*)")).to_h do |*values, count|
[group_bys.one? ? values.first : values, count]
end
instantiate_group_keys groups_hash
end
def where(attribute, operator, values)
filter = filter_for(attribute)
filter.operator = operator
filter.values = values
filter.context = context
# Remove any previous instances of the same filter
remove_filter(filter.name)
filters << filter
self
end
def remove_filter(name)
filters.delete(find_active_filter(name))
end
def select(*select_values, add_not_existing: true)
select_values.each do |select_value|
select_column = select_for(select_value)
if !select_column.is_a?(::Queries::Selects::NotExistingSelect) || add_not_existing
selects << select_column
end
end
self
end
def order(hash)
hash.each do |attribute, direction|
order = order_for(attribute)
order.direction = direction.to_sym
orders << order
end
self
end
def group(*attributes)
self.group_bys = attributes.map { |attribute| group_by_for(attribute) }
self
end
def default_scope
self.class.model.all
end
def find_active_filter(name)
filters.detect { |f| f.name == name }
end
def find_available_filter(name)
available_filters.detect { |f| f.name == name }
end
def ordered?
orders.any?
end
protected
def filters_valid
filters.each do |filter|
next if filter.valid?
add_error(:filters, filter.human_name, filter)
end
end
def sortation_valid
orders.each do |order|
next if order.valid?
add_error(:orders, order.name, order)
end
end
def group_bys_valid
group_bys.each do |group_by|
next if group_by.valid?
add_error(:group_by, group_by.name, group_by)
end
end
def add_error(local_attribute, attribute_name, object)
messages = object
.errors
.messages
.values
.flatten
.join(" #{I18n.t('support.array.sentence_connector')} ")
errors.add local_attribute, errors.full_message(attribute_name, messages)
end
def empty_scope
self.class.model.where(Arel::Nodes::Equality.new(1, 0))
end
def context
self
end
def apply_filters(query_scope)
filters.inject(query_scope) do |scope, filter|
filter.apply_to(scope)
end
end
def apply_orders(query_scope)
query_scope = build_orders.inject(query_scope) do |scope, order|
order.apply_to(scope)
end
# To get deterministic results, especially when paginating (limit + offset)
# an order needs to be prepended that is ensured to be
# different between all elements.
# Without such a criteria, results can occur on multiple pages.
already_ordered_by_id?(query_scope) ? query_scope : query_scope.order(id: :desc)
end
def apply_group_by(query_scope)
return query_scope if group_bys.empty?
group_bys
.inject(query_scope) { |scope, group_by| group_by.apply_to(scope) }
.order(*group_by_names)
end
def group_by_names
group_bys.map(&:name)
end
def build_orders
return orders unless respond_to?(:group_bys)
group_by_orders + orders
end
def group_by_orders
group_bys
.reject { |group_by| ordered_by?(group_by) }
.map { |group_by| group_by_order(group_by) }
end
def ordered_by?(group_by)
orders.any? { |order| order.class.key == group_by.order_key }
end
def group_by_order(group_by)
order_for(group_by.order_key).tap do |order|
order.direction = :asc
end
end
def instantiate_group_keys(groups)
group_by = group_bys.first
return groups unless group_bys.one? && group_by.association_class
ar_keys = group_by.association_class.where(id: groups.keys.compact)
groups.transform_keys do |key|
ar_keys.detect { |ar_key| ar_key.id == key } || "#{key} #{I18n.t(:label_not_found)}"
end
end
def already_ordered_by_id?(scope)
scope.order_values.any? do |order|
order.respond_to?(:value) && order.value.respond_to?(:relation) &&
order.value.relation.name == self.class.model.table_name &&
order.value.name == "id"
end
end
end