diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb index 5b9acf9..01aacc9 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -1,3 +1,5 @@ +require 'base64' + include ShoppingCartPlugin::CartHelper class ShoppingCartPluginProfileController < ProfileController @@ -6,6 +8,12 @@ class ShoppingCartPluginProfileController < ProfileController before_filter :login_required, :only => [] + def get + has_products = !cart.nil? && (cart[:items].keys.size > 0) || false + config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } + render :text => config.to_json + end + def add self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) @@ -85,6 +93,7 @@ class ShoppingCartPluginProfileController < ProfileController def buy @environment = profile.environment + @cart = cart render :layout => false end @@ -249,10 +258,31 @@ class ShoppingCartPluginProfileController < ProfileController protected def cart - session[:cart] + @cart ||= + begin + cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil + end + @cart end def cart=(data) - session[:cart] = data + @cart = data end + + after_filter :save_cookie + def save_cookie + if @cart.nil? && cookies[cookie_key] + cookies.delete(cookie_key) + else + cookies[cookie_key] = { + :value => Base64.encode64(@cart.to_yaml), + :path => "/profile/#{profile.identifier}/plugin/shopping_cart" + } + end + end + + def cookie_key + :_noosfero_session_shopping_cart + end + end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin.rb b/plugins/shopping_cart/lib/shopping_cart_plugin.rb index e335f32..384a57b 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin.rb @@ -35,7 +35,7 @@ class ShoppingCartPlugin < Noosfero::Plugin end def body_beginning - expanded_template('cart.html.erb',{:cart => context.session[:cart]}) + expanded_template('cart.html.erb') end def control_panel_buttons diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb index 8f19397..9c9acce 100644 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb @@ -48,7 +48,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase end should 'not try to remove a product if there is no cart' do - instantiate_session + instantiate_cart assert !cart? assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } @@ -76,7 +76,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase end should 'not try to list the cart if there is no cart' do - instantiate_session + instantiate_cart assert !cart? assert_nothing_raised { get :list, :profile => enterprise.identifier } @@ -100,7 +100,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase end should 'not try to update quantity the quantity of a product if there is no cart' do - instantiate_session + instantiate_cart assert !cart? assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } @@ -139,7 +139,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase end should 'not crash if there is no cart' do - instantiate_session + instantiate_cart assert !cart? assert_nothing_raised { get :clean, :profile => enterprise.identifier } end @@ -147,7 +147,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase should 'register order on send request' do product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) - @controller.stubs(:session).returns({:cart => {:items => {product1.id => 1, product2.id => 2}}}) + @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do post :send_request, :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, @@ -165,7 +165,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase should 'register order on send request and not crash if product is not defined' do product1 = fast_create(Product, :enterprise_id => enterprise.id) - @controller.stubs(:session).returns({:cart => {:items => {product1.id => 1}}}) + @controller.stubs(:cart).returns({:items => {product1.id => 1}}) assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do post :send_request, :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, @@ -184,15 +184,15 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase end def cart? - !session[:cart].nil? + @controller.send(:cart).nil? end def product_in_cart?(product) - session[:cart][:items].has_key?(product.id) + @controller.send(:cart)[:items].has_key?(product.id) end def product_quantity(product) - session[:cart][:items][product.id] + @controller.send(:cart)[:items][product.id] end def response_ok? @@ -205,7 +205,7 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase # temporary hack...if I don't do this the session stays as an Array instead # of a TestSession - def instantiate_session + def instantiate_cart get :add, :profile => enterprise.identifier, :id => product.id get :remove, :profile => enterprise.identifier, :id => product.id end diff --git a/plugins/shopping_cart/views/cart.html.erb b/plugins/shopping_cart/views/cart.html.erb index 18eef10..f39af37 100644 --- a/plugins/shopping_cart/views/cart.html.erb +++ b/plugins/shopping_cart/views/cart.html.erb @@ -18,6 +18,19 @@ diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb index 9db0672..9c60970 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb @@ -16,7 +16,7 @@ <%= submit_button(:send, _('Send buy request')) %> <% end %> - <%= items_table(session[:cart][:items], profile) %> + <%= items_table(@cart[:items], profile) %> <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %> diff --git a/vendor/plugins/noosfero_caching/init.rb b/vendor/plugins/noosfero_caching/init.rb index ba951bf..1033838 100644 --- a/vendor/plugins/noosfero_caching/init.rb +++ b/vendor/plugins/noosfero_caching/init.rb @@ -38,6 +38,8 @@ module NoosferoHttpCaching def call(env) status, headers, body = @app.call(env) if headers['X-Noosfero-Auth'] == 'false' + # FIXME do not do this if there is any plugin cookie set (e.g. + # _noosfero_session_shopping_cart) headers.delete('Set-Cookie') end headers.delete('X-Noosfero-Auth') -- libgit2 0.21.2