Class: Pagy::Calendar
- Defined in:
- lib/pagy/calendar.rb
Overview
Paginate a Time period by units (year, month, week or day)
Constant Summary collapse
- DAY =
60 * 60 * 24
- WEEK =
DAY * 7
Constants inherited from Pagy
DEFAULT, ElasticsearchRails, LABEL_PLACEHOLDER, Meilisearch, PAGE_PLACEHOLDER, Searchkick, VERSION
Instance Attribute Summary collapse
-
#count ⇒ Object
writeonly
Sets the attribute count.
-
#in ⇒ Object
writeonly
Sets the attribute in.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#utc_from ⇒ Object
readonly
Returns the value of attribute utc_from.
-
#utc_to ⇒ Object
readonly
Returns the value of attribute utc_to.
-
#week_offset ⇒ Object
readonly
Returns the value of attribute week_offset.
Attributes inherited from Pagy
#count, #from, #in, #items, #last, #next, #offset, #page, #pages, #params, #prev, #to, #vars
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
Generate a label for each page, with the specific ‘Time` period it refers to (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
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pagy/calendar.rb', line 23 def initialize(vars) # rubocop:disable Lint/MissingSuper normalize_vars(vars) setup_vars(page: 1, week_offset: 0) 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
#count=(value) ⇒ Object (writeonly)
Sets the attribute count
20 21 22 |
# File 'lib/pagy/calendar.rb', line 20 def count=(value) @count = value end |
#in=(value) ⇒ Object (writeonly)
Sets the attribute in
20 21 22 |
# File 'lib/pagy/calendar.rb', line 20 def in=(value) @in = value end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
19 20 21 |
# File 'lib/pagy/calendar.rb', line 19 def order @order end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
19 20 21 |
# File 'lib/pagy/calendar.rb', line 19 def unit @unit end |
#utc_from ⇒ Object (readonly)
Returns the value of attribute utc_from.
19 20 21 |
# File 'lib/pagy/calendar.rb', line 19 def utc_from @utc_from end |
#utc_to ⇒ Object (readonly)
Returns the value of attribute utc_to.
19 20 21 |
# File 'lib/pagy/calendar.rb', line 19 def utc_to @utc_to end |
#week_offset ⇒ Object (readonly)
Returns the value of attribute week_offset.
19 20 21 |
# File 'lib/pagy/calendar.rb', line 19 def week_offset @week_offset 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)
51 52 53 |
# File 'lib/pagy/calendar.rb', line 51 def label(**opts) label_for(@page, **opts) end |
#label_for(page, **opts) ⇒ Object
Generate a label for each page, with the specific ‘Time` period it refers to (it can pass along the I18n gem opts when it’s used with the i18n extra)
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pagy/calendar.rb', line 36 def label_for(page, **opts) snap = snap(page.to_i) time = case @unit when :year then new_time(@initial.year + snap) when :month then bump_month(@initial, snap) when :week then @initial + (snap * WEEK) when :day then @initial + (snap * DAY) else raise InternalError, "expected @unit to be in [:year, :month, :week, :day]; got #{@unit.inspect}" end opts[:format] ||= @vars[:"#{@unit}_format"] localize(time, **opts) end |