Change OC :suppliers and :distributors to associations

This commit is contained in:
Kristina Lim
2019-05-06 10:24:44 +08:00
parent 71bff0dd5b
commit 781afc1015

View File

@@ -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).