Skip to content

Commit 1a504a2

Browse files
authored
Merge pull request #5043 from coalest/fix-organization-show-n-plus-ones
Fix N+1s in OrganizationController#show
2 parents 87d5428 + 7cd1b96 commit 1a504a2

8 files changed

Lines changed: 40 additions & 10 deletions

File tree

app/controllers/admin/organizations_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def create
6868
def show
6969
@organization = Organization.find(params[:id])
7070
@header_link = admin_dashboard_path
71+
@default_storage_location = StorageLocation.find_by(id: @organization.default_storage_location) if @organization.default_storage_location
72+
@intake_storage_location = StorageLocation.find_by(id: @organization.storage_locations) if @organization.intake_location
73+
@users = @organization.users.with_discarded.includes(:roles, :organization).alphabetized
7174
end
7275

7376
def destroy

app/controllers/organizations_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class OrganizationsController < ApplicationController
66
def show
77
@organization = current_organization
88
@header_link = dashboard_path
9+
@default_storage_location = StorageLocation.find_by(id: @organization.default_storage_location) if @organization.default_storage_location
10+
@intake_storage_location = StorageLocation.find_by(id: @organization.intake_location) if @organization.intake_location
11+
@users = @organization.users.with_discarded.includes(:roles, :organization).alphabetized
912
end
1013

1114
def edit

app/models/user.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ def invitation_status
112112
end
113113

114114
def kind
115-
return "super" if has_role?(Role::SUPER_ADMIN)
116-
return "admin" if has_role?(Role::ORG_ADMIN, organization)
117-
return "normal" if has_role?(Role::ORG_USER, organization)
118-
return "partner" if has_role?(Role::PARTNER, partner)
115+
return "super" if has_cached_role?(Role::SUPER_ADMIN)
116+
return "admin" if has_cached_role?(Role::ORG_ADMIN, organization)
117+
return "normal" if has_cached_role?(Role::ORG_USER, organization)
118+
return "partner" if has_cached_role?(Role::PARTNER, partner)
119119

120120
"normal"
121121
end
122122

123123
def is_admin?(org)
124-
has_role?(Role::ORG_ADMIN, org) || has_role?(Role::SUPER_ADMIN)
124+
has_cached_role?(Role::ORG_ADMIN, org) || has_cached_role?(Role::SUPER_ADMIN)
125125
end
126126

127127
def switchable_roles

app/views/organizations/_details.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<h3 class='font-bold'>Default Intake Location</h3>
8181
<p>
8282
<%= fa_icon "building" %>
83-
<%= StorageLocation.find_by(id: @organization.intake_location)&.name || "Not defined" %>
83+
<%= @intake_storage_location&.name || "Not defined" %>
8484
</p>
8585
</div>
8686
<div class="mb-4">
@@ -102,7 +102,7 @@
102102
<h3 class='font-bold'>Default Storage Location</h3>
103103
<p>
104104
<%= fa_icon "building-o" %>
105-
<%= StorageLocation.find_by(id: @organization.default_storage_location)&.name || "Not defined" %>
105+
<%= @default_storage_location&.name || "Not defined" %>
106106
</p>
107107
</div>
108108
<div class="mb-4">

app/views/users/_organization_user.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<td><%= user.invitation_status %></td>
99
<td nowrap><%= reinvite_user_link(user) %></td>
1010
<td>
11-
<% unless user.has_role?(Role::SUPER_ADMIN) || user.has_role?(Role::ORG_ADMIN, current_organization) %>
11+
<% unless user.has_cached_role?(Role::SUPER_ADMIN) || user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
1212
<div class="dropdown">
1313
<button class="btn btn-default dropdown-toggle" type="button" id="<%= dom_id(user, "dropdownMenu") %>" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
1414
Actions
@@ -33,7 +33,7 @@
3333
</ul>
3434
</div>
3535
<% end %>
36-
<% if current_user.is_admin?(current_organization) && user.has_role?(Role::ORG_ADMIN, current_organization) %>
36+
<% if current_user.is_admin?(current_organization) && user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
3737
<%= edit_button_to demote_to_user_organization_path(user_id: user.id, organization_name: current_organization.short_name),
3838
{text: 'Demote to User'},
3939
{method: :post, rel: "nofollow", data: {confirm: 'This will demote the admin to user status. Are you sure that you want to submit this?', size: 'xs'}} unless user.id == current_user.id %>

app/views/users/_organization_users_table.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<tbody>
2626
<%= render partial: "/users/organization_user",
27-
collection: @organization.users.with_discarded.alphabetized,
27+
collection: @users,
2828
as: :user,
2929
locals: { current_organization: current_organization || @organization } %>
3030
</tbody>

spec/requests/admin/organizations_requests_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@
166166
expect(response).to be_successful
167167
end
168168

169+
it "displays the correct organization details" do
170+
intake_storage_location = create(:storage_location, organization:, name: "Intake Center")
171+
default_storage_location = create(:storage_location, organization:, name: "Default Center")
172+
173+
organization.update!(intake_location: intake_storage_location.id, default_storage_location: default_storage_location.id)
174+
175+
get admin_organization_path({ id: organization.id })
176+
177+
expect(response.body).to include("Intake Center")
178+
expect(response.body).to include("Default Center")
179+
end
180+
169181
context "with an organization user" do
170182
let!(:user) { create(:user, organization: organization) }
171183

spec/requests/organization_requests_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747
expect(html.text).to include("Receive email when partner makes a request:")
4848
end
4949

50+
it "displays the correct organization details" do
51+
intake_storage_location = create(:storage_location, organization:, name: "Intake Center")
52+
default_storage_location = create(:storage_location, organization:, name: "Default Center")
53+
54+
organization.update!(intake_location: intake_storage_location.id, default_storage_location: default_storage_location.id)
55+
56+
get organization_path
57+
58+
expect(response.body).to include("Intake Center")
59+
expect(response.body).to include("Default Center")
60+
end
61+
5062
context "when enable_packs flipper is on" do
5163
it "displays organization's custom units" do
5264
Flipper.enable(:enable_packs)

0 commit comments

Comments
 (0)