-
Notifications
You must be signed in to change notification settings - Fork 57
[Fix Cart Latency] Remove CartReservations! #587
Changes from 25 commits
7f6cec8
24b8af5
0303655
62c0b4d
40b6b10
0d27e56
0ad99e3
5e76a2a
bd2faf6
b03565a
5e4ef13
1ea018e
c4eb79d
58d92ad
1700ad3
639efc1
cf5880c
6637d1a
c3cbcce
ae7f1f9
926aa24
e01c1a1
b0ad1bf
8036481
f24defa
5f9854a
78d50aa
03227d7
09fb2d4
755c2fd
aeb899d
190b6dc
8c5f56d
805e4b0
339b27e
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 |
---|---|---|
|
@@ -49,7 +49,7 @@ def new | |
redirect_to catalog_path | ||
else | ||
# error handling | ||
@errors = Reservation.validate_set(cart.reserver, cart.cart_reservations) | ||
@errors = Reservation.validate_set(cart.reserver, cart.prepare_all) | ||
unless @errors.empty? | ||
if can? :override, :reservation_errors | ||
flash[:error] = 'Are you sure you want to continue? Please review the errors below.' | ||
|
@@ -68,7 +68,8 @@ def create | |
#using http://stackoverflow.com/questions/7233859/ruby-on-rails-updating-multiple-models-from-the-one-controller as inspiration | ||
Reservation.transaction do | ||
begin | ||
@errors = Reservation.validate_set(cart.reserver, cart.cart_reservations) | ||
cart_reservations = cart.prepare_all | ||
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 = Reservation.validate_set(cart.reserver, cart_reservations) | ||
if @errors.empty? | ||
# If the reservation is a finalized reservation, save it as auto-approved ... | ||
params[:reservation][:approval_status] = "auto" | ||
|
@@ -83,7 +84,7 @@ def create | |
success_message = "This request has been successfully submitted, and is now subject to approval by an administrator." | ||
end | ||
|
||
cart.cart_reservations.each do |cart_res| | ||
cart_reservations.each do |cart_res| | ||
@reservation = Reservation.new(params[:reservation]) | ||
@reservation.equipment_model = cart_res.equipment_model | ||
# TODO: is this line needed? it's ugly. we should refactor if it's necessary. | ||
|
@@ -92,7 +93,6 @@ def create | |
successful_reservations << @reservation | ||
end | ||
|
||
cart.items.each { |item| CartReservation.delete(item) } | ||
session[:cart] = Cart.new | ||
|
||
# emails are probably failing---this code was already commented out 2014.06.19, and we don't know why. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ class Cart | |
|
||
def initialize | ||
@errors = ActiveModel::Errors.new(self) | ||
@items = [] | ||
@items = Hash.new() | ||
@start_date = Date.today | ||
@due_date = Date.tomorrow | ||
@reserver_id = nil | ||
|
@@ -37,107 +37,51 @@ def Cart.lookup_ancestors | |
|
||
## Item methods | ||
|
||
# Adds CartReservation to database; saves ID into items array | ||
# Adds equipment model id to items hash | ||
def add_item(equipment_model) | ||
current_item = CartReservation.new(start_date: @start_date, | ||
due_date: @due_date, reserver: self.reserver) | ||
current_item.equipment_model = equipment_model | ||
if current_item.save | ||
@items << current_item.id | ||
end | ||
return if equipment_model.nil? | ||
key = equipment_model.id.to_s | ||
self.items[key] = self.items[key] ? self.items[key] + 1 : 1 | ||
end | ||
|
||
# Removes CartReservation from database and ID from items array | ||
# Remove equipment model id from items hash, or decrement its count | ||
def remove_item(equipment_model) | ||
to_be_deleted = nil | ||
@items.each { |item| to_be_deleted = item if CartReservation.find(item).equipment_model == equipment_model } | ||
CartReservation.delete(to_be_deleted) | ||
@items.delete(to_be_deleted) | ||
end | ||
|
||
# Returns CartReservations that correspond to the IDs in the items array | ||
def cart_reservations | ||
CartReservation.where(id: @items) | ||
end | ||
|
||
# Returns a hash of the equipment models in the cart with their quantities | ||
def models_with_quantities | ||
models = Hash.new | ||
cart_reservations.each { |res| models[res.equipment_model.id] = res.same_model_count(cart_reservations) } | ||
models | ||
return if equipment_model.nil? | ||
key = equipment_model.id.to_s | ||
self.items[key] = self.items[key] ? self.items[key] - 1 : 0 | ||
self.items = self.items.except(key) if self.items[key] == 0 | ||
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. Should we make this |
||
end | ||
|
||
def empty? | ||
@items.empty? | ||
end | ||
|
||
## Date methods | ||
|
||
# Sets start date and updates all CartReservations to match | ||
def set_start_date(date) | ||
date = date.to_time.in_time_zone | ||
if date >= Date.today | ||
@start_date = date | ||
fix_due_date | ||
cart_reservations.update_all({start_date: @start_date, due_date: @due_date}) | ||
end | ||
end | ||
|
||
# Sets due date and updates all CartReservations to match | ||
def set_due_date(date) | ||
date = date.to_time.in_time_zone | ||
@due_date = date | ||
fix_due_date | ||
cart_reservations.update_all({start_date: @start_date, due_date: @due_date}) | ||
end | ||
|
||
# If the dates were illogical, sets due date to day after start date | ||
def fix_due_date | ||
if @start_date.to_time.in_time_zone > @due_date | ||
#TODO: allow admin to set default reservation length and respect that length here | ||
@due_date = @start_date + 1.day | ||
end | ||
# remove all items from cart | ||
def purge_all | ||
@items = Hash.new() | ||
end | ||
|
||
#Create an array of all the reservations that should be renewed instead of having a new reservation | ||
def renewable_reservations | ||
user_reservations = reserver.reservations | ||
renewable_reservations = [] | ||
@items.each do |item| | ||
cart_item_count = item.quantity #renew up to this many of the item | ||
matching_reservations = user_reservations.each do |res| | ||
# the end date should be the same as the start date | ||
# the reservation should be renewable | ||
# also the user should only renew as many reservations as they have in their cart | ||
if (res.due_date.to_date == @start_date && | ||
res.equipment_model_id == item.equipment_model_id && | ||
cart_item_count > 0 && | ||
res.is_eligible_for_renew?) | ||
renewable_reservations << res | ||
cart_item_count-= 1 | ||
end | ||
# return array of reservations crafted from the cart contents | ||
def prepare_all | ||
reservations = [] | ||
@items.each do |id, quantity| | ||
quantity.times do | ||
reservations << Reservation.new(reserver: self.reserver, | ||
start_date: @start_date, | ||
due_date: @due_date, | ||
equipment_model_id: id) | ||
end | ||
end | ||
return renewable_reservations | ||
reservations | ||
end | ||
|
||
|
||
# Returns the cart's duration | ||
def duration #in days | ||
@due_date.to_date - @start_date.to_date + 1 | ||
end | ||
|
||
## Reserver methods | ||
|
||
#TODO: should only have to set reserver OR reserver_id | ||
# Sets reserver id and updates the CartReservations to match | ||
def set_reserver_id(user_id) | ||
@reserver_id = user_id | ||
cart_reservations.update_all(reserver_id: @reserver_id) | ||
end | ||
|
||
# Returns the reserver | ||
def reserver | ||
reserver = User.find(@reserver_id) | ||
User.find(@reserver_id) | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
Reservations::Application.config.session_store :active_record_store | ||
Reservations::Application.config.session_store :cookie_store, key: '_app_session' | ||
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. please take this out and file a separate issue, so we can deal with the delicacies of cookies encryption and whatnot. :-) |
||
|
||
# Use the database for sessions instead of the cookie-based default, | ||
# which shouldn't be used to store highly confidential information | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class RemoveCartReservations < ActiveRecord::Migration | ||
def up | ||
drop_table :cart_reservations | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
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.
don't we need to change this to
cart.start_date
as well?set_start_date
is still a method ofCart
but here we're calling it on the session cart.