Merge pull request #3011 from HugsDaniel/3000-api-endpoint-enterprise-fee

Add an API endpoint for EnterpriseFeesController#destroy
This commit is contained in:
Pau Pérez Fabregat
2018-11-27 17:28:43 +01:00
committed by GitHub
7 changed files with 69 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
angular.module('admin.enterpriseFees').directive 'spreeDeleteResource', ->
(scope, element, attrs) ->
if scope.enterprise_fee.id
url = '/admin/enterprise_fees/' + scope.enterprise_fee.id
url = '/api/enterprise_fees/' + scope.enterprise_fee.id
html = '<a href="' + url + '" class="delete-resource icon_link icon-trash no-text" data-action="remove" data-confirm="' + t('are_you_sure') + '" url="' + url + '"></a>'
#var html = '<a href="'+url+'" class="delete-resource" data-confirm="Are you sure?"><img alt="Delete" src="/assets/admin/icons/delete.png" /> Delete</a>';
element.append html

View File

@@ -2,7 +2,6 @@ module Admin
class EnterpriseFeesController < ResourceController
before_filter :load_enterprise_fee_set, :only => :index
before_filter :load_data
before_filter :do_not_destroy_referenced_fees, :only => :destroy
def index
@@ -45,22 +44,6 @@ module Admin
private
def do_not_destroy_referenced_fees
product_distribution = ProductDistribution.where(:enterprise_fee_id => @object).first
if product_distribution
p = product_distribution.product
error = I18n.t(:enterprise_fees_destroy_error, id: p.id, name: p.name)
respond_with(@object) do |format|
format.html do
flash[:error] = error
redirect_to collection_url
end
format.js { render text: error, status: 403 }
end
end
end
def load_enterprise_fee_set
@enterprise_fee_set = EnterpriseFeeSet.new :collection => collection
end

View File

@@ -0,0 +1,21 @@
module Api
class EnterpriseFeesController < BaseController
respond_to :json
def destroy
authorize! :destroy, enterprise_fee
if enterprise_fee.destroy
render text: I18n.t(:successfully_removed), status: 204
else
render text: enterprise_fee.errors.full_messages.first, status: 403
end
end
private
def enterprise_fee
@enterprise_fee ||= EnterpriseFee.find_by_id params[:id]
end
end
end

View File

@@ -26,6 +26,7 @@ class EnterpriseFee < ActiveRecord::Base
validates_presence_of :name
before_save :ensure_valid_tax_category_settings
before_destroy :ensure_no_product_distributions
scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) }
scope :for_enterprises, lambda { |enterprises| where(enterprise_id: enterprises) }
@@ -68,6 +69,15 @@ class EnterpriseFee < ActiveRecord::Base
return true
end
def ensure_no_product_distributions
dependent_distribution = ProductDistribution.where(enterprise_fee_id: self).first
return unless dependent_distribution
product = dependent_distribution.product
error = I18n.t(:enterprise_fees_destroy_error, id: product.id, name: product.name)
errors.add(:base, error)
false
end
def refresh_products_cache
OpenFoodNetwork::ProductsCache.enterprise_fee_changed self
end

View File

@@ -111,6 +111,8 @@ Openfoodnetwork::Application.routes.draw do
resources :customers, only: [:index, :update]
resources :enterprise_fees, only: [:destroy]
post '/product_images/:product_id', to: 'product_images#update_product_image'
end

View File

@@ -35,7 +35,7 @@ Openfoodnetwork::Application.routes.draw do
resources :enterprise_relationships
resources :enterprise_roles
resources :enterprise_fees do
resources :enterprise_fees, except: :destroy do
collection do
get :for_order_cycle
post :bulk_update, :as => :bulk_update

View File

@@ -0,0 +1,34 @@
require 'spec_helper'
module Api
describe EnterpriseFeesController, type: :controller do
include AuthenticationWorkflow
let!(:unreferenced_fee) { create(:enterprise_fee) }
let!(:referenced_fee) { create(:enterprise_fee) }
let(:product) { create(:product) }
let(:distributor) { create(:distributor_enterprise) }
let!(:product_distribution) { create(:product_distribution, product: product, distributor: distributor, enterprise_fee: referenced_fee) }
let(:current_user) { create(:admin_user) }
before do
allow(controller).to receive(:spree_current_user) { current_user }
end
describe "destroy" do
it "removes the fee" do
expect { spree_delete :destroy, id: unreferenced_fee.id, format: :json }
.to change { EnterpriseFee.count }.by -1
end
context "when the fee is referenced by a product distribution" do
it "does not remove the fee" do
spree_delete :destroy, id: referenced_fee.id, format: :json
expect(response.status).to eq 403
expect(response.body).to match(/That enterprise fee cannot be deleted/)
expect(referenced_fee.reload).to eq(referenced_fee)
end
end
end
end
end