module Pagy::Calendar::MonthMixin

def add_to(time, months)

Add months to time
def add_to(time, months)
  months += months_in(time)
  year  = months / 12
  month = months % 12
  month.zero? ? new_time(year - 1, 12) : new_time(year, month)
end

def beginning_month(month)

Return the beginning month for the unit (e.g. quarter) that includes the month argument
def beginning_month(month)
  (@months * ((month - 1) / @months)) + 1
end

def months_in(time)

Months in time
def months_in(time)
  (time.year * 12) + time.month
end

def setup_unit_vars

Setup the calendar variables
def setup_unit_vars
  super
  @months  = self.class::MONTHS  # number of months in the unit
  @initial = new_time(@starting.year, beginning_month(@starting.month))
  @final   = add_to(new_time(@ending.year, beginning_month(@ending.month)), @months)
  @pages   = @last = (months_in(@final) - months_in(@initial)) / @months
  @from    = start_for(@page)
  @to      = add_to(@from, @months)
end

def start_for(page)

Time for the page
def start_for(page)
  add_to(@initial, snap(page) * @months)
end