This repository was archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
[Fix Cart Latency] Remove CartReservations! #587
Merged
Merged
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
7f6cec8
first attempts at restructuring cart
squidgetx 24b8af5
cart spec and model
squidgetx 0303655
cart spec
squidgetx 62c0b4d
converted storage to cookie_storage
squidgetx 40b6b10
remove cart_reservations and deleted unnecessary files
squidgetx 0d27e56
edit _list_items partial to work with new item hash
squidgetx 0ad99e3
change cart method to accept attraccessor reserver_id : first commit …
squidgetx 5e76a2a
editing update_cart to no longer validate and also work with attracce…
squidgetx bd2faf6
add basic benchmarking to console : currently it takes 1500ms to chan…
squidgetx b03565a
remove cart reservation spec
squidgetx 5e4ef13
wrestling with the cart. still doesn't add items properly
squidgetx 1ea018e
Merge branch 'development' into 587_fix_cart_latency
squidgetx c4eb79d
continuing to remove traces of cart_reservation
squidgetx 58d92ad
disable activeadmin routes; enable this later
squidgetx 1700ad3
remove last traces of cart_reservations
squidgetx 639efc1
refactor method is_eligible_for_renew
squidgetx cf5880c
forgot an end
squidgetx 6637d1a
merge in development
squidgetx c3cbcce
cleaning up from dev merge
squidgetx ae7f1f9
re-enable validations
squidgetx 926aa24
remove last trace of CartReservation
squidgetx e01c1a1
spec edit
squidgetx b0ad1bf
fixing specs round 2
squidgetx 8036481
fix time related spec failures
squidgetx f24defa
Merge branch 'development' into 587_fix_cart_latency
squidgetx 5f9854a
remove cart reservation factory
squidgetx 78d50aa
Remove cookie_store
squidgetx 03227d7
reimplement fix due date
squidgetx 09fb2d4
edit cart factory
squidgetx 755c2fd
remove all references to set_start_date
squidgetx aeb899d
catch <= 0
squidgetx 190b6dc
remove useless error methods
squidgetx 8c5f56d
Merge branch '587_fix_cart_latency' of https://github.com/YaleSTC/res…
squidgetx 805e4b0
upload schema
squidgetx 339b27e
remove set_reserver id
squidgetx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.