Standing Orders: adding routing and controller action for deleting standing line items

This commit is contained in:
Rob Harrington
2016-10-20 10:52:09 +11:00
parent 594bec1b37
commit 38d75d2b1c
5 changed files with 44 additions and 1 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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