Class: Pagy::Calendar
- 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/quarter.rb,
lib/pagy/calendar/month_mixin.rb
Overview
:nodoc:
Defined Under Namespace
Modules: MonthMixin Classes: Day, Month, 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]
- DAY =
rubocop:disable Style/MutableConstant
60 * 60 * 24
- WEEK =
One day in seconds
DAY * 7
Constants inherited from Pagy
DEFAULT, ElasticsearchRails, LABEL_PLACEHOLDER, Meilisearch, PAGE_PLACEHOLDER, Searchkick, VERSION
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
One week in seconds.
Attributes inherited from Pagy
#count, #from, #in, #items, #last, #next, #offset, #page, #pages, #params, #prev, #to, #vars
Class Method Summary collapse
-
.create(unit, vars) ⇒ Object
Create a subclass instance by unit name (internal use).
Instance Method Summary collapse
-
#initialize(vars) ⇒ Calendar
constructor
Merge and validate the options, do some simple arithmetic and set a few instance variables.
-
#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).
-
#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).
Methods included from OverflowExtra::Pagy
Methods included from FrontendHelpers::Calendar
Methods inherited from Pagy
Methods included from SearchkickExtra::Pagy
Methods included from MeilisearchExtra::Pagy
Methods included from ElasticsearchRailsExtra::Pagy
Methods included from GearboxExtra
#setup_items_var, #setup_pages_var
Methods included from FrontendHelpers::Pagy
Constructor Details
#initialize(vars) ⇒ Calendar
Merge and validate the options, do some simple arithmetic and set a few instance variables
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pagy/calendar.rb', line 17 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 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
#order ⇒ Object (readonly)
One week in seconds
14 15 16 |
# File 'lib/pagy/calendar.rb', line 14 def order @order end |
Class Method Details
.create(unit, vars) ⇒ Object
Create a subclass instance by unit name (internal use)
79 80 81 82 83 84 85 |
# File 'lib/pagy/calendar.rb', line 79 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)
32 33 34 |
# File 'lib/pagy/calendar.rb', line 32 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)
37 38 39 40 |
# File 'lib/pagy/calendar.rb', line 37 def label_for(page, opts = {}) opts[:format] ||= @vars[:format] localize(starting_time_for(page.to_i), opts) # page could be a string end |