Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
fix number_for_date_range usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sydney Young committed Apr 11, 2016
1 parent bdc1bb8 commit a8ed003
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/models/cart_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def check_max_ems
valid = Reservation.number_for_date_range(source, start_date..due_date,
equipment_model_id: model.id,
overdue: false).max
valid ||= 0
# count overdue reservations for this eq model
overdue = source.count { |r| r.equipment_model == model && r.overdue }

Expand Down Expand Up @@ -153,6 +154,7 @@ def check_max_cat
max = cat.maximum_per_user
s = source.select { |r| r.equipment_model.category == cat }
s = Reservation.number_for_date_range(s, start_date..due_date).max
s ||= 0
o = overdue.count { |r| r.equipment_model.category == cat }
next unless s + o + q > max
errors << "Only #{max} #{cat.name.pluralize} can be reserved at a time."
Expand Down
8 changes: 5 additions & 3 deletions app/models/equipment_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ def document_attributes=(document_attributes)
def num_busy(start_date, due_date, source)
# get the number busy (not able to be reserved) in the source reservations
# uses 0 queries
Reservation.number_for_date_range(source, start_date..due_date,
equipment_model_id: id,
overdue: false).max + overdue_count
max = Reservation.number_for_date_range(source, start_date..due_date,
equipment_model_id: id,
overdue: false).max
max ||= 0
max + overdue_count
end
def num_available(start_date, due_date, source = nil)
Expand Down

0 comments on commit a8ed003

Please sign in to comment.