Skip to content

Commit

Permalink
Eager load alerts and offline reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinco committed Mar 6, 2025
1 parent ed40714 commit 234d970
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def facility_product_path(facility, product)
def warning_if_instrument_is_offline_or_partially_available(instrument)
if instrument.offline?
tooltip_icon "fa fa-exclamation-triangle icon-large", t("instruments.offline.note")
elsif instrument.alert
elsif instrument.alert.present?
tooltip_icon "fa fa-exclamation-triangle partially-available-warning icon-large", instrument.alert.note
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/models/concerns/products/scheduling_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ def quick_reservation_intervals
end

def offline?
current_offline_reservations.present?
if current_offline_reservations.loaded?
current_offline_reservations.present?
else
current_offline_reservation.exists?
end
end

def offline_category
Expand Down
10 changes: 4 additions & 6 deletions app/models/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ module Pricing
end
end.freeze

with_options foreign_key: "product_id" do |instrument|
instrument.has_many :admin_reservations
instrument.has_many :instrument_price_policies
instrument.has_many :offline_reservations
instrument.has_many :current_offline_reservations, -> { current }, class_name: "OfflineReservation"
with_options foreign_key: "product_id" do
has_many :admin_reservations
has_many :instrument_price_policies
has_many :offline_reservations
end
has_one :alert, dependent: :destroy, class_name: "InstrumentAlert"

email_list_attribute :cancellation_email_recipients
email_list_attribute :issue_report_recipients
Expand Down
22 changes: 13 additions & 9 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ class Product < ApplicationRecord
has_one :product_display_group_product
has_one :product_display_group, through: :product_display_group_product

# Instrument specifc
has_one(
:alert,
dependent: :destroy,
class_name: "InstrumentAlert",
foreign_key: :instrument_id,
inverse_of: :instrument
)
has_many :current_offline_reservations, -> { current }, class_name: "OfflineReservation"

before_save :start_time_disabled_daily_booking_only
after_create :create_default_price_group_products
after_create :create_skip_review_price_policies, if: :skip_review_mode?
after_create :create_nonbillable_price_policy, if: :nonbillable_mode?
before_save :start_time_disabled_daily_booking_only

email_list_attribute :training_request_contacts

Expand Down Expand Up @@ -70,11 +80,9 @@ def self.billing_modes
scope :mergeable_into_order, -> { not_archived.where(type: mergeable_types) }
scope :cross_core_available, -> { where(cross_core_ordering_available: true) }
scope :in_active_facility, -> { joins(:facility).where(facilities: { is_active: true }) }
scope :of_type, ->(type) { where(type: type) }
scope :of_type, ->(type) { where(type:) }
scope :with_schedule, -> { where.not(schedule_id: nil) }
scope :without_display_group, -> {
left_outer_joins(:product_display_group_product).where(product_display_group_products: { id: nil })
}
scope :without_display_group, -> { where.missing(:product_display_group_product) }

# All product types. This cannot be a cattr_accessor because the block is evaluated
# at definition time (not lazily as I expected) and this causes a circular dependency
Expand Down Expand Up @@ -332,10 +340,6 @@ def is_accessible_to_user?(user)
!(is_archived? || (is_hidden? && !is_operator))
end

def alert
nil
end

def duration_pricing_mode?
false
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/product_display_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def to_s

def self.fake_groups_by_type(products)
Product.orderable_types.map do |type|
Fake.new(name: type.constantize.model_name.human(count: :many), products: products.where(type: type))
Fake.new(
name: type.constantize.model_name.human(count: :many),
products: products.where(type:)
)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/facilities/_product_display_group.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- collection = product_display_group.products.merge(@product_scope)
- collection = product_display_group.products.merge(@product_scope).includes(:alert, :current_offline_reservations)
- if collection.present?
.product_list{ class: classes }
%h3= product_display_group
Expand Down

0 comments on commit 234d970

Please sign in to comment.