Skip to content

Commit 9424917

Browse files
committed
Fix N+1s in OrganizationController#show
1 parent ac8a209 commit 9424917

6 files changed

Lines changed: 14 additions & 10 deletions

File tree

app/controllers/admin/organizations_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ 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+
@users = @organization.users.with_discarded.includes(:roles, :organization).alphabetized
7173
end
7274

7375
def destroy

app/controllers/organizations_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
@users = @organization.users.with_discarded.includes(:roles, :organization).alphabetized
911
end
1012

1113
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+
<%= @default_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>

0 commit comments

Comments
 (0)