mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Change OC :suppliers and :distributors to associations
This commit is contained in:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user