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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/hobo/model/scopes/automatic_scopes.rb', line 31
def create_scope(check_only=false)
matched_scope = true
like_operator = ActiveRecord::Base.connection.adapter_name =~ /postg/i ? 'ILIKE' : 'LIKE'
case
when name =~ /^with_(.*)/ && (refl = reflection($1))
return true if check_only
def_scope do |*records|
if records.empty?
@klass.where exists_sql_condition(refl, true)
else
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" AND ")
@klass.where *([exists_sql] + records)
end
end
when name =~ /^with_(.*)/ && (refl = reflection($1.pluralize))
return true if check_only
exists_sql = exists_sql_condition(refl)
def_scope do |record|
record = find_if_named(refl, record)
@klass.where exists_sql, record
end
when name =~ /^any_of_(.*)/ && (refl = reflection($1))
return true if check_only
def_scope do |*records|
if records.empty?
@klass.where exists_sql_condition(refl, true)
else
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" OR ")
@klass.where *([exists_sql] + records)
end
end
when name =~ /^without_(.*)/ && (refl = reflection($1))
return true if check_only
def_scope do |*records|
if records.empty?
@klass.where "NOT (#{exists_sql_condition(refl, true)})"
else
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" AND ")
@klass.where *(["NOT (#{exists_sql})"] + records)
end
end
when name =~ /^without_(.*)/ && (refl = reflection($1.pluralize))
return true if check_only
exists_sql = exists_sql_condition(refl)
def_scope do |record|
record = find_if_named(refl, record)
@klass.where "NOT #{exists_sql}", record
end
when name =~ /^(.*)_is$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
return true if check_only
if refl.options[:polymorphic]
def_scope do |record|
record = find_if_named(refl, record)
@klass.where "#{foreign_key_column refl} = ? AND #{$1}_type = ?", record, record.class.name
end
else
def_scope do |record|
record = find_if_named(refl, record)
@klass.where "#{foreign_key_column refl} = ?", record
end
end
when name =~ /^(.*)_is_not$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
return true if check_only
if refl.options[:polymorphic]
def_scope do |record|
record = find_if_named(refl, record)
@klass.where "#{foreign_key_column refl} <> ? OR #{name}_type <> ?", record, record.class.name
end
else
def_scope do |record|
record = find_if_named(refl, record)
@klass.where "#{foreign_key_column refl} <> ?", record
end
end
when name =~ /^(.*)_is$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} = ?", str
end
when name =~ /^(.*)_is_not$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} <> ?", str
end
when name =~ /^(.*)_contains$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} #{like_operator} ?", "%#{str}%"
end
when name =~ /^(.*)_does_not_contain$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} NOT #{like_operator} ?", "%#{str}%"
end
when name =~ /^(.*)_starts$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} #{like_operator} ?", "#{str}%"
end
when name =~ /^(.*)_does_not_start$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} NOT #{like_operator} ?", "#{str}%"
end
when name =~ /^(.*)_ends$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} #{like_operator} ?", "%#{str}"
end
when name =~ /^(.*)_does_not_end$/ && (col = column($1))
return true if check_only
def_scope do |str|
@klass.where "#{column_sql(col)} NOT #{like_operator} ?", "%#{str}"
end
when (col = column(name)) && (col.type == :boolean)
return true if check_only
def_scope do
@klass.where "#{column_sql(col)} = ?", true
end
when name =~ /^not_(.*)$/ && (col = column($1)) && (col.type == :boolean)
return true if check_only
def_scope do
@klass.where "#{column_sql(col)} <> ?", true
end
when name =~ /^(.*)_before$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only
def_scope do |time|
@klass.where "#{column_sql(col)} < ?", time
end
when name =~ /^(.*)_after$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only
def_scope do |time|
@klass.where "#{column_sql(col)} > ?", time
end
when name =~ /^(.*)_between$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only
def_scope do |time1, time2|
@klass.where "#{column_sql(col)} >= ? AND #{column_sql(col)} <= ?", time1, time2
end
when @klass.has_lifecycle? && name.to_sym.in?(@klass::Lifecycle.state_names)
return true if check_only
if @klass::Lifecycle.state_names.length == 1
def_scope { @klass.scoped }
true
else
def_scope do
@klass.where "#{@klass.table_name}.#{@klass::Lifecycle.state_field} = ?", name
end
end
when name == "is"
return true if check_only
def_scope do |record|
@klass.where "#{@klass.table_name}.#{@klass.primary_key} = ?", record
end
when name == "is_not"
return true if check_only
def_scope do |record|
@klass.where "#{@klass.table_name}.#{@klass.primary_key} <> ?", record
end
when name == "by_most_recent"
return true if check_only
def_scope do
@klass.order "#{@klass.table_name}.created_at DESC"
end
when name == "recent"
return true if check_only
if "created_at".in?(@klass.columns.*.name)
def_scope do |*args|
count = args.first || 6
@klass.order("#{@klass.table_name}.created_at DESC").limit(count)
end
else
def_scope do |*args|
count = args.first || 6
limit(count)
end
end
when name == "order_by"
return true if check_only
klass = @klass
def_scope do |*args|
field, asc = args
field ||= ""
type = klass.attr_type(field)
if type.nil?
colspec = "#{field}"
elsif type.respond_to?(:name_attribute) && (name = type.name_attribute)
include = field
colspec = "#{type.table_name}.#{name}"
else
colspec = "#{klass.table_name}.#{field}"
end
@klass.includes(include).order("#{colspec} #{asc._?.upcase}")
end
when name == "include"
Rails.logger.warn "Automatic scope :include has been deprecated: use :includes instead."
return true if check_only
def_scope do |inclusions|
@klass.includes(inclusions)
end
when name == "search"
return true if check_only
def_scope do |query, *fields|
using_postgresql = %w(PostgreSQL PostGIS).include?(::ActiveRecord::Base.connection.adapter_name)
match_keyword = using_postgresql ? "ILIKE" : "LIKE"
words = (query || "").split
args = []
word_queries = words.map do |word|
field_query = '(' + fields.map { |field|
if using_postgresql
casted_field = "CAST(#{@klass.table_name}.#{field} AS TEXT)"
else
casted_field = "#{@klass.table_name}.#{field}"
end
field = "#{casted_field}" unless field =~ /\./
"(#{field} #{match_keyword} ?)"
}.join(" OR ") + ')'
args += ["%#{word}%"] * fields.length
field_query
end
@klass.where *([word_queries.join(" AND ")] + args)
end
else
matched_scope = false
end
matched_scope
end
|