Move distributor permissions onto order class, do not show links to change distributor if the action is invalid

This commit is contained in:
Rohan Mitchell
2012-06-24 15:15:53 +10:00
parent 7b92fcb614
commit a41c5d4735
5 changed files with 24 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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