Class: Pagy::Calendar

Inherits:
Pagy
  • Object
show all
Includes:
I18nExtra::CalendarOverride, JSTools::CalendarOverride, OverflowExtra::PagyOverride
Defined in:
lib/pagy/calendar.rb,
lib/pagy/calendar/day.rb,
lib/pagy/calendar/week.rb,
lib/pagy/calendar/year.rb,
lib/pagy/calendar/month.rb,
lib/pagy/calendar/helper.rb,
lib/pagy/calendar/quarter.rb

Overview

:nodoc:

Direct Known Subclasses

Day, Month, Quarter, Week, Year

Defined Under Namespace

Classes: Day, Helper, Month, OutOfRangeError, Quarter, 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]

Constants inherited from Pagy

DEFAULT, ElasticsearchRails, LABEL_TOKEN, Meilisearch, PAGE_TOKEN, Searchkick, VERSION

Instance Attribute Summary collapse

Attributes inherited from Pagy

#count, #in, #items, #last, #next, #offset, #page, #prev, #vars

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSTools::CalendarOverride

#label_sequels

Methods included from OverflowExtra::PagyOverride

#overflow?

Methods inherited from Pagy

root, #series

Methods included from SearchkickExtra::PagyExtension

#new_from_searchkick

Methods included from MeilisearchExtra::PagyExtension

#new_from_meilisearch

Methods included from ElasticsearchRailsExtra::PagyAddOn

#new_from_elasticsearch_rails

Methods included from GearboxExtra

#setup_items_var, #setup_last_var, #setup_offset_var

Methods included from JSTools::PagyAddOn

#label_sequels, #sequels

Constructor Details

#initialize(vars) ⇒ Calendar

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

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pagy/calendar.rb', line 24

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)

  vars = self.class::DEFAULT.merge(vars)  # subclass specific default
  normalize_vars(vars)                    # general default
  setup_vars(page: 1)
  setup_unit_vars
  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

#fromObject (readonly)

rubocop:disable Style/MutableConstant



21
22
23
# File 'lib/pagy/calendar.rb', line 21

def from
  @from
end

#orderObject (readonly)

rubocop:disable Style/MutableConstant



21
22
23
# File 'lib/pagy/calendar.rb', line 21

def order
  @order
end

#toObject (readonly)

rubocop:disable Style/MutableConstant



21
22
23
# File 'lib/pagy/calendar.rb', line 21

def to
  @to
end

Class Method Details

.create(unit, vars) ⇒ Object

Create a subclass instance by unit name (internal use)

Raises:



114
115
116
117
118
119
120
# File 'lib/pagy/calendar.rb', line 114

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

  name    = unit.to_s
  name[0] = name[0].capitalize
  Object.const_get("Pagy::Calendar::#{name}").new(vars)
end

Instance Method Details

#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)



38
39
40
# File 'lib/pagy/calendar.rb', line 38

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)



43
44
45
46
# File 'lib/pagy/calendar.rb', line 43

def label_for(page, opts = {})
  opts[:format] ||= @vars[:format]
  localize(starting_time_for(page.to_i), opts)  # page could be a string
end