Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 9b8083e

Browse files
Aakash ShahsethboylesJaskanwal Pawar
committed
Eager load associations for v3/roles list
Co-authored-by: Seth Boyles <sboyles@pivotal.io> Co-authored-by: Aakash Shah <ashah@pivotal.io> Co-authored-by: Jaskanwal Pawar <jpawar@pivotal.io>
1 parent 94a57c5 commit 9b8083e

6 files changed

Lines changed: 41 additions & 6 deletions

File tree

app/controllers/v3/roles_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def index
2929
unprocessable!(message.errors.full_messages) unless message.valid?
3030

3131
RoleGuidPopulate.populate
32-
roles = RoleListFetcher.fetch(message, readable_roles)
32+
roles = RoleListFetcher.fetch(message, readable_roles, eager_loaded_associations: Presenters::V3::RolePresenter.associated_resources)
3333

3434
decorators = []
3535
decorators << IncludeRoleUserDecorator if IncludeRoleUserDecorator.match?(message.include)

app/fetchers/role_list_fetcher.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
module VCAP::CloudController
55
class RoleListFetcher
66
class << self
7-
def fetch(message, readable_users_dataset)
8-
filter(message, readable_users_dataset)
7+
def fetch(message, readable_users_dataset, eager_loaded_associations: [])
8+
filter(message, readable_users_dataset).eager(eager_loaded_associations)
99
end
1010

1111
private

app/models/runtime/role.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ class Role < Sequel::Model(
8181
).from_self
8282
)
8383

84+
many_to_one :user, key: :user_id
85+
many_to_one :organization, key: :organization_id
86+
many_to_one :space, key: :space_id
87+
8488
def user_guid
85-
User.first(id: user_id).guid
89+
user.guid
8690
end
8791

8892
def organization_guid
89-
Organization.first(id: organization_id)&.guid
93+
organization&.guid
9094
end
9195

9296
def space_guid
93-
Space.first(id: space_id)&.guid
97+
space&.guid
9498
end
9599

96100
def for_space?

app/presenters/v3/role_presenter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
module VCAP::CloudController::Presenters::V3
44
class RolePresenter < BasePresenter
5+
class << self
6+
def associated_resources
7+
[:user, :space, :organization]
8+
end
9+
end
10+
511
def to_hash
612
hash = {
713
guid: role.guid,

spec/request/roles_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,19 @@ def make_space_role_for_current_user(type)
948948
end
949949
end
950950

951+
describe 'eager loading' do
952+
it 'eager loads associated resources that the presenter specifies' do
953+
expect(VCAP::CloudController::RoleListFetcher).to receive(:fetch).with(
954+
anything,
955+
anything,
956+
hash_including(eager_loaded_associations: [:user, :space, :organization])
957+
).and_call_original
958+
959+
get '/v3/roles', nil, admin_header
960+
expect(last_response).to have_status_code(200)
961+
end
962+
end
963+
951964
context 'listing all roles' do
952965
let(:expected_codes_and_responses) do
953966
h = Hash.new(code: 200, response_objects: [space_auditor_response_object, org_auditor_response_object])

spec/unit/fetchers/role_list_fetcher_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ module VCAP::CloudController
2020

2121
let(:message) { RolesListMessage.from_params(filters) }
2222

23+
context 'eager loading associated resources' do
24+
let(:filters) { {} }
25+
26+
it 'eager loads the specified resources for the routes' do
27+
results = RoleListFetcher.fetch(message, Role.dataset, eager_loaded_associations: [:user, :space]).all
28+
29+
expect(results.first.associations.key?(:user)).to be true
30+
expect(results.first.associations.key?(:space)).to be true
31+
expect(results.first.associations.key?(:organization)).to be false
32+
end
33+
end
34+
2335
context 'when no filters are specified' do
2436
let(:filters) { {} }
2537

0 commit comments

Comments
 (0)