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

Cart compatibility upgrade #676

Merged
merged 6 commits into from
Jul 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ApplicationController < ActionController::Base
c.before_filter :fix_cart_date
c.before_filter :set_view_mode
c.before_filter :check_view_mode
c.before_filter :make_cart_compatible
end

helper_method :current_user
Expand Down Expand Up @@ -114,6 +115,15 @@ def fix_cart_date
cart.fix_due_date
end

# If user's session has an old Cart object that stores items in Array rather
# than a Hash (see #587), regenerate the Cart.
# TODO: Remove in ~2015, when nobody could conceivably run the old app?
def make_cart_compatible
unless session[:cart].items.is_a? Hash
session[:cart] = Cart.new
end
end

#-------- end before_filter methods --------#

def update_cart
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def method_requiring_user
controller.stub(:fix_cart_date)
controller.stub(:set_view_mode)
controller.stub(:current_user)
controller.stub(:make_cart_compatible)
end

describe 'make_cart_compatible' do
before(:each) do
controller.unstub(:make_cart_compatible)
end
it 'replaces the cart if items is an Array' do
session[:cart] = FactoryGirl.build(:cart, items: [1])
get :index
session[:cart].items.should be_a(Hash)
session[:cart].items.should be_empty
session[:cart].items.should_not be_a(Array)
end
it 'leaves the cart alone if items is a Hash' do
session[:cart] = FactoryGirl.build(:cart_with_items)
expect { get :index }.to_not change { session[:cart].items }
end
end

describe 'app_setup_check' do
Expand Down Expand Up @@ -243,6 +261,7 @@ def method_requiring_user
controller.stub(:fix_cart_date)
controller.stub(:set_view_mode)
controller.stub(:current_user)
controller.stub(:make_cart_compatible)
end

#TODO - This may involve rewriting the method somewhat
Expand Down