rest_framework 0.7.6 → 0.7.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/rest_framework/controller_mixins/models.rb +13 -1
- data/lib/rest_framework/filters.rb +2 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 829e2b04366870f6c4a7cad1c0bfdaf62c56c7085bdb52d028e1ec87c46b12ee
|
4
|
+
data.tar.gz: 739f019c64bdd79e9af321b3ccb8cd4db8c0ab13c8e7e01b116b78e9fe1a529a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64c8bf0286fbef571f76aeeb0679041f0844fccc3ae06aea1f1e09256ebbc973e073ffc47bca6cafa1b7ff4d72e955cfe44145dfe48aa0ba7ed56cbb089eed9c
|
7
|
+
data.tar.gz: 85d773111e1c1923b79181593ed963c8b5566dcb2ff35a71fa542809bf90616de7c8ff005d11d9acfd5ce78fcaa99096e4b5490beda9126712c5b6cbc588aa11
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.7
|
@@ -430,11 +430,23 @@ module RESTFramework::BaseModelControllerMixin
|
|
430
430
|
return @recordset = nil
|
431
431
|
end
|
432
432
|
|
433
|
+
# Get the recordset but with any associations included to avoid N+1 queries.
|
434
|
+
def get_recordset_with_includes
|
435
|
+
reflections = self.class.get_model.reflections.keys
|
436
|
+
associations = self.get_fields(fallback: true).select { |f| f.in?(reflections) }
|
437
|
+
|
438
|
+
if associations.any?
|
439
|
+
return self.get_recordset.includes(associations)
|
440
|
+
end
|
441
|
+
|
442
|
+
return self.get_recordset
|
443
|
+
end
|
444
|
+
|
433
445
|
# Get the records this controller has access to *after* any filtering is applied.
|
434
446
|
def get_records
|
435
447
|
return @records if instance_variable_defined?(:@records)
|
436
448
|
|
437
|
-
return @records = self.get_filtered_data(self.
|
449
|
+
return @records = self.get_filtered_data(self.get_recordset_with_includes)
|
438
450
|
end
|
439
451
|
|
440
452
|
# Get a single record by primary key or another column, if allowed. The return value is cached and
|
@@ -23,25 +23,18 @@ class RESTFramework::ModelFilter < RESTFramework::BaseFilter
|
|
23
23
|
def _get_filter_params
|
24
24
|
# Map filterset fields to strings because query parameter keys are strings.
|
25
25
|
fields = self._get_fields
|
26
|
-
@associations = []
|
27
26
|
|
28
27
|
return @controller.request.query_parameters.select { |p, _|
|
29
28
|
# Remove any trailing `__in` from the field name.
|
30
29
|
field = p.chomp("__in")
|
31
30
|
|
32
|
-
# Remove any associations whose sub-fields are not filterable.
|
33
|
-
# so the caller can include them.
|
31
|
+
# Remove any associations whose sub-fields are not filterable.
|
34
32
|
if match = /(.*)\.(.*)/.match(field)
|
35
33
|
field, sub_field = match[1..2]
|
36
34
|
next false unless field.in?(fields)
|
37
35
|
|
38
36
|
sub_fields = @controller.class.get_field_config(field)[:sub_fields]
|
39
|
-
|
40
|
-
@associations << field.to_sym
|
41
|
-
next true
|
42
|
-
end
|
43
|
-
|
44
|
-
next false
|
37
|
+
next sub_field.in?(sub_fields)
|
45
38
|
end
|
46
39
|
|
47
40
|
next field.in?(fields)
|
@@ -64,9 +57,6 @@ class RESTFramework::ModelFilter < RESTFramework::BaseFilter
|
|
64
57
|
# Filter data according to the request query parameters.
|
65
58
|
def get_filtered_data(data)
|
66
59
|
if filter_params = self._get_filter_params.presence
|
67
|
-
# Include any associations.
|
68
|
-
data = data.includes(*@associations) unless @associations.empty?
|
69
|
-
|
70
60
|
return data.where(**filter_params)
|
71
61
|
end
|
72
62
|
|
@@ -88,8 +78,6 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
|
|
88
78
|
def _get_ordering
|
89
79
|
return nil if @controller.class.ordering_query_param.blank?
|
90
80
|
|
91
|
-
@associations = []
|
92
|
-
|
93
81
|
# Ensure ordering_fields are strings since the split param will be strings.
|
94
82
|
fields = self._get_fields
|
95
83
|
order_string = @controller.params[@controller.class.ordering_query_param]
|
@@ -106,16 +94,6 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
|
|
106
94
|
end
|
107
95
|
next unless !fields || column.in?(fields)
|
108
96
|
|
109
|
-
# Populate any `@associations` so the caller can include them.
|
110
|
-
if match = /(.*)\.(.*)/.match(column)
|
111
|
-
association, sub_field = match[1..2]
|
112
|
-
@associations << association.to_sym
|
113
|
-
|
114
|
-
# Also, due to Rails weirdness, we need to convert the association name to the table name.
|
115
|
-
table_name = @controller.class.get_model.reflections[association].table_name
|
116
|
-
column = "#{table_name}.#{sub_field}"
|
117
|
-
end
|
118
|
-
|
119
97
|
ordering[column] = direction
|
120
98
|
end
|
121
99
|
return ordering
|
@@ -130,9 +108,6 @@ class RESTFramework::ModelOrderingFilter < RESTFramework::BaseFilter
|
|
130
108
|
reorder = !@controller.class.ordering_no_reorder
|
131
109
|
|
132
110
|
if ordering && !ordering.empty?
|
133
|
-
# Include any associations.
|
134
|
-
data = data.includes(*@associations) unless @associations.empty?
|
135
|
-
|
136
111
|
return data.send(reorder ? :reorder : :order, ordering)
|
137
112
|
end
|
138
113
|
|