From 38d75d2b1c6e4fa2f25ddcc34f31216242dfe329 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 20 Oct 2016 10:52:09 +1100 Subject: [PATCH] Standing Orders: adding routing and controller action for deleting standing line items --- .../admin/standing_line_items_controller.rb | 5 ++++ app/models/spree/ability_decorator.rb | 3 +++ config/routes.rb | 2 +- .../standing_line_items_controller_spec.rb | 26 +++++++++++++++++++ spec/factories.rb | 9 +++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/standing_line_items_controller.rb b/app/controllers/admin/standing_line_items_controller.rb index 60e09f96e8..ec1bdffd82 100644 --- a/app/controllers/admin/standing_line_items_controller.rb +++ b/app/controllers/admin/standing_line_items_controller.rb @@ -7,6 +7,11 @@ module Admin respond_to :json + respond_override destroy: { json: { + success: lambda { render nothing: true, :status => 204 }, + failure: lambda { render json: { errors: @standing_line_item.errors.full_messages }, status: :unprocessable_entity } + } } + def build return render json: { errors: ['Unauthorised'] }, status: :unauthorized unless @shop if @variant diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 740bcc698f..2bf031969c 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -257,6 +257,9 @@ class AbilityDecorator user.enterprises.include?(standing_order.shop) end can [:admin, :build], StandingLineItem + can [:destroy], StandingLineItem do |standing_line_item| + user.enterprises.include?(standing_line_item.standing_order.shop) + end end def add_relationship_management_abilities(user) diff --git a/config/routes.rb b/config/routes.rb index 0056651de9..ca19716a40 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -179,7 +179,7 @@ Openfoodnetwork::Application.routes.draw do resources :standing_orders, only: [:index, :new, :create, :edit, :update] - resources :standing_line_items, only: [], format: :json do + resources :standing_line_items, only: [:destroy], format: :json do post :build, on: :collection end end diff --git a/spec/controllers/admin/standing_line_items_controller_spec.rb b/spec/controllers/admin/standing_line_items_controller_spec.rb index fbb51015d8..3cbdf78a93 100644 --- a/spec/controllers/admin/standing_line_items_controller_spec.rb +++ b/spec/controllers/admin/standing_line_items_controller_spec.rb @@ -95,4 +95,30 @@ describe Admin::StandingLineItemsController, type: :controller do end end end + + describe "destroy" do + let!(:standing_order) { create(:standing_order_with_items) } + let!(:item) { standing_order.standing_line_items.first } + let!(:user) { create(:user) } + let(:params) { { format: :json, id: item.id } } + + context 'as an enterprise user' do + before { allow(controller).to receive(:spree_current_user) { user } } + + context "that does not manage the relevant shop" do + it "returns an error" do + spree_post :destroy, params + expect(response).to redirect_to spree.unauthorized_path + end + end + + context 'that manages that relevant shop' do + before { standing_order.shop.update_attributes(owner: user) } + + it 'removes the item' do + expect{ spree_post :destroy, params }.to change{standing_order.standing_line_items.count}.by(-1) + end + end + end + end end diff --git a/spec/factories.rb b/spec/factories.rb index 2cb8915dc0..9eba9080ad 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -149,6 +149,15 @@ FactoryGirl.define do begins_at { 1.month.ago } end + factory :standing_order_with_items, parent: :standing_order do |standing_order| + standing_line_items { create_list(:standing_line_item, 3) } + before(:create) do |standing_order, proxy| + oc = standing_order.schedule.order_cycles.first + ex = create(:exchange, :order_cycle => oc, :sender => standing_order.shop, :receiver => standing_order.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + standing_order.standing_line_items.each { |sli| ex.variants << sli.variant } + end + end + factory :standing_line_item, :class => StandingLineItem do standing_order variant