rest_framework 0.7.6 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edeb7963dbca0185ff384e628a185c83341e3154d6414c9d8f70a05115abd23a
4
- data.tar.gz: 8d6bc94fea07e57c20625c8aa192ced88789e078bf3a4b29b71e6c80099f2436
3
+ metadata.gz: 829e2b04366870f6c4a7cad1c0bfdaf62c56c7085bdb52d028e1ec87c46b12ee
4
+ data.tar.gz: 739f019c64bdd79e9af321b3ccb8cd4db8c0ab13c8e7e01b116b78e9fe1a529a
5
5
  SHA512:
6
- metadata.gz: 68ecf8a8a045c3e217c597f86b46573dc80bb931e20be45af01b939079f8cf2dd27d68e8bebd8d458726b04b8b55fb2dd65a60bcf4c847c30feb1257f7cfb9ea
7
- data.tar.gz: 388478f7c8f2c51a96740f25043aa7d188b5e59731478f4bca2ce6171fadc5ba1e201b12ee051859386ce151c1114b9b6f207dbe840e732dec652d9639323825
6
+ metadata.gz: 64c8bf0286fbef571f76aeeb0679041f0844fccc3ae06aea1f1e09256ebbc973e073ffc47bca6cafa1b7ff4d72e955cfe44145dfe48aa0ba7ed56cbb089eed9c
7
+ data.tar.gz: 85d773111e1c1923b79181593ed963c8b5566dcb2ff35a71fa542809bf90616de7c8ff005d11d9acfd5ce78fcaa99096e4b5490beda9126712c5b6cbc588aa11
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.6
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.get_recordset)
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. Also populate `@associations`
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
- if sub_field.in?(sub_fields)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory N. Schmit