Skip to content

Commit

Permalink
Optimize Org.with_template_and_user_counts (2/2)
Browse files Browse the repository at this point in the history
- This is a small addition to commit 6708697.
- Rather than using all of the virtual attributes (org.has_users, org.has_contributors, org.has_plans), we 'combine' them into org.has_associations.
- Basically, has_associations == (org.has_users || org.has_contributors || org.has_plans)
- The corresponding changes are also made in app/views/paginable/orgs/_index.html.erb
  • Loading branch information
aaronskiba committed Jun 25, 2024
1 parent 6708697 commit d335d4d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/models/org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ def self.default_orgs
.group('orgs.id')
.select("orgs.*,
count(distinct templates.family_id) as template_count,
EXISTS (SELECT 1 FROM users WHERE users.org_id = orgs.id) AS has_users,
EXISTS (SELECT 1 FROM contributors WHERE contributors.org_id = orgs.id) AS has_contributors,
EXISTS (SELECT 1 FROM plans WHERE plans.org_id = orgs.id) as has_plans")
EXISTS (SELECT 1 FROM users WHERE users.org_id = orgs.id) OR
EXISTS (SELECT 1 FROM contributors WHERE contributors.org_id = orgs.id) OR
EXISTS (SELECT 1 FROM plans WHERE plans.org_id = orgs.id) as has_associations")
}

# EVALUATE CLASS AND INSTANCE METHODS BELOW
Expand Down
2 changes: 1 addition & 1 deletion app/views/paginable/orgs/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="org-<%= org.id %>-actions">
<li class="nav-item"><%= link_to _('Edit'), admin_edit_org_path(org), class:'dropdown-item px-3' %></li>
<% unless org.has_users || org.template_count > 0 || org.has_contributors || org.has_plans %>
<% unless org.template_count > 0 || org.has_associations? %>
<li class="nav-item"><%= link_to _('Remove'), super_admin_org_path(org), data: {confirm: _("You are about to delete '%{org_name}'. Are you sure?") % { org_name: org.name}}, method: :delete, class:'dropdown-item px-3' %></li>
<% end %>
</ul>
Expand Down

0 comments on commit d335d4d

Please sign in to comment.