module Pagy::Calendar::MonthMixin
def add_to(time, months)
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)
def beginning_month(month) (@months * ((month - 1) / @months)) + 1 end
def months_in(time)
def months_in(time) (time.year * 12) + time.month end
def setup_unit_vars
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)
def start_for(page) add_to(@initial, snap(page) * @months) end