diff --git a/lib/open_food_network/variant_proxy.rb b/lib/open_food_network/variant_proxy.rb new file mode 100644 index 0000000000..9764cfde06 --- /dev/null +++ b/lib/open_food_network/variant_proxy.rb @@ -0,0 +1,19 @@ +module OpenFoodNetwork + class VariantProxy + instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ } + + def initialize(variant, hub) + @variant = variant + @hub = hub + end + + def price + VariantOverride.price_for(@variant, @hub) || @variant.price + end + + + def method_missing(name, *args, &block) + @variant.send(name, *args, &block) + end + end +end diff --git a/spec/lib/open_food_network/variant_proxy_spec.rb b/spec/lib/open_food_network/variant_proxy_spec.rb new file mode 100644 index 0000000000..6439c502a5 --- /dev/null +++ b/spec/lib/open_food_network/variant_proxy_spec.rb @@ -0,0 +1,27 @@ +require 'open_food_network/variant_proxy' + +module OpenFoodNetwork + describe VariantProxy do + let(:hub) { double(:hub) } + let(:v) { double(:variant, sku: 'sku123', price: 'global price') } + let(:vp) { VariantProxy.new(v, hub) } + + describe "delegating calls to proxied variant" do + it "delegates sku" do + vp.sku.should == 'sku123' + end + end + + describe "looking up the price" do + it "returns the override price when there is one" do + VariantOverride.stub(:price_for) { 'override price' } + vp.price.should == 'override price' + end + + it "returns the global price otherwise" do + VariantOverride.stub(:price_for) { nil } + vp.price.should == 'global price' + end + end + end +end