Files
openfoodnetwork/app/models/schedule.rb
Maikel Linke 1364b878fe Add ApplicationRecord for customisations
Rails 5 introduced this new class to confine application-specific monkey
patches to our models only, and not leak into other libraries using
ActiveRecord::Base.

https://bigbinary.com/blog/application-record-in-rails-5
2021-04-15 15:59:03 +10:00

14 lines
596 B
Ruby

class Schedule < ApplicationRecord
has_paper_trail meta: { custom_data: proc { |schedule| schedule.order_cycle_ids.to_s } }
has_many :order_cycle_schedules, dependent: :destroy
has_many :order_cycles, through: :order_cycle_schedules
has_many :coordinators, -> { distinct }, through: :order_cycles
scope :with_coordinator, lambda { |enterprise| joins(:order_cycles).where('coordinator_id = ?', enterprise.id).select('DISTINCT schedules.*') }
def current_or_next_order_cycle
order_cycles.where('orders_close_at > (?)', Time.zone.now).order('orders_close_at ASC').first
end
end