Resources

Summary

# Gemfile
gem 'ice_cube'

# event.rb
class Event < ApplicationRecord
  enum occurrence: { biweekly: 0, monthly: 1, annually: 2 }

  def schedule
    # Not covered in the video, but in these situations,
    # I'll most likely call schedule multiple times in a 
    # complex view, so I will use memoization for the schedule
    @schedule ||= begin
      schedule = IceCube::Schedule.new(now = start_date)
      case occurrence      
      when 'biweekly'
        schedule.add_recurrence_rule IceCube::Rule.weekly(2)
      when 'monthly'
        schedule.add_recurrence_rule IceCube::Rule.monthly(1)
      when 'annually'
        schedule.add_recurrence_rule IceCube::Rule.yearly(1)
      end
      schedule
    end
  end
end

# rails console
start_date = Date.new(2017,1,1)
end_date = Date.new(2017,12,31)
event = Event.create(name: '2017 Calendar', start_date: start_date, end_date: end_date)
event.schedule.occurrences(end_date)

event.schedule
    => #[#], :base_wday=>[#], :base_hour=>[#], :base_min=>[#], :base_sec=>[#]}, @interval=2, @week_start=:sunday, @time=nil, @start_time=nil, @uses=0>], @all_exception_rules=[]>

event.schedule.occurrences(end_date)
    => [2017-01-01 00:00:00 -0500, 2017-01-15 00:00:00 -0500, 2017-01-29 00:00:00 -0500, 2017-02-12 00:00:00 -0500, 2017-02-26 00:00:00 -0500, 2017-03-12 00:00:00 -0500, 2017-03-26 00:00:00 -0400, 2017-04-09 00:00:00 -0400, 2017-04-23 00:00:00 -0400, 2017-05-07 00:00:00 -0400, 2017-05-21 00:00:00 -0400, 2017-06-04 00:00:00 -0400, 2017-06-18 00:00:00 -0400, 2017-07-02 00:00:00 -0400, 2017-07-16 00:00:00 -0400, 2017-07-30 00:00:00 -0400, 2017-08-13 00:00:00 -0400, 2017-08-27 00:00:00 -0400, 2017-09-10 00:00:00 -0400, 2017-09-24 00:00:00 -0400, 2017-10-08 00:00:00 -0400, 2017-10-22 00:00:00 -0400, 2017-11-05 00:00:00 -0400, 2017-11-19 00:00:00 -0500, 2017-12-03 00:00:00 -0500, 2017-12-17 00:00:00 -0500, 2017-12-31 00:00:00 -0500]

event.schedule.previous_occurrence(Date.today)
    => 2017-07-30 00:00:00 -0400

event.schedule.previous_occurrence(Date.today)
    => 2017-07-30 00:00:00 -0400

event.schedule.next_occurrence(Date.today)
    => 2017-08-27 00:00:00 -0400

event.schedule.next_occurrences(4, Date.today)
    => [2017-08-27 00:00:00 -0400, 2017-09-10 00:00:00 -0400, 2017-09-24 00:00:00 -0400, 2017-10-08 00:00:00 -0400]