Skip to content

Commit

Permalink
Merge pull request #15387 from jntullo/bz/fix_virtual_attr_expansion
Browse files Browse the repository at this point in the history
Fix virtual attribute selection
(cherry picked from commit e92afb1)

https://bugzilla.redhat.com/show_bug.cgi?id=1468294
  • Loading branch information
abellotti authored and simaishi committed Jul 6, 2017
1 parent 813b764 commit d4fdfeb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def virtual_attribute_search(resource, attribute)
if resource.class < ApplicationRecord
# is relation in 'attribute' variable plural in the model class (from 'resource.class') ?
if [:has_many, :has_and_belongs_to_many].include?(resource.class.reflection_with_virtual(attribute).try(:macro))
Rbac.filtered(resource.public_send(attribute))
resource_attr = resource.public_send(attribute)
return resource_attr unless resource_attr.try(:first).kind_of?(ApplicationRecord)
Rbac.filtered(resource_attr)
else
Rbac.filtered_object(resource).try(:public_send, attribute)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/api/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ def expect_svc_with_vms
expect_result_resources_to_include_hrefs("resources", @svc1_vm_list)
end

it "supports expansion of virtual attributes" do
run_get services_url, :expand => "resources", :attributes => "power_states"

expected = {
"resources" => [
a_hash_including("power_states" => svc1.power_states)
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it "can query vms as subcollection via expand" do
run_get services_url(svc1.id), :expand => "vms"

Expand Down

0 comments on commit d4fdfeb

Please sign in to comment.