Skip to content

Commit

Permalink
Optimize /org/admin/users/admin_index
Browse files Browse the repository at this point in the history
app/controllers/users_controller.rb
- Both in the super_user case as well as the non-super_user case, Bullet warnings are addressed by modifying the .includes() args

app/views/paginable/users/_index.html.erb
- This commit removes `presenter = IdentifierPresenter.new(identifiable: user)` and instead accesses `identifier_scheme.name` via the `user` variable. user.identifiers and user.identifiers[i].identifier_scheme are now being eager loaded, whereas IdentifierPresenter was loading IdentifierScheme.for_users for each user.
- Analyzing the code below, we can see how there is no difference between `presenter` and `user` when acccessing `.identifiers` and `.identifiers[i].identifier_scheme`

```
# app/views/paginable/users/_index.html.erb
<% presenter = IdentifierPresenter.new(identifiable: user) %>
<% presenter.identifiers.each do |identifier| %>
  <p><%= identifier.identifier_scheme.name %></p>

# app/presenters/identifier_presenter.rb
class IdentifierPresenter
  attr_reader :schemes, :identifiable

  def initialize(identifiable:)
    @Identifiable = identifiable

    @schemes = load_schemes
  end

  def identifiers
    @identifiable.identifiers
  end
```
  • Loading branch information
aaronskiba committed Jun 6, 2024
1 parent 76f14fe commit 70f08fa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def admin_index
@filter_admin = false

@users = if current_user.can_super_admin?
User.includes(:department, :org, :perms, :roles, :identifiers).page(1)
User.includes(:department, :org, :perms, :roles, identifiers: :identifier_scheme).page(1)
else
current_user.org.users
.includes(:department, :org, :perms, :roles, :identifiers)
.includes(:department, :perms, :roles, identifiers: :identifier_scheme)
.page(1)
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/views/paginable/users/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@
<% end %>
</td>
<td class="text-center">
<% presenter = IdentifierPresenter.new(identifiable: user) %>
<% presenter.identifiers.each do |identifier| %>
<% user.identifiers.each do |identifier| %>
<p><%= identifier.identifier_scheme.name %></p>
<% end %>
</td>
Expand Down

0 comments on commit 70f08fa

Please sign in to comment.