Class: Graphiti::Adapters::ActiveRecord
Defined Under Namespace
Modules: Inference
Classes: BelongsToSideload, HasManySideload, HasOneSideload, ManyToManySideload, Sanitizer
Instance Attribute Summary
Attributes inherited from Abstract
#resource
Class Method Summary
collapse
Instance Method Summary
collapse
-
#associate(parent, child, association_name, association_type) ⇒ Object
-
#associate_all(parent, children, association_name, association_type) ⇒ Object
-
#average(scope, attr) ⇒ Float
The average of the scope.
-
#base_scope(model) ⇒ Object
-
#can_group? ⇒ Boolean
-
#close ⇒ Object
-
#count(scope, attr) ⇒ Numeric
-
#create(model_class, create_params) ⇒ Object
-
#destroy(model_instance) ⇒ Object
-
#disassociate(parent, child, association_name, association_type) ⇒ Object
When a has_and_belongs_to_many relationship, we don't have a foreign key that can be null'd.
-
#filter_datetime_eq(scope, attribute, value, is_not: false) ⇒ Object
Ensure fractional seconds don't matter.
-
#filter_datetime_lte(scope, attribute, value) ⇒ Object
-
#filter_datetime_not_eq(scope, attribute, value) ⇒ Object
-
#filter_eq(scope, attribute, value) ⇒ Object
(also: #filter_integer_eq, #filter_float_eq, #filter_big_decimal_eq, #filter_date_eq, #filter_boolean_eq, #filter_uuid_eq, #filter_enum_eq, #filter_enum_eql)
-
#filter_gt(scope, attribute, value) ⇒ Object
(also: #filter_integer_gt, #filter_float_gt, #filter_big_decimal_gt, #filter_datetime_gt, #filter_date_gt)
-
#filter_gte(scope, attribute, value) ⇒ Object
(also: #filter_integer_gte, #filter_float_gte, #filter_big_decimal_gte, #filter_datetime_gte, #filter_date_gte)
-
#filter_lt(scope, attribute, value) ⇒ Object
(also: #filter_integer_lt, #filter_float_lt, #filter_big_decimal_lt, #filter_datetime_lt, #filter_date_lt)
-
#filter_lte(scope, attribute, value) ⇒ Object
(also: #filter_integer_lte, #filter_float_lte, #filter_big_decimal_lte, #filter_date_lte)
-
#filter_not_eq(scope, attribute, value) ⇒ Object
(also: #filter_integer_not_eq, #filter_float_not_eq, #filter_big_decimal_not_eq, #filter_date_not_eq, #filter_boolean_not_eq, #filter_uuid_not_eq, #filter_enum_not_eq, #filter_enum_not_eql)
-
#filter_string_eq(scope, attribute, value, is_not: false) ⇒ Object
-
#filter_string_eql(scope, attribute, value, is_not: false) ⇒ Object
-
#filter_string_match(scope, attribute, value, is_not: false) ⇒ Object
-
#filter_string_not_eq(scope, attribute, value) ⇒ Object
-
#filter_string_not_eql(scope, attribute, value) ⇒ Object
-
#filter_string_not_match(scope, attribute, value) ⇒ Object
-
#filter_string_not_prefix(scope, attribute, value) ⇒ Object
-
#filter_string_not_suffix(scope, attribute, value) ⇒ Object
-
#filter_string_prefix(scope, attribute, value, is_not: false) ⇒ Object
-
#filter_string_suffix(scope, attribute, value, is_not: false) ⇒ Object
-
#group(scope, attribute) ⇒ Object
-
#maximum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#minimum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#order(scope, attribute, direction) ⇒ Object
-
#paginate(scope, current_page, per_page, offset) ⇒ Object
-
#resolve(scope) ⇒ Object
-
#save(model_instance) ⇒ Object
-
#sum(scope, attr) ⇒ Numeric
-
#transaction(model_class) ⇒ Object
Run this write request within an ActiveRecord transaction.
-
#update(model_class, update_params) ⇒ Object
Methods inherited from Abstract
#assign_attributes, #belongs_to_many_filter, #build, default_operators, #initialize, numerical_operators, #persistence_attributes
#process_belongs_to, #process_has_many, #update_foreign_key, #update_foreign_key_for_parents, #update_foreign_type
Instance Method Details
#associate(parent, child, association_name, association_type) ⇒ Object
264
265
266
267
268
269
270
271
272
|
# File 'lib/graphiti/adapters/active_record.rb', line 264
def associate(parent, child, association_name, association_type)
if activerecord_associate?(parent, child, association_name)
association = parent.association(association_name)
association.loaded!
association.instance_variable_set(:@target, child)
else
super
end
end
|
#associate_all(parent, children, association_name, association_type) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/graphiti/adapters/active_record.rb', line 242
def associate_all(parent, children, association_name, association_type)
if activerecord_associate?(parent, children[0], association_name)
association = parent.association(association_name)
association.loaded!
children.each do |child|
if association_type == :many_to_many &&
[:create, :update].include?(Graphiti.context[:namespace]) &&
!parent.send(association_name).exists?(child.id) &&
child.errors.blank?
parent.send(association_name) << child
else
target = association.instance_variable_get(:@target)
target |= [child]
association.instance_variable_set(:@target, target)
end
end
else
super
end
end
|
#average(scope, attr) ⇒ Float
Returns the average of the scope.
208
209
210
|
# File 'lib/graphiti/adapters/active_record.rb', line 208
def average(scope, attr)
scope.average(attr).to_f
end
|
#base_scope(model) ⇒ Object
181
182
183
|
# File 'lib/graphiti/adapters/active_record.rb', line 181
def base_scope(model)
model.all
end
|
#can_group? ⇒ Boolean
316
317
318
|
# File 'lib/graphiti/adapters/active_record.rb', line 316
def can_group?
true
end
|
308
309
310
311
312
313
314
|
# File 'lib/graphiti/adapters/active_record.rb', line 308
def close
if ::ActiveRecord.version > Gem::Version.new("7.1")
::ActiveRecord::Base.connection_handler.clear_active_connections!
else
::ActiveRecord::Base.clear_active_connections!
end
end
|
#count(scope, attr) ⇒ Numeric
Returns the count of the scope.
199
200
201
202
203
204
205
|
# File 'lib/graphiti/adapters/active_record.rb', line 199
def count(scope, attr)
if attr.to_sym == :total
scope.distinct.count(:all)
else
scope.distinct.count(attr)
end
end
|
#create(model_class, create_params) ⇒ Object
285
286
287
288
289
|
# File 'lib/graphiti/adapters/active_record.rb', line 285
def create(model_class, create_params)
instance = model_class.new(create_params)
instance.save
instance
end
|
#destroy(model_instance) ⇒ Object
303
304
305
306
|
# File 'lib/graphiti/adapters/active_record.rb', line 303
def destroy(model_instance)
model_instance.destroy
model_instance
end
|
#disassociate(parent, child, association_name, association_type) ⇒ Object
When a has_and_belongs_to_many relationship, we don't have a foreign
key that can be null'd. Instead, go through the ActiveRecord API.
277
278
279
280
281
282
|
# File 'lib/graphiti/adapters/active_record.rb', line 277
def disassociate(parent, child, association_name, association_type)
if association_type == :many_to_many
parent.send(association_name).delete(child)
end
end
|
#filter_datetime_eq(scope, attribute, value, is_not: false) ⇒ Object
Ensure fractional seconds don't matter
165
166
167
168
169
|
# File 'lib/graphiti/adapters/active_record.rb', line 165
def filter_datetime_eq(scope, attribute, value, is_not: false)
ranges = value.map { |v| (v..v + 1.second - 0.00000001) unless v.nil? }
clause = {attribute => ranges}
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#filter_datetime_lte(scope, attribute, value) ⇒ Object
175
176
177
178
179
|
# File 'lib/graphiti/adapters/active_record.rb', line 175
def filter_datetime_lte(scope, attribute, value)
value = value.map { |v| v + 1.second - 0.00000001 }
column = scope.klass.arel_table[attribute]
scope.where(column.lteq_any(value))
end
|
#filter_datetime_not_eq(scope, attribute, value) ⇒ Object
171
172
173
|
# File 'lib/graphiti/adapters/active_record.rb', line 171
def filter_datetime_not_eq(scope, attribute, value)
filter_datetime_eq(scope, attribute, value, is_not: true)
end
|
#filter_eq(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_eq, filter_float_eq, filter_big_decimal_eq, filter_date_eq, filter_boolean_eq, filter_uuid_eq, filter_enum_eq, filter_enum_eql
19
20
21
|
# File 'lib/graphiti/adapters/active_record.rb', line 19
def filter_eq(scope, attribute, value)
scope.where(attribute => value)
end
|
#filter_gt(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_gt, filter_float_gt, filter_big_decimal_gt, filter_datetime_gt, filter_date_gt
125
126
127
128
|
# File 'lib/graphiti/adapters/active_record.rb', line 125
def filter_gt(scope, attribute, value)
column = column_for(scope, attribute)
scope.where(column.gt_any(value))
end
|
#filter_gte(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_gte, filter_float_gte, filter_big_decimal_gte, filter_datetime_gte, filter_date_gte
135
136
137
138
|
# File 'lib/graphiti/adapters/active_record.rb', line 135
def filter_gte(scope, attribute, value)
column = column_for(scope, attribute)
scope.where(column.gteq_any(value))
end
|
#filter_lt(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_lt, filter_float_lt, filter_big_decimal_lt, filter_datetime_lt, filter_date_lt
145
146
147
148
|
# File 'lib/graphiti/adapters/active_record.rb', line 145
def filter_lt(scope, attribute, value)
column = column_for(scope, attribute)
scope.where(column.lt_any(value))
end
|
#filter_lte(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_lte, filter_float_lte, filter_big_decimal_lte, filter_date_lte
155
156
157
158
|
# File 'lib/graphiti/adapters/active_record.rb', line 155
def filter_lte(scope, attribute, value)
column = column_for(scope, attribute)
scope.where(column.lteq_any(value))
end
|
#filter_not_eq(scope, attribute, value) ⇒ Object
Also known as:
filter_integer_not_eq, filter_float_not_eq, filter_big_decimal_not_eq, filter_date_not_eq, filter_boolean_not_eq, filter_uuid_not_eq, filter_enum_not_eq, filter_enum_not_eql
31
32
33
|
# File 'lib/graphiti/adapters/active_record.rb', line 31
def filter_not_eq(scope, attribute, value)
scope.where.not(attribute => value)
end
|
#filter_string_eq(scope, attribute, value, is_not: false) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/graphiti/adapters/active_record.rb', line 43
def filter_string_eq(scope, attribute, value, is_not: false)
return filter_string_eql(scope, attribute, nil, is_not: is_not) if Array(value).compact.blank?
column = column_for(scope, attribute)
clause = column.lower.eq_any(value.map(&:downcase))
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#filter_string_eql(scope, attribute, value, is_not: false) ⇒ Object
51
52
53
54
|
# File 'lib/graphiti/adapters/active_record.rb', line 51
def filter_string_eql(scope, attribute, value, is_not: false)
clause = {attribute => value.presence}
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#filter_string_match(scope, attribute, value, is_not: false) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/graphiti/adapters/active_record.rb', line 68
def filter_string_match(scope, attribute, value, is_not: false)
clause = sanitized_like_for(scope, attribute, value) { |v|
"%#{v}%"
}
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#filter_string_not_eq(scope, attribute, value) ⇒ Object
56
57
58
|
# File 'lib/graphiti/adapters/active_record.rb', line 56
def filter_string_not_eq(scope, attribute, value)
filter_string_eq(scope, attribute, value.presence, is_not: true)
end
|
#filter_string_not_eql(scope, attribute, value) ⇒ Object
60
61
62
|
# File 'lib/graphiti/adapters/active_record.rb', line 60
def filter_string_not_eql(scope, attribute, value)
filter_string_eql(scope, attribute, value.presence, is_not: true)
end
|
#filter_string_not_match(scope, attribute, value) ⇒ Object
121
122
123
|
# File 'lib/graphiti/adapters/active_record.rb', line 121
def filter_string_not_match(scope, attribute, value)
filter_string_match(scope, attribute, value, is_not: true)
end
|
#filter_string_not_prefix(scope, attribute, value) ⇒ Object
113
114
115
|
# File 'lib/graphiti/adapters/active_record.rb', line 113
def filter_string_not_prefix(scope, attribute, value)
filter_string_prefix(scope, attribute, value, is_not: true)
end
|
#filter_string_not_suffix(scope, attribute, value) ⇒ Object
117
118
119
|
# File 'lib/graphiti/adapters/active_record.rb', line 117
def filter_string_not_suffix(scope, attribute, value)
filter_string_suffix(scope, attribute, value, is_not: true)
end
|
#filter_string_prefix(scope, attribute, value, is_not: false) ⇒ Object
75
76
77
78
79
80
|
# File 'lib/graphiti/adapters/active_record.rb', line 75
def filter_string_prefix(scope, attribute, value, is_not: false)
clause = sanitized_like_for(scope, attribute, value) { |v|
"#{v}%"
}
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#filter_string_suffix(scope, attribute, value, is_not: false) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/graphiti/adapters/active_record.rb', line 82
def filter_string_suffix(scope, attribute, value, is_not: false)
clause = sanitized_like_for(scope, attribute, value) { |v|
"%#{v}"
}
is_not ? scope.where.not(clause) : scope.where(clause)
end
|
#group(scope, attribute) ⇒ Object
320
321
322
|
# File 'lib/graphiti/adapters/active_record.rb', line 320
def group(scope, attribute)
scope.group(attribute)
end
|
#maximum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope.
218
219
220
|
# File 'lib/graphiti/adapters/active_record.rb', line 218
def maximum(scope, attr)
scope.maximum(attr)
end
|
#minimum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope.
223
224
225
|
# File 'lib/graphiti/adapters/active_record.rb', line 223
def minimum(scope, attr)
scope.minimum(attr)
end
|
#order(scope, attribute, direction) ⇒ Object
186
187
188
|
# File 'lib/graphiti/adapters/active_record.rb', line 186
def order(scope, attribute, direction)
scope.order(attribute => direction)
end
|
#paginate(scope, current_page, per_page, offset) ⇒ Object
191
192
193
194
195
196
|
# File 'lib/graphiti/adapters/active_record.rb', line 191
def paginate(scope, current_page, per_page, offset)
scope = scope.page(current_page) if current_page
scope = scope.per(per_page) if per_page
scope = scope.padding(offset) if offset
scope
end
|
#resolve(scope) ⇒ Object
Resolve the scope. This is where you'd actually fire SQL,
actually make an HTTP call, etc.
228
229
230
|
# File 'lib/graphiti/adapters/active_record.rb', line 228
def resolve(scope)
scope.to_a
end
|
#save(model_instance) ⇒ Object
298
299
300
301
|
# File 'lib/graphiti/adapters/active_record.rb', line 298
def save(model_instance)
model_instance.save
model_instance
end
|
#sum(scope, attr) ⇒ Numeric
Returns the sum of the scope.
213
214
215
|
# File 'lib/graphiti/adapters/active_record.rb', line 213
def sum(scope, attr)
scope.sum(attr)
end
|
#transaction(model_class) ⇒ Object
Run this write request within an ActiveRecord transaction
236
237
238
239
240
|
# File 'lib/graphiti/adapters/active_record.rb', line 236
def transaction(model_class)
model_class.transaction do
yield
end
end
|
#update(model_class, update_params) ⇒ Object
292
293
294
295
296
|
# File 'lib/graphiti/adapters/active_record.rb', line 292
def update(model_class, update_params)
instance = model_class.find(update_params.only(:id))
instance.update_attributes(update_params.except(:id))
instance
end
|