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

Commit 8114287

Browse files
committed
Merge pull request #676 from YaleSTC/667_cart_compatibility_upgrade
Implements cart compatibility upgrade, as per #667.
2 parents b174431 + 5fed7e4 commit 8114287

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

app/controllers/application_controller.rb

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ApplicationController < ActionController::Base
1818
c.before_filter :fix_cart_date
1919
c.before_filter :set_view_mode
2020
c.before_filter :check_view_mode
21+
c.before_filter :make_cart_compatible
2122
end
2223

2324
helper_method :current_user
@@ -114,6 +115,15 @@ def fix_cart_date
114115
cart.fix_due_date
115116
end
116117

118+
# If user's session has an old Cart object that stores items in Array rather
119+
# than a Hash (see #587), regenerate the Cart.
120+
# TODO: Remove in ~2015, when nobody could conceivably run the old app?
121+
def make_cart_compatible
122+
unless session[:cart].items.is_a? Hash
123+
session[:cart] = Cart.new
124+
end
125+
end
126+
117127
#-------- end before_filter methods --------#
118128

119129
def update_cart

spec/controllers/application_controller_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ def method_requiring_user
4848
controller.stub(:fix_cart_date)
4949
controller.stub(:set_view_mode)
5050
controller.stub(:current_user)
51+
controller.stub(:make_cart_compatible)
52+
end
53+
54+
describe 'make_cart_compatible' do
55+
before(:each) do
56+
controller.unstub(:make_cart_compatible)
57+
end
58+
it 'replaces the cart if items is an Array' do
59+
session[:cart] = FactoryGirl.build(:cart, items: [1])
60+
get :index
61+
session[:cart].items.should be_a(Hash)
62+
session[:cart].items.should be_empty
63+
session[:cart].items.should_not be_a(Array)
64+
end
65+
it 'leaves the cart alone if items is a Hash' do
66+
session[:cart] = FactoryGirl.build(:cart_with_items)
67+
expect { get :index }.to_not change { session[:cart].items }
68+
end
5169
end
5270

5371
describe 'app_setup_check' do
@@ -243,6 +261,7 @@ def method_requiring_user
243261
controller.stub(:fix_cart_date)
244262
controller.stub(:set_view_mode)
245263
controller.stub(:current_user)
264+
controller.stub(:make_cart_compatible)
246265
end
247266

248267
#TODO - This may involve rewriting the method somewhat

0 commit comments

Comments
 (0)