-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
scope_variant_to_hub.rb
67 lines (57 loc) · 1.74 KB
/
scope_variant_to_hub.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module OpenFoodNetwork
class ScopeVariantToHub
def initialize(hub, variant_overrides=nil)
@hub = hub
@variant_overrides = variant_overrides || VariantOverride.indexed(@hub)
end
def scope(variant)
variant.send :extend, OpenFoodNetwork::ScopeVariantToHub::ScopeVariantToHub
variant.instance_variable_set :@hub, @hub
variant.instance_variable_set :@variant_override, @variant_overrides[variant]
end
module ScopeVariantToHub
def price
@variant_override.andand.price || super
end
def price_in(currency)
Spree::Price.new(amount: price, currency: currency)
end
def count_on_hand
@variant_override.andand.count_on_hand || super
end
def on_demand
if @variant_override.andand.on_demand.nil?
if @variant_override.andand.count_on_hand.present?
# If we're overriding the stock level of an on_demand variant, show it as not
# on_demand, so our stock control can take effect.
false
else
super
end
else
@variant_override.andand.on_demand
end
end
def decrement!(attribute, by=1)
if attribute == :count_on_hand && @variant_override.andand.stock_overridden?
@variant_override.decrement_stock! by
else
super
end
end
def increment!(attribute, by=1)
if attribute == :count_on_hand && @variant_override.andand.stock_overridden?
@variant_override.increment_stock! by
else
super
end
end
def sku
@variant_override.andand.sku || super
end
def tag_list
@variant_override.andand.tag_list || []
end
end
end
end