From 4713a618a36bd45ff6ac515d18366b3e7dcf1fb9 Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Thu, 15 Jun 2017 14:28:59 -0400 Subject: [PATCH] only filter active record objects --- app/controllers/api/base_controller/renderer.rb | 4 +++- spec/requests/api/services_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/base_controller/renderer.rb b/app/controllers/api/base_controller/renderer.rb index c779ee59eb5..3943d51f4a4 100644 --- a/app/controllers/api/base_controller/renderer.rb +++ b/app/controllers/api/base_controller/renderer.rb @@ -166,7 +166,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 diff --git a/spec/requests/api/services_spec.rb b/spec/requests/api/services_spec.rb index 76afc80013e..b8bb9e79ec3 100644 --- a/spec/requests/api/services_spec.rb +++ b/spec/requests/api/services_spec.rb @@ -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"