properties_h uses inherit_properties flag, and returns property id rather than presentation

This commit is contained in:
Rob Harrington
2015-04-22 16:06:01 +10:00
parent f610a709e1
commit 976c24cf4c
2 changed files with 41 additions and 12 deletions

View File

@@ -111,15 +111,18 @@ Spree::Product.class_eval do
def properties_h
# Product properties override producer properties
ps = product_properties.all
supplier.producer_properties.each do |producer_property|
unless ps.find { |product_property| product_property.property.presentation == producer_property.property.presentation }
ps << producer_property
if inherits_properties
supplier.producer_properties.each do |producer_property|
unless ps.find { |product_property| product_property.property.presentation == producer_property.property.presentation }
ps << producer_property
end
end
end
ps.
sort_by { |pp| pp.position }.
map { |pp| {presentation: pp.property.presentation, value: pp.value} }
map { |pp| {id: pp.property.id, value: pp.value} }
end
def in_distributor?(distributor)

View File

@@ -8,7 +8,7 @@ module Spree
it { should belong_to(:primary_taxon) }
it { should have_many(:product_distributions) }
end
describe "validations and defaults" do
it "is valid when built from factory" do
build(:product).should be_valid
@@ -122,7 +122,7 @@ module Spree
end
end
end
describe "scopes" do
describe "in_supplier" do
@@ -316,8 +316,9 @@ module Spree
it "returns product properties as a hash" do
product = create(:simple_product)
product.set_property 'Organic Certified', 'NASAA 12345'
property = product.properties.last
product.properties_h.should == [{presentation: 'Organic Certified', value: 'NASAA 12345'}]
product.properties_h.should == [{id: property.id, value: 'NASAA 12345'}]
end
it "returns producer properties as a hash" do
@@ -325,8 +326,9 @@ module Spree
product = create(:simple_product, supplier: supplier)
supplier.set_producer_property 'Organic Certified', 'NASAA 54321'
property = supplier.properties.last
product.properties_h.should == [{presentation: 'Organic Certified', value: 'NASAA 54321'}]
product.properties_h.should == [{id: property.id, value: 'NASAA 54321'}]
end
it "overrides producer properties with product properties" do
@@ -335,8 +337,32 @@ module Spree
product.set_property 'Organic Certified', 'NASAA 12345'
supplier.set_producer_property 'Organic Certified', 'NASAA 54321'
property = product.properties.last
product.properties_h.should == [{presentation: 'Organic Certified', value: 'NASAA 12345'}]
product.properties_h.should == [{id: property.id, value: 'NASAA 12345'}]
end
context "when product has an inherit_properties value set to true" do
let(:supplier) { create(:supplier_enterprise) }
let(:product) { create(:simple_product, supplier: supplier, inherits_properties: true) }
it "inherits producer properties" do
supplier.set_producer_property 'Organic Certified', 'NASAA 54321'
property = supplier.properties.last
product.properties_h.should == [{id: property.id, value: 'NASAA 54321'}]
end
end
context "when product has an inherit_properties value set to true" do
let(:supplier) { create(:supplier_enterprise) }
let(:product) { create(:simple_product, supplier: supplier, inherits_properties: false) }
it "does not inherit producer properties" do
supplier.set_producer_property 'Organic Certified', 'NASAA 54321'
product.properties_h.should == []
end
end
it "sorts by position" do
@@ -352,9 +378,9 @@ module Spree
supplier.producer_properties.create!({property_id: pb.id, value: '2', position: 2}, {without_protection: true})
product.properties_h.should ==
[{presentation: 'A', value: '1'},
{presentation: 'B', value: '2'},
{presentation: 'C', value: '3'}]
[{id: pa.id, value: '1'},
{id: pb.id, value: '2'},
{id: pc.id, value: '3'}]
end
end