From 781afc10155f09fb49d382a5504e825ae3b6ddfb Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Mon, 6 May 2019 10:24:44 +0800 Subject: [PATCH] Change OC :suppliers and :distributors to associations --- app/models/order_cycle.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index c37a2118a0..071ee49f22 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -8,6 +8,14 @@ class OrderCycle < ActiveRecord::Base has_many :exchanges, :dependent => :destroy + # These scope names are prepended with "cached_" because there are existing accessor methods + # :incoming_exchanges and :outgoing_exchanges. Future work can be done to name these properly. + has_many :cached_incoming_exchanges, conditions: { incoming: true }, class_name: "Exchange" + has_many :cached_outgoing_exchanges, conditions: { incoming: false }, class_name: "Exchange" + + has_many :suppliers, source: :sender, through: :cached_incoming_exchanges, uniq: true + has_many :distributors, source: :receiver, through: :cached_outgoing_exchanges, uniq: true + has_and_belongs_to_many :schedules, join_table: 'order_cycle_schedules' # TODO: DRY the incoming/outgoing clause used in several cases below @@ -124,16 +132,6 @@ class OrderCycle < ActiveRecord::Base oc.reload end - def suppliers - enterprise_ids = self.exchanges.incoming.pluck :sender_id - Enterprise.where('enterprises.id IN (?)', enterprise_ids) - end - - def distributors - enterprise_ids = self.exchanges.outgoing.pluck :receiver_id - Enterprise.where('enterprises.id IN (?)', enterprise_ids) - end - def variants Spree::Variant. joins(:exchanges).