Class: Pagy::Calendar

Inherits:
Hash
  • Object
show all
Defined in:
lib/pagy/classes/calendar/year.rb,
lib/pagy/classes/calendar/day.rb,
lib/pagy/classes/calendar/unit.rb,
lib/pagy/classes/calendar/week.rb,
lib/pagy/classes/calendar/month.rb,
lib/pagy/classes/calendar/quarter.rb,
lib/pagy/classes/calendar/calendar.rb

Overview

Calendar class

Defined Under Namespace

Classes: Day, Month, Quarter, Unit, Week, Year

Constant Summary collapse

UNITS =

List of units in desc order of duration. It can be used for custom units.

%i[year quarter month week day]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.localize_with_rails_i18n_gem(*locales) ⇒ Object

:nocov: Localize with rails-i18n in any env

Raises:



26
27
28
29
30
31
32
33
# File 'lib/pagy/classes/calendar/calendar.rb', line 26

def localize_with_rails_i18n_gem(*locales)
  Unit.prepend(Module.new { def localize(...) = ::I18n.localize(...) })
  raise RailsI18nLoadError, "Pagy: The gem 'rails-i18n' must be installed if you don't use Rails" \
        unless (path = Gem.loaded_specs['rails-i18n']&.full_gem_path)

  path = Pathname.new(path)
  ::I18n.load_path += locales.map { |locale| path.join("rails/locale/#{locale}.yml") }
end

Instance Method Details

#showtimeObject

Return the current time of the smallest time unit shown



45
46
47
# File 'lib/pagy/classes/calendar/calendar.rb', line 45

def showtime
  self[@units.last].from
end

#url_at(time) ⇒ Object

Return the url for the calendar (shortest unit) page at time



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pagy/classes/calendar/calendar.rb', line 50

def url_at(time, **)
  conf      = Marshal.load(Marshal.dump(@conf))
  page_keys = {}
  @units.inject(nil) do |object, unit|
    conf[unit][:period]  = object&.send(:active_period) || @period
    conf[unit][:page]    = page_keys["#{unit}_#{@page_key}"] \
                         = create(unit, **conf[unit]).send(:page_at, time, **)
    conf[unit][:querify] = ->(params) { params.merge!(page_keys) }
    create(unit, **conf[unit])
  end.send(:compose_page_url, 1, **)
end