Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Last active December 17, 2015 21:18
Show Gist options
  • Save brandon-beacher/5673338 to your computer and use it in GitHub Desktop.
Save brandon-beacher/5673338 to your computer and use it in GitHub Desktop.
class ShipmentCycle < ActiveRecord::Base
belongs_to :plan, inverse_of: :shipment_cycles
has_many :gift_subscriptions, inverse_of: :shipment_cycle
has_many :plan_allotments, inverse_of: :shipment_cycle
has_many :subscriptions, inverse_of: :shipment_cycle
has_one :assortment, through: :plan
validates :phase,
presence: true
validates :plan_id,
presence: true
def next_shipment(date = Date.current)
nearest_shipment(date, days: 1)
end
def previous_shipment(date = Date.current)
nearest_shipment(date, days: -1)
end
private
def nearest_shipment(date = Date.current, options = { days: 1 })
begin
recurrence = Recurrence.new(plan.days_between_shipments, date)
date = date.advance(options)
end until recurrence.phase == phase
Shipment.new(self, recurrence.date)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment