mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
Refactoring with feedback on pr #1073
This commit is contained in:
@@ -7,7 +7,7 @@ Darkswarm.factory 'Dereferencer', ->
|
||||
unreferenced = []
|
||||
if source && target
|
||||
for object, i in source
|
||||
key = if object then object.id else undefined
|
||||
key = object?.id
|
||||
if data.hasOwnProperty(key)
|
||||
target[i] = data[key]
|
||||
else
|
||||
|
||||
@@ -16,7 +16,8 @@ class EnterprisesController < BaseController
|
||||
def relatives
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
enterprises = Enterprise.find(params[:id]).andand.relatives.activated
|
||||
enterprise = Enterprise.find(params[:id])
|
||||
enterprises = enterprise.andand.relatives.andand.activated
|
||||
render(json: enterprises,
|
||||
each_serializer: Api::EnterpriseSerializer,
|
||||
data: OpenFoodNetwork::EnterpriseInjectionData.new)
|
||||
|
||||
@@ -176,6 +176,16 @@ class Enterprise < ActiveRecord::Base
|
||||
joins(:enterprise_roles).where('enterprise_roles.user_id = ?', user.id)
|
||||
end
|
||||
}
|
||||
scope :relatives_of_one_union_others, lambda { |one, others|
|
||||
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
|
||||
(?)
|
||||
", one, one, others)
|
||||
}
|
||||
|
||||
# Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554
|
||||
def self.distinct_count
|
||||
@@ -221,19 +231,8 @@ class Enterprise < ActiveRecord::Base
|
||||
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)
|
||||
enterprise_ids = Exchange.in_order_cycle(order_cycles).incoming.pluck :sender_id
|
||||
Enterprise.relatives_of_one_union_others(id, enterprise_ids)
|
||||
end
|
||||
|
||||
def relatives_including_self
|
||||
|
||||
@@ -29,19 +29,24 @@ 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
|
||||
ensure_supplier_exchange(exchange, variant.product.supplier)
|
||||
exchange.variants << variant
|
||||
end
|
||||
|
||||
def set_order_cycle(order, order_cycle)
|
||||
order.update_attribute(:order_cycle, order_cycle)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# 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.
|
||||
def ensure_supplier_exchange(exchange, supplier)
|
||||
oc = exchange.order_cycle
|
||||
if oc.exchanges.from_enterprise(supplier).incoming.empty?
|
||||
create(:exchange, order_cycle: oc, incoming: true,
|
||||
sender: supplier, receiver: oc.coordinator)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user