Perform variant override scoping on product/variant by external class. Centralise this so we can load everything in one go.

This commit is contained in:
Rohan Mitchell
2015-06-18 13:11:11 +10:00
parent 6ed9a2620c
commit 7cc2bc4fde
11 changed files with 67 additions and 57 deletions

View File

@@ -1,16 +1,21 @@
require 'open_food_network/scope_variant_to_hub'
module OpenFoodNetwork
module ScopeProductToHub
def variants_distributed_by(order_cycle, distributor)
super.each { |v| v.scope_to_hub @hub }
end
end
module ProductScopableToHub
def scope_to_hub(hub)
extend OpenFoodNetwork::ScopeProductToHub
class ScopeProductToHub
def initialize(hub)
@hub = hub
end
def scope(product)
product.send :extend, OpenFoodNetwork::ScopeProductToHub::ScopeProductToHub
product.instance_variable_set :@hub, @hub
end
module ScopeProductToHub
def variants_distributed_by(order_cycle, distributor)
super.each { |v| ScopeVariantToHub.new(@hub).scope(v) }
end
end
end
end

View File

@@ -1,30 +1,36 @@
module OpenFoodNetwork
module ScopeVariantToHub
def price
VariantOverride.price_for(@hub, self) || super
end
def price_in(currency)
Spree::Price.new(amount: price, currency: currency)
end
def count_on_hand
VariantOverride.count_on_hand_for(@hub, self) || super
end
def decrement!(attribute, by=1)
if attribute == :count_on_hand && VariantOverride.stock_overridden?(@hub, self)
VariantOverride.decrement_stock! @hub, self, by
else
super
end
end
end
module VariantScopableToHub
def scope_to_hub(hub)
extend OpenFoodNetwork::ScopeVariantToHub
class ScopeVariantToHub
def initialize(hub)
@hub = hub
end
def scope(variant)
variant.send :extend, OpenFoodNetwork::ScopeVariantToHub::ScopeVariantToHub
variant.instance_variable_set :@hub, @hub
end
module ScopeVariantToHub
def price
VariantOverride.price_for(@hub, self) || super
end
def price_in(currency)
Spree::Price.new(amount: price, currency: currency)
end
def count_on_hand
VariantOverride.count_on_hand_for(@hub, self) || super
end
def decrement!(attribute, by=1)
if attribute == :count_on_hand && VariantOverride.stock_overridden?(@hub, self)
VariantOverride.decrement_stock! @hub, self, by
else
super
end
end
end
end
end

View File

@@ -3,8 +3,9 @@ Spree::Core::ControllerHelpers::Order.class_eval do
order = current_order_without_scoped_variants(create_order_if_necessary)
if order
scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)
order.line_items.each do |li|
li.variant.scope_to_hub order.distributor
scoper.scope(li.variant)
end
end