From 08fdc8a5bd6907da7a482009af7870f0e01e3720 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 29 Jun 2016 12:30:43 +1000 Subject: [PATCH] Load producers of all or selected order cycles into shop --- .../filters/filter_products.js.coffee | 6 +++--- .../darkswarm/services/dereferencer.js.coffee | 18 +++++++++++++----- .../darkswarm/services/enterprises.js.coffee | 18 +++++++----------- app/helpers/injection_helper.rb | 7 ++++++- app/models/enterprise.rb | 16 ++++++++++++++++ .../consumer/shopping/shopping_spec.rb | 18 +++++++++--------- spec/support/request/shop_workflow.rb | 9 +++++++++ 7 files changed, 63 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee b/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee index c01a941659..f65c8854dc 100644 --- a/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee +++ b/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee @@ -1,7 +1,7 @@ -Darkswarm.filter 'products', (Matcher)-> - (products, text)-> +Darkswarm.filter 'products', (Matcher) -> + (products, text) -> products ||= [] text ?= "" - products.filter (product)=> + products.filter (product) => propertiesToMatch = [product.name, product.supplier.name, product.primary_taxon.name] Matcher.match propertiesToMatch, text diff --git a/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee b/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee index dbe092b0be..40f195c6f5 100644 --- a/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee +++ b/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee @@ -1,8 +1,16 @@ Darkswarm.factory 'Dereferencer', -> new class Dereferencer dereference: (array, data) -> - if array - for object, i in array - key = undefined - key = object.id if object - array[i] = data[key] + @dereference_from(array, array, data) + + dereference_from: (source, target, data) -> + unreferenced = [] + if source && target + for object, i in source + key = if object then object.id else undefined + if data.hasOwnProperty(key) + target[i] = data[key] + else + delete target[i] + unreferenced[i] = source[i] + unreferenced diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index b030c6e723..6473b58442 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -23,18 +23,14 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, @dereferenceEnterprise enterprise dereferenceEnterprise: (enterprise) -> - # keep a backup of enterprise ids + # keep unreferenced enterprise ids # in case we dereference again after adding more enterprises - if enterprise.hub_references - enterprise.hubs = enterprise.hub_references.slice() - else - enterprise.hub_references = enterprise.hubs.slice() - if enterprise.producer_references - enterprise.producers = enterprise.producer_references.slice() - else - enterprise.producer_references = enterprise.producers.slice() - Dereferencer.dereference enterprise.hubs, @enterprises_by_id - Dereferencer.dereference enterprise.producers, @enterprises_by_id + hubs = enterprise.unreferenced_hubs || enterprise.hubs + enterprise.unreferenced_hubs = + Dereferencer.dereference_from hubs, enterprise.hubs, @enterprises_by_id + producers = enterprise.unreferenced_producers || enterprise.producers + enterprise.unreferenced_producers = + Dereferencer.dereference_from producers, enterprise.producers, @enterprises_by_id dereferenceTaxons: -> for enterprise in @enterprises diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index d76f37bf62..6432597632 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -6,7 +6,12 @@ module InjectionHelper end def inject_shop_enterprises - inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data + ocs = if current_order_cycle + [current_order_cycle] + else + OrderCycle.not_closed.with_distributor(current_distributor) + end + inject_json_ams "enterprises", current_distributor.relatives_and_oc_producers(ocs).activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data end def inject_group_enterprises diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 554529d7f2..eca5a44da2 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -220,6 +220,22 @@ class Enterprise < ActiveRecord::Base ", self.id, self.id) end + def relatives_and_oc_producers(order_cycles) + enterprise_ids = [] + order_cycles.each do |oc| + enterprise_ids += oc.exchanges.incoming.pluck :sender_id + end + Enterprise.where(" + enterprises.id IN + (SELECT child_id FROM enterprise_relationships WHERE enterprise_relationships.parent_id=?) + OR enterprises.id IN + (SELECT parent_id FROM enterprise_relationships WHERE enterprise_relationships.child_id=?) + OR enterprises.id IN + (?) + OR enterprises.id = ? + ", id, id, enterprise_ids, id) + end + def relatives_including_self Enterprise.where(id: relatives.pluck(:id) | [id]) end diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index 7dc9f474da..b449b4d083 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -34,7 +34,7 @@ feature "As a consumer I want to shop with a distributor", js: true do it "shows the producers for a distributor" do exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) - exchange.variants << variant + add_variant_to_order_cycle(exchange, variant) visit shop_path find("#tab_producers a").click @@ -68,7 +68,7 @@ feature "As a consumer I want to shop with a distributor", js: true do it "shows products after selecting an order cycle" do variant.update_attribute(:display_name, "kitten") variant.update_attribute(:display_as, "rabbit") - exchange1.variants << variant ## add product to exchange + add_variant_to_order_cycle(exchange1, variant) visit shop_path page.should_not have_content product.name Spree::Order.last.order_cycle.should == nil @@ -89,8 +89,8 @@ feature "As a consumer I want to shop with a distributor", js: true do it "shows the correct fees after selecting and changing an order cycle" do enterprise_fee = create(:enterprise_fee, amount: 1001) exchange2.enterprise_fees << enterprise_fee - exchange2.variants << variant - exchange1.variants << variant + add_variant_to_order_cycle(exchange2, variant) + add_variant_to_order_cycle(exchange1, variant) # -- Selecting an order cycle visit shop_path @@ -116,8 +116,8 @@ feature "As a consumer I want to shop with a distributor", js: true do describe "declining to clear the cart" do before do - exchange2.variants << variant - exchange1.variants << variant + add_variant_to_order_cycle(exchange2, variant) + add_variant_to_order_cycle(exchange1, variant) visit shop_path select "turtles", from: "order_cycle_id" @@ -147,9 +147,9 @@ feature "As a consumer I want to shop with a distributor", js: true do before do exchange.update_attribute :pickup_time, "frogs" - exchange.variants << variant - exchange.variants << variant1 - exchange.variants << variant2 + add_variant_to_order_cycle(exchange, variant) + add_variant_to_order_cycle(exchange, variant1) + add_variant_to_order_cycle(exchange, variant2) order.order_cycle = oc1 end diff --git a/spec/support/request/shop_workflow.rb b/spec/support/request/shop_workflow.rb index 279ead2630..d7654617e1 100644 --- a/spec/support/request/shop_workflow.rb +++ b/spec/support/request/shop_workflow.rb @@ -29,6 +29,15 @@ module ShopWorkflow end def add_variant_to_order_cycle(exchange, variant) + oc = exchange.order_cycle + supplier = variant.product.supplier + # An order cycle needs an incoming exchange for a supplier + # before having its products. Otherwise the data will be inconsistent and + # and not all needed enterprises are loaded into the shop page. + if oc.exchanges.from_enterprise(supplier).incoming.empty? + create(:exchange, order_cycle: oc, incoming: true, + sender: supplier, receiver: oc.coordinator) + end exchange.variants << variant end