Fixes following code review for checkout changes

This commit is contained in:
Rob H
2013-01-18 19:17:55 +11:00
parent 6a45e8be3b
commit f7890bd94d
10 changed files with 27 additions and 53 deletions

View File

@@ -5,7 +5,6 @@ module ApplicationHelper
end
end
# Pass URL helper calls on to spree where applicable so that we don't need to use
# spree.foo_path in any view rendered from non-spree-namespaced controllers.
def method_missing(method, *args, &block)

View File

@@ -2,4 +2,8 @@ module EnterprisesHelper
def current_distributor
@current_distributor ||= current_order(false).andand.distributor
end
def enterprises_options enterprises
enterprises.map { |enterprise| [enterprise.name + ": " + enterprise.address.address1 + ", " + enterprise.address.city, enterprise.id.to_i] }
end
end

View File

@@ -5,5 +5,9 @@ module Spree
amount = order.line_items.map { |li| li.itemwise_shipping_cost }.sum
options.delete(:format_as_currency) ? number_to_currency(amount) : amount
end
def alternative_available_distributors(order)
DistributorChangeValidator.new(order).available_distributors(Enterprise.all) - [order.distributor]
end
end
end

View File

@@ -15,11 +15,6 @@ Spree::Order.class_eval do
errors.add(:distributor_id, "cannot supply the products in your cart") unless DistributorChangeValidator.new(self).can_change_to_distributor?(distributor)
end
def distributor=(distributor)
#raise "You cannot change the distributor of an order with products" unless distributor == self.distributor || DistributorChangeValidator.new(self).can_change_to_distributor?(distributor)
super(distributor)
end
def set_distributor!(distributor)
self.distributor = distributor
save!
@@ -36,10 +31,6 @@ Spree::Order.class_eval do
line_item.save!
end
def can_add_product_to_cart? product
DistributorChangeValidator.new(self).can_change_distributor? || product.distributors.include?(distributor)
end
def line_item_variants
line_items.map{ |li| li.variant }
end

View File

@@ -1,14 +1,9 @@
<% unless @order.state != 'address' %>
<div class="columns omega four" style="padding-top: 30px">
<%
other_available_distributors = DistributorChangeValidator.new(@order).available_distributors(Enterprise.all)
other_available_distributors.delete(@order.distributor)
unless other_available_distributors.empty?
%>
<% unless alternative_available_distributors(@order).empty? %>
<%= form_for(@order) do |f| %>
<%= f.label :distributor_label, "Alternative distributors for this order:" %>
<% available_distributors_array = other_available_distributors.map { |distributor| [distributor.name + ": " + distributor.address.address1 + ", " + distributor.address.city, distributor.id.to_i] } %>
<%= f.select :distributor_id, options_for_select( available_distributors_array ) %>
<%= f.select :distributor_id, options_for_select( enterprises_options(alternative_available_distributors(@order)) ) %>
<%= f.submit "Change Distributor" %>
<% end %>
<% else %>

View File

@@ -1,3 +1,3 @@
%td(data-hook = "order_item_description")
%h4= item.variant.product.name
= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty?
= "(" + variant_options(item.variant) + ")" unless item.variant.option_values.empty?

View File

@@ -3,7 +3,7 @@
- if !@product.has_stock? && !Spree::Config[:allow_backorders]
= content_tag('strong', t(:out_of_stock))
- elsif !order.nil? && !order.can_add_product_to_cart?(@product)
- elsif !order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !@product.distributors.include?(order.distributor)
.error-distributor
Please complete your order at
= link_to current_distributor.name, root_path

View File

@@ -28,38 +28,6 @@ describe Spree::Order do
li.max_quantity.should == 3
end
context "permissions for adding products to the cart" do
it "allows products to be added to cart when cart is empty" do
p_first = double(:product)
p_first.stub(:distributors) { [d1] }
subject.stub(:line_items) { [] }
subject.can_add_product_to_cart?(p_first).should be_true
end
it "allows products to be added to cart when they are available from the current distributor" do
d1 = double(:distributor)
p_first = double(:product)
p_first.stub(:distributors) { [d1] }
p_subsequent_same_dist = double(:product)
p_subsequent_same_dist.stub(:distributors) { [d1] }
subject.stub(:line_items) { [1, 2, 3] }
subject.stub(:distributor) { d1 }
subject.can_add_product_to_cart?(p_subsequent_same_dist).should be_true
end
it "does not allow products to be added to cart when they are not available from the current distributor" do
d1 = double(:distributor)
d2 = double(:distributor)
p_first = double(:product)
p_first.stub(:distributors) { [d1] }
p_subsequent_other_dist = double(:product)
p_subsequent_other_dist.stub(:distributors) { [d2] }
subject.stub(:line_items) { [1, 2, 3] }
subject.stub(:distributor) { d1 }
subject.can_add_product_to_cart?(p_subsequent_other_dist).should be_false
end
end
context "validating distributor changes" do
it "checks that a distributor is available when changing" do
order_enterprise = FactoryGirl.create(:enterprise, id: 1, :name => "Order Enterprise")

View File

@@ -17,6 +17,16 @@ feature %q{
:state => Spree::State.find_by_name('Victoria'),
:country => Spree::Country.find_by_name('Australia')),
:pickup_times => 'Tuesday, 4 PM')
@distributor_alternative = create(:distributor_enterprise, :name => 'Alternative Distributor',
:address => create(:address,
:address1 => '1600 Rathdowne St',
:city => 'Carlton North',
:zipcode => 3054,
:state => Spree::State.find_by_name('Victoria'),
:country => Spree::Country.find_by_name('Australia')),
:pickup_times => 'Tuesday, 4 PM')
@shipping_method_1 = create(:shipping_method, :name => 'Shipping Method One')
@shipping_method_1.calculator.set_preference :amount, 1
@@ -28,9 +38,11 @@ feature %q{
@product_1 = create(:product, :name => 'Fuji apples')
@product_1.product_distributions.create(:distributor => @distributor, :shipping_method => @shipping_method_1)
@product_1.product_distributions.create(:distributor => @distributor_alternative, :shipping_method => @shipping_method_1)
@product_2 = create(:product, :name => 'Garlic')
@product_2.product_distributions.create(:distributor => @distributor, :shipping_method => @shipping_method_2)
@product_2.product_distributions.create(:distributor => @distributor_alternative, :shipping_method => @shipping_method_2)
@zone = create(:zone)
c = Spree::Country.find_by_name('Australia')
@@ -110,7 +122,9 @@ feature %q{
page.should have_content value
end
end
page.should have_selector "select#order_distributor_id option[value='#{@distributor_alternative.id}']"
click_button 'Save and Continue'
# -- Checkout: Delivery

View File

@@ -144,7 +144,6 @@ feature %q{
# When we select the distributor and view the product
visit spree.select_distributor_order_path(distributor1)
visit spree.product_path(product)
binding.pry
# Then we should see our distributor as the default option when adding the item to our cart
page.should have_selector "select#distributor_id option[value='#{distributor1.id}'][selected='selected']"