Skip to content

Commit

Permalink
[#148491] Improve performance of FacilitiesController#show (Part 2) (#…
Browse files Browse the repository at this point in the history
…1949)

* Avoid always-executing COUNT(*) query when we don’t need it

* Load all non-instruments in a single query for the facility home page

* Gracefully handle facilities without bundles on FacilitiesController#show
  • Loading branch information
vandrijevik authored May 10, 2019
1 parent 62c1432 commit 3212929
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
11 changes: 3 additions & 8 deletions app/controllers/facilities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ def show

if acting_as? || session_user.try(:operator_of?, current_facility)
@instruments = instruments_scope.not_archived.alphabetized
@items = current_facility.items.not_archived.alphabetized
@services = current_facility.services.not_archived.alphabetized
@timed_services = current_facility.timed_services.not_archived.alphabetized
@bundles = current_facility.bundles.not_archived.alphabetized
@non_instrument_products_by_type = current_facility.non_instrument_products.not_archived.group_by(&:type)
else
@instruments = instruments_scope.active.alphabetized
@items = current_facility.items.active.alphabetized
@services = current_facility.services.active.alphabetized
@timed_services = current_facility.timed_services.active.alphabetized
@bundles = current_facility.bundles.active.alphabetized.reject{|b| !b.products_active?}
@non_instrument_products_by_type = current_facility.non_instrument_products.active.group_by(&:type)
@non_instrument_products_by_type["Bundle"].try(:select!, &:products_active?)
end

render layout: "application"
Expand Down
1 change: 1 addition & 0 deletions app/models/facility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Facility < ApplicationRecord
has_many :instruments, inverse_of: :facility
has_many :items, inverse_of: :facility
has_many :journals
has_many :non_instrument_products, -> { where.not(type: "Instrument").alphabetized }, class_name: "Product", inverse_of: :facility
has_many :order_details, through: :products
has_many :order_imports, dependent: :destroy
has_many :orders, -> { where.not(ordered_at: nil) }
Expand Down
2 changes: 1 addition & 1 deletion app/views/facilities/_product_list.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- if products.any?
- if products.present?
.product_list{ class: @columns }
%h3= product_list_title products, local_assigns[:title_extra]

Expand Down
12 changes: 4 additions & 8 deletions app/views/facilities/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
.button_row
= f.submit class: ['btn', 'btn-primary']
= render partial: 'product_list', locals: { products: @instruments, f: f, title_extra: daily_view_link }
= render partial: 'product_list', locals: { products: @services, f: f }
= render partial: 'product_list', locals: { products: @timed_services, f: f }
= render partial: 'product_list', locals: { products: @items, f: f }
= render partial: 'product_list', locals: { products: @bundles, f: f }
- ["Service", "TimedService", "Item", "Bundle"].each do |type|
= render partial: 'product_list', locals: { products: @non_instrument_products_by_type.fetch(type, []), f: f }
.button_row
= f.submit class: ['btn', 'btn-primary']
- else
.product_list_container{class: @columns}
= render partial: 'product_list', locals: { products: @instruments, title_extra: daily_view_link }
= render partial: 'product_list', locals: { products: @services }
= render partial: 'product_list', locals: { products: @timed_services }
= render partial: 'product_list', locals: { products: @items }
= render partial: 'product_list', locals: { products: @bundles }
- ["Service", "TimedService", "Item", "Bundle"].each do |type|
= render partial: 'product_list', locals: { products: @non_instrument_products_by_type.fetch(type, []) }

0 comments on commit 3212929

Please sign in to comment.