Skip to content

Commit ddd28eb

Browse files
committed
Add include_for_find support via 'expand'
Allows for opt in of `:include_for_find` option for `Rbac::Filterer` via the `expand` request parameter. Include for find will effectively add a `.include` to the base query done in `collection_search`, which in cases like this: /api/vms?expand=resources,hardware&attributes=num_cpu,name Can avoid an N+1 on the associated resource.
1 parent e8db32c commit ddd28eb

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

app/controllers/api/base_controller/parameters.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ def sort_directive(klass, attr, order, options)
100100
end
101101
arel
102102
end
103+
104+
def determine_include_for_find(klass)
105+
return nil unless klass.respond_to?(:reflect_on_association)
106+
107+
relations = @req.derived_include_for_find.select do |relation|
108+
klass.reflect_on_association(relation)
109+
end
110+
111+
relations.empty? ? nil : relations
112+
end
103113
end
104114
end
105115
end

app/controllers/api/base_controller/renderer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def collection_filterer(res, type, klass, is_subcollection = false)
185185
options[:filter] = miq_expression if miq_expression
186186
options[:offset] = params['offset'] if params['offset']
187187
options[:limit] = params['limit'] if params['limit']
188+
options[:include_for_find] = determine_include_for_find(klass)
188189

189190
filter_results(miq_expression, res, options)
190191
end

lib/api/request_adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def attributes
3737
@attributes ||= @params['attributes'].to_s.split(',')
3838
end
3939

40+
def derived_include_for_find
41+
@derived_include_for_find ||= expand_requested.reject { |item| item == "resource" }
42+
end
43+
4044
def base
4145
url.partition(fullpath)[0] # http://target
4246
end

0 commit comments

Comments
 (0)