Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise and Refactor Org.with_template_and_user_counts #3431

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/paginable/orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
authorize(Org)
paginable_renderise(
partial: 'index',
scope: Org.with_template_and_user_counts,
scope: Org.with_template_count_and_associations_check,
query_params: { sort_field: 'orgs.name', sort_direction: :asc },
format: :json
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/super_admin/orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OrgsController < ApplicationController
def index
authorize Org
render 'index', locals: {
orgs: Org.with_template_and_user_counts.page(1)
orgs: Org.with_template_count_and_associations_check.page(1)
}
end

Expand Down
9 changes: 5 additions & 4 deletions app/models/org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ def self.default_orgs
}

# Scope used in several controllers
scope :with_template_and_user_counts, lambda {
joins('LEFT OUTER JOIN templates ON orgs.id = templates.org_id')
.joins('LEFT OUTER JOIN users ON orgs.id = users.org_id')
scope :with_template_count_and_associations_check, lambda {
left_outer_joins(:templates)
.group('orgs.id')
.select("orgs.*,
count(distinct templates.family_id) as template_count,
count(users.id) as user_count")
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.user_count > 0 || org.template_count > 0 || org.contributors.length > 0 || org.plans.length > 0 %>
<% 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
Loading