Skip to content

Commit

Permalink
Feature #1257 Warenkorb wird angezeigt, auch wenn er leer ist
Browse files Browse the repository at this point in the history
  • Loading branch information
chiramiso committed Jan 20, 2015
1 parent 7165fb7 commit 7063ef1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class CartsController < ApplicationController

before_filter :generate_session, only: :edit
before_filter :clear_session, only: :show # userhas the possibility to reset the session by continue buying
before_filter :set_cart
before_filter :set_cart, except: :empty_cart
before_filter :dont_cache, only: [:edit, :update]

before_filter :authorize_and_authenticate_user_on_cart, only: :show
skip_before_filter :authenticate_user!, only: :show
skip_before_filter :authenticate_user!, only: [:show, :empty_cart]


def show
Expand Down Expand Up @@ -65,6 +65,11 @@ def update
end
end

def empty_cart
authorize Cart.new
render :empty_cart
end

private

def generate_session
Expand Down
4 changes: 4 additions & 0 deletions app/policies/cart_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ def update?
user == cart.user
end

def empty_cart? cookie_id = nil
(!user && !cookie_id) || (user && !user.carts.open.any?)
end

end
4 changes: 4 additions & 0 deletions app/views/carts/empty_cart.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h2
=t('cart.heading')
.Content
= t('cart.texts.empty_cart')
3 changes: 3 additions & 0 deletions app/views/layouts/partials/_header_nav.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
/ - current_cart.line_items.each do |line_item|
/ = render "layouts/partials/header_nav_cart_item", line_item: line_item
/ = link_to t('header.cart.button'), current_cart, class: 'Button'
- else
li
= link_to t('header.cart.title', count: 0), empty_cart_path, data: navigation_push
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
end
end

resources :carts, only: [:show,:edit,:update]
resources :carts, only: [:show, :edit, :update]

match '/empty_cart', to: 'carts#empty_cart', as: 'empty_cart', via: :get

resources :line_items, only: [:create,:update,:destroy]

Expand All @@ -98,7 +100,7 @@
resources :business_transactions, only: [:show] do
resources :refunds, only: [ :new, :create ]
end
match '/transactions/set_transport_ready/:id', to: 'business_transactions#set_transport_ready', as: 'set_transport_ready', via: [:get, :pos]
match '/transactions/set_transport_ready/:id', to: 'business_transactions#set_transport_ready', as: 'set_transport_ready', via: [:get, :post]

get 'welcome/reconfirm_terms'
post 'welcome/reconfirm_terms'
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/carts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative '../test_helper'

describe CartsController do
it 'should render empty cart template when user has no cart' do
get :empty_cart
assert_response :success
assert_template :empty_cart
end
end
9 changes: 9 additions & 0 deletions test/features/cart_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ def expect_cart_emails arg= :once
CartMailer.expects(:buyer_email).returns(Mail::Message.new)
end

feature 'Empty cart' do
it 'header should show link to empty cart' do
visit root_path
page.html.must_include I18n.t('header.cart.title', count: 0)
click_link(I18n.t('header.cart.title', count: 0), match: :first)
page.must_have_content 'Dein Warenkorb ist leer.'
end
end

feature 'Adding an Article to the cart' do

scenario 'anonymous user adds article to his cart' do
Expand Down
3 changes: 3 additions & 0 deletions test/policies/cart_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
it { subject.must_deny(:show) }
it { subject.must_deny(:edit) }
it { subject.must_deny(:update) }
it { subject.must_permit(:empty_cart) }
end

describe "for a random logged-in user" do
let(:user) { FactoryGirl.create :user }
it { subject.must_deny(:show) }
it { subject.must_deny(:edit) }
it { subject.must_deny(:update) }
it { subject.must_permit(:empty_cart) }
end

describe "for the owning user" do
let(:user) { cart.user }
it { subject.must_permit(:show) }
it { subject.must_permit(:edit) }
it { subject.must_permit(:update) }
it { subject.must_deny(:empty_cart) }
end
end

0 comments on commit 7063ef1

Please sign in to comment.