Extract property merging to lib class

This commit is contained in:
Rohan Mitchell
2016-06-17 11:14:39 +10:00
parent aae1689a27
commit 58379a5e28
5 changed files with 52 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
require 'open_food_network/permalink_generator'
require 'open_food_network/property_merge'
Spree::Product.class_eval do
include PermalinkGenerator
@@ -128,11 +129,7 @@ Spree::Product.class_eval do
ps = product_properties.all
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
ps = OpenFoodNetwork::PropertyMerge.merge(ps, supplier.producer_properties)
end
ps.

View File

@@ -0,0 +1,18 @@
module OpenFoodNetwork
class PropertyMerge
def self.merge(primary, secondary)
primary + secondary.reject do |secondary_p|
primary.any? do |primary_p|
property_of(primary_p).presentation == property_of(secondary_p).presentation
end
end
end
private
def self.property_of(p)
p.respond_to?(:property) ? p.property : p
end
end
end

View File

@@ -259,6 +259,12 @@ FactoryGirl.define do
end
end
factory :producer_property, class: ProducerProperty do
value 'abc123'
producer { create(:supplier_enterprise) }
property
end
factory :customer, :class => Customer do
email { Faker::Internet.email }
enterprise

View File

@@ -0,0 +1,25 @@
require 'spec_helper'
module OpenFoodNetwork
describe PropertyMerge do
let(:p1a) { create(:property, presentation: 'One') }
let(:p1b) { create(:property, presentation: 'One') }
let(:p2) { create(:property, presentation: 'Two') }
describe "merging Spree::Properties" do
it "merges properties" do
expect(PropertyMerge.merge([p1a], [p1b, p2])).to eq [p1a, p2]
end
end
describe "merging ProducerProperties and Spree::ProductProperties" do
let(:pp1a) { create(:product_property, property: p1a) }
let(:pp1b) { create(:producer_property, property: p1b) }
let(:pp2) { create(:producer_property, property: p2) }
it "merges properties" do
expect(PropertyMerge.merge([pp1a], [pp1b, pp2])).to eq [pp1a, pp2]
end
end
end
end

View File

@@ -420,7 +420,7 @@ module Spree
end
end
context "when product has an inherit_properties value set to true" do
context "when product has an inherit_properties value set to false" do
let(:supplier) { create(:supplier_enterprise) }
let(:product) { create(:simple_product, supplier: supplier, inherits_properties: false) }