Merge branch 'master' into product-amount-units

Conflicts:
	app/assets/javascripts/admin/bulk_product_update.js.coffee
	spec/spec_helper.rb
This commit is contained in:
Rohan Mitchell
2014-01-14 15:34:05 +11:00
88 changed files with 1723 additions and 719 deletions

View File

@@ -8,7 +8,7 @@ class Cart < ActiveRecord::Base
order = create_or_find_order_for_distributor distributor, order_cycle, currency
@populator = Spree::OrderPopulator.new(order, currency)
@populator.populate({ :variants => { variant_id => quantity }, :distributor_id => distributor.id, :order_cycle_id => order_cycle })
@populator.populate({ :variants => { variant_id => quantity } })
end
def create_or_find_order_for_distributor distributor, order_cycle, currency
@@ -17,6 +17,7 @@ class Cart < ActiveRecord::Base
order_for_distributor = Spree::Order.create(:currency => currency, :distributor => distributor)
order_for_distributor.distributor = distributor
order_for_distributor.order_cycle = order_cycle
order_for_distributor.save!
orders << order_for_distributor
end

View File

@@ -1,26 +1,15 @@
Spree::OrderPopulator.class_eval do
def populate_with_distribution_validation(from_hash)
@distributor, @order_cycle = load_distributor_and_order_cycle(from_hash)
@distributor, @order_cycle = distributor_and_order_cycle
# Refactor: We may not need this validation - we can't change distribution here, so
# this validation probably can't fail
if !distribution_can_supply_products_in_cart(@distributor, @order_cycle)
errors.add(:base, "That distributor or order cycle can't supply all the products in your cart. Please choose another.")
end
# Set order distributor and order cycle
@orig_distributor, @orig_order_cycle = orig_distributor_and_order_cycle
cart_distribution_set = false
if valid?
set_cart_distributor_and_order_cycle @distributor, @order_cycle
cart_distribution_set = true
end
populate_without_distribution_validation(from_hash) if valid?
# Undo distribution setting if validation failed when adding a product
if !valid? && cart_distribution_set
set_cart_distributor_and_order_cycle @orig_distributor, @orig_order_cycle
end
valid?
end
alias_method_chain :populate, :distribution_validation
@@ -32,7 +21,7 @@ Spree::OrderPopulator.class_eval do
variant = Spree::Variant.find(variant_id)
if quantity > 0
if check_stock_levels(variant, quantity) &&
check_distribution_provided_for(variant) &&
check_order_cycle_provided_for(variant) &&
check_variant_available_under_distribution(variant)
@order.add_variant(variant, quantity, currency)
@@ -43,44 +32,18 @@ Spree::OrderPopulator.class_eval do
private
def orig_distributor_and_order_cycle
def distributor_and_order_cycle
[@order.distributor, @order.order_cycle]
end
def load_distributor_and_order_cycle(from_hash)
distributor = from_hash[:distributor_id].present? ?
Enterprise.is_distributor.find(from_hash[:distributor_id]) : nil
order_cycle = from_hash[:order_cycle_id].present? ?
OrderCycle.find(from_hash[:order_cycle_id]) : nil
[distributor, order_cycle]
end
def set_cart_distributor_and_order_cycle(distributor, order_cycle)
# Using @order.reload or not performing any reload causes totals fields (ie. item_total)
# to be set to zero
@order = Spree::Order.find @order.id
@order.set_distribution! distributor, order_cycle
end
def distribution_can_supply_products_in_cart(distributor, order_cycle)
DistributionChangeValidator.new(@order).can_change_to_distribution?(distributor, order_cycle)
end
def check_distribution_provided_for(variant)
distribution_provided = distribution_provided_for variant
unless distribution_provided
if order_cycle_required_for variant
errors.add(:base, "Please choose a distributor and order cycle for this order.")
else
errors.add(:base, "Please choose a distributor for this order.")
end
end
distribution_provided
def check_order_cycle_provided_for(variant)
order_cycle_provided = (!order_cycle_required_for(variant) || @order_cycle.present?)
errors.add(:base, "Please choose an order cycle for this order.") unless order_cycle_provided
order_cycle_provided
end
def check_variant_available_under_distribution(variant)
@@ -92,10 +55,6 @@ Spree::OrderPopulator.class_eval do
end
end
def distribution_provided_for(variant)
@distributor.present? && (!order_cycle_required_for(variant) || @order_cycle.present?)
end
def order_cycle_required_for(variant)
variant.product.product_distributions.empty?
end