mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add ProductProxy which wraps the product's variants in VariantProxys
This commit is contained in:
18
lib/open_food_network/product_proxy.rb
Normal file
18
lib/open_food_network/product_proxy.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module OpenFoodNetwork
|
||||
class ProductProxy
|
||||
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
|
||||
|
||||
def initialize(product, hub)
|
||||
@product = product
|
||||
@hub = hub
|
||||
end
|
||||
|
||||
def variants
|
||||
@product.variants.map { |v| VariantProxy.new(v, @hub) }
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
@product.send(name, *args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
27
spec/lib/open_food_network/product_proxy_spec.rb
Normal file
27
spec/lib/open_food_network/product_proxy_spec.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require 'open_food_network/product_proxy'
|
||||
require 'open_food_network/variant_proxy'
|
||||
|
||||
module OpenFoodNetwork
|
||||
describe ProductProxy do
|
||||
let(:hub) { double(:hub) }
|
||||
let(:p) { double(:product, name: 'name') }
|
||||
let(:pp) { ProductProxy.new(p, hub) }
|
||||
|
||||
describe "delegating calls to proxied product" do
|
||||
it "delegates name" do
|
||||
pp.name.should == 'name'
|
||||
end
|
||||
end
|
||||
|
||||
describe "fetching the variants" do
|
||||
let(:v1) { double(:variant) }
|
||||
let(:v2) { double(:variant) }
|
||||
let(:p) { double(:product, variants: [v1, v2]) }
|
||||
|
||||
it "returns variants wrapped in VariantProxy" do
|
||||
# #class is proxied too, so we test that it worked by #object_id
|
||||
pp.variants.map(&:object_id).sort.should_not == [v1.object_id, v2.object_id].sort
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user