mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-19 00:27:25 +00:00
Move distributor permissions onto order class, do not show links to change distributor if the action is invalid
This commit is contained in:
@@ -14,7 +14,7 @@ module Spree
|
||||
|
||||
order = current_order(true)
|
||||
|
||||
if order.line_items.empty?
|
||||
if order.can_change_distributor?
|
||||
order.distributor = distributor
|
||||
order.save!
|
||||
end
|
||||
@@ -25,7 +25,7 @@ module Spree
|
||||
def deselect
|
||||
order = current_order(true)
|
||||
|
||||
if order.line_items.empty?
|
||||
if order.can_change_distributor?
|
||||
order.distributor = nil
|
||||
order.save!
|
||||
end
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
Spree::Order.class_eval do
|
||||
belongs_to :distributor
|
||||
|
||||
def can_change_distributor?
|
||||
# Distributor may not be changed once an item has been added to the cart/order
|
||||
line_items.empty?
|
||||
end
|
||||
|
||||
# before_validation :shipping_address_from_distributor
|
||||
|
||||
private
|
||||
|
||||
@@ -6,7 +6,10 @@
|
||||
|
||||
%h6.filter_name Shop by Distributor
|
||||
%ul.filter_choices
|
||||
- order = current_order(false)
|
||||
- @distributors.each do |distributor|
|
||||
%li.nowrap= link_to distributor.name, select_distributor_path(distributor)
|
||||
- if current_distributor
|
||||
%li.nowrap
|
||||
- if order.nil? || order.can_change_distributor?
|
||||
= link_to distributor.name, select_distributor_path(distributor)
|
||||
- if current_distributor && order.can_change_distributor?
|
||||
%li.nowrap= link_to 'Leave distributor', deselect_distributors_path
|
||||
|
||||
11
spec/models/order_spec.rb
Normal file
11
spec/models/order_spec.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Order do
|
||||
it "provides permissions for changing distributor" do
|
||||
p = build(:product)
|
||||
|
||||
subject.can_change_distributor?.should be_true
|
||||
subject.add_variant(p.master, 1)
|
||||
subject.can_change_distributor?.should be_false
|
||||
end
|
||||
end
|
||||
@@ -46,6 +46,7 @@ feature %q{
|
||||
visit spree.root_path
|
||||
page.should_not have_selector 'a', :text => d1.name
|
||||
page.should_not have_selector 'a', :text => d2.name
|
||||
page.should_not have_selector 'a', :text => 'Leave distributor'
|
||||
end
|
||||
|
||||
context "adding a subsequent product to the cart" do
|
||||
|
||||
Reference in New Issue
Block a user