-
Notifications
You must be signed in to change notification settings - Fork 57
Fundamental Rewrite of Validation System #644
Changes from 22 commits
050e5f6
83f1fc5
713774c
fbf0ea5
e6e2cc5
a8e53a7
4ccf7df
cd922e2
2a6ccfd
ead90de
a381359
4d3f10c
79f3311
9cac163
66b271f
6e6669b
3d7e684
50929dc
ef96a84
38f077c
732c089
520db6a
289d331
79a3246
8d1a50c
13a269a
a93b4dc
0fc4537
f81b282
04fe394
da72d24
9822540
68509c6
882c9dd
c97a030
15384b2
5a2bc17
57c31db
c0dfabf
29193f0
c14851c
4719214
13cb8f0
7116b9d
761e8c4
469f641
e614951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module CartValidations | ||
def validate_dates | ||
# run on date change | ||
errors = [] | ||
# blackouts not on date | ||
errors << "blackout exists on start date" if Blackout.hard_blackout_exists_on_date(self.start_date) | ||
errors << "blackout exists on end date" if Blackout.hard_blackout_exists_on_date(self.due_date) | ||
errors << "overdue reservations" if Reservation.for_reserver(self.reserver_id).overdue.count > 0 | ||
# for some reason reserver is submitted at the same time as dates | ||
return errors | ||
end | ||
|
||
def validate_items | ||
errors = [] | ||
relevant = Reservation.for_reserver(self.reserver_id).not_returned | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an AR Relation? So line 25 then builds on this, chains two more scopes, and counts however many reservations remain? (Sorry for the stupid question, but I've read this code over three times and it only clicked now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
|
||
category = Hash.new | ||
# check if under max model count while simultaneously building a category hash | ||
self.items.each do |em_id, quantity| | ||
model = EquipmentModel.find(em_id) | ||
max_models = model.maximum_per_user | ||
self.start_date.to_date.upto(self.due_date.to_date) do |d| | ||
errors << "over max model count" if relevant.overlaps_with_date(d).for_eq_model(model).count + quantity > max_models | ||
break | ||
end | ||
|
||
if category.include?(model.category) | ||
category[model.category] += quantity | ||
else | ||
category[model.category] = quantity | ||
end | ||
|
||
end | ||
|
||
# check if under max category count | ||
category.each do |cat, q| | ||
max_cat = cat.maximum_per_user | ||
self.start_date.to_date.upto(due_date.to_date) do |d| | ||
count = 0 | ||
relevant.overlaps_with_date(d).each do |r| | ||
count += 1 if r.equipment_model.category == cat | ||
end | ||
errors << "over max category count" if count + q > max_cat | ||
break | ||
end | ||
end | ||
return errors | ||
|
||
end | ||
|
||
def validate_dates_and_items | ||
# validations that run on both item and date changes | ||
|
||
user_reservations = Reservation.for_reserver(self.reserver_id).checked_out | ||
errors = [] | ||
self.items.each do |item, quantity| | ||
model = EquipmentModel.find(item) | ||
|
||
# check availability | ||
errors << "not available" if model.num_available(self.start_date, self.due_date) < quantity | ||
|
||
# check maximum checkout length | ||
max_length = model.category.max_checkout_length | ||
max_length = Float::INFINTIY if max_length == 'unrestricted' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
errors << "too long checkout length" if self.duration > max_length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this and the next error message probably need to be tweaked (but I realize that I may be code reviewing prematurely) |
||
|
||
user_reservations.for_eq_model(model).each do |r| | ||
errors << "renew, man" if r.due_date == self.start_date && r.is_eligible_for_renew? | ||
end | ||
end | ||
return errors | ||
end | ||
|
||
def validate_all | ||
errors = validate_dates | ||
errors.concat(validate_items.to_a).concat(validate_dates_and_items.to_a) | ||
return errors | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a more descriptive name:
validate_max_number_of_items
?