Class: Pagy::Calendar

Inherits:
Pagy
  • Object
show all
Includes:
FrontendHelpers::Calendar, I18nExtra::Calendar, OverflowExtra::Pagy
Defined in:
lib/pagy/calendar.rb,
lib/pagy/calendar.rb,
lib/pagy/calendar/day.rb,
lib/pagy/calendar/week.rb,
lib/pagy/calendar/year.rb,
lib/pagy/calendar/month.rb

Overview

:nodoc:

Direct Known Subclasses

Day, Month, Week, Year

Defined Under Namespace

Classes: Day, Month, Week, Year

Constant Summary collapse

DAY =
60 * 60 * 24
WEEK =
DAY * 7
UNITS =
{ year: Year, month: Month, week: Week, day: Day }.freeze

Constants inherited from Pagy

DEFAULT, ElasticsearchRails, LABEL_PLACEHOLDER, Meilisearch, PAGE_PLACEHOLDER, Searchkick, VERSION

Instance Attribute Summary collapse

Attributes inherited from Pagy

#count, #from, #in, #items, #last, #next, #offset, #page, #pages, #params, #prev, #to, #vars

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OverflowExtra::Pagy

#overflow?

Methods included from FrontendHelpers::Calendar

#label_sequels

Methods inherited from Pagy

root, #series

Methods included from SearchkickExtra::Pagy

#new_from_searchkick

Methods included from MeilisearchExtra::Pagy

#new_from_meilisearch

Methods included from ElasticsearchRailsExtra::Pagy

#new_from_elasticsearch_rails

Methods included from GearboxExtra

#setup_items_var, #setup_pages_var

Methods included from FrontendHelpers::Pagy

#label_sequels, #sequels

Constructor Details

#initialize(vars) ⇒ Calendar

Merge and validate the options, do some simple arithmetic and set a few instance variables

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pagy/calendar.rb', line 15

def initialize(vars) # rubocop:disable Lint/MissingSuper
  raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)

  normalize_vars(vars)
  @vars = self.class::DEFAULT.merge(@vars)
  setup_vars(page: 1)
  setup_unit_vars
  setup_params_var
  raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last

  @prev = (@page - 1 unless @page == 1)
  @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



12
13
14
# File 'lib/pagy/calendar.rb', line 12

def order
  @order
end

Class Method Details

.create(unit, vars) ⇒ Object

Create a subclass instance by unit name (internal use)

Raises:



83
84
85
86
87
# File 'lib/pagy/calendar.rb', line 83

def self.create(unit, vars)
  raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)

  UNITS[unit].new(vars)
end

Instance Method Details

#active_periodObject

Period of the active page (used for nested units)



41
42
43
# File 'lib/pagy/calendar.rb', line 41

def active_period
  [[@starting, @from].max, [@to - DAY, @ending].min] # include only last unit day
end

#label(**opts) ⇒ Object

The label for the current page (it can pass along the I18n gem opts when it’s used with the i18n extra)



30
31
32
# File 'lib/pagy/calendar.rb', line 30

def label(**opts)
  label_for(@page, **opts)
end

#label_for(page, **opts) ⇒ Object

The label for any page (it can pass along the I18n gem opts when it’s used with the i18n extra)



35
36
37
38
# File 'lib/pagy/calendar.rb', line 35

def label_for(page, **opts)
  opts[:format] ||= @vars[:format]
  localize(start_for(page.to_i), **opts)
end