mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Instead of protecting shipping methods from destruction when referenced by product distributions, protect enterprise fees
This commit is contained in:
@@ -2,6 +2,8 @@ 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
|
||||
respond_to do |format|
|
||||
@@ -21,6 +23,20 @@ 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
|
||||
flash[:error] = "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}."
|
||||
|
||||
respond_with(@object) do |format|
|
||||
format.html { redirect_to collection_url }
|
||||
format.js { render text: flash[:error], status: 403 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def load_enterprise_fee_set
|
||||
@enterprise_fee_set = EnterpriseFeeSet.new :collection => collection
|
||||
end
|
||||
@@ -32,6 +48,5 @@ module Admin
|
||||
def collection
|
||||
super.order('enterprise_id', 'fee_type', 'name')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -99,4 +99,27 @@ feature %q{
|
||||
page.should_not have_selector "input[value='#{fee.name}']"
|
||||
end
|
||||
|
||||
scenario "deleting a shipping method referenced by a product distribution" do
|
||||
# Given an enterprise fee referenced by a product distribution
|
||||
fee = create(:enterprise_fee)
|
||||
p = create(:product)
|
||||
d = create(:distributor_enterprise)
|
||||
create(:product_distribution, product: p, distributor: d, enterprise_fee: fee)
|
||||
|
||||
# When I go to the enterprise fees page
|
||||
login_to_admin_section
|
||||
click_link 'Configuration'
|
||||
click_link 'Enterprise Fees'
|
||||
|
||||
# And I click delete
|
||||
find("a.delete-resource").click
|
||||
|
||||
# Then I should see an error
|
||||
page.should have_content "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}."
|
||||
|
||||
# And my enterprise fee should not have been deleted
|
||||
visit admin_enterprise_fees_path
|
||||
page.should have_selector "input[value='#{fee.name}']"
|
||||
EnterpriseFee.find(fee.id).should_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,15 +26,4 @@ feature 'shipping methods' do
|
||||
page.should have_content "That shipping method cannot be deleted as it is referenced by an order: #{o.number}."
|
||||
Spree::ShippingMethod.find(@sm.id).should_not be_nil
|
||||
end
|
||||
|
||||
scenario "deleting a shipping method referenced by a product distribution" do
|
||||
p = create(:product)
|
||||
d = create(:distributor_enterprise)
|
||||
create(:product_distribution, product: p, distributor: d, shipping_method: @sm)
|
||||
|
||||
visit_delete spree.admin_shipping_method_path(@sm)
|
||||
|
||||
page.should have_content "That shipping method cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}."
|
||||
Spree::ShippingMethod.find(@sm.id).should_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user