Skip to content

Commit

Permalink
Simplify calendar code
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Oct 29, 2024
1 parent 2d41469 commit f521502
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/extras/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ option `fit_time: true` to avoid the error and get the url to the page closest t

Each page link in the calendar navs is conveniently labeled with the specific `Time` period it refers to. You can change the time
format to your needs by setting the `:format` variable to a standard `strftime` format. (See
the [Pagy::Calendar variables](/docs/api/calendar.md#variables))
the [Pagy::Calendar::Unit variables](/docs/api/calendar/units.md#variables))

You can also get the [label method](/docs/api/calendar.md#methods) with e.g.: `@calendar[:month].label`, which might be useful to
use in your UI.
Expand Down
20 changes: 11 additions & 9 deletions gem/lib/pagy/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def create(unit, **vars)
end

# Return calendar, from, to
def init(conf, period, params)
new.send(:init, conf, period, params)
def init(...)
new.send(:init, ...)
end
end

Expand All @@ -47,18 +47,20 @@ def init(conf, period, params)
@period = period
@params = params
@page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
@units.each do |unit| # set all the :page_param vars for later deletion
unit_page_param = :"#{unit}_#{@page_param}"
conf[unit][:page_param] = unit_page_param
conf[unit][:page] = @params[unit_page_param]
end
# set all the :page_param vars for later deletion
@units.each { |unit| conf[unit][:page_param] = :"#{unit}_#{@page_param}" }
calendar = {}
object = nil
@units.each_with_index do |unit, index|
params_to_delete = @units[(index + 1), @units.size].map { |sub| conf[sub][:page_param] } + [@page_param]
conf[unit][:params] = lambda { |up| up.except(*params_to_delete.map(&:to_s)) } # rubocop:disable Style/Lambda
conf[unit][:params] = ->(up) { up.except(*params_to_delete.map(&:to_s)) }
conf[unit][:period] = object&.send(:active_period) || @period
calendar[unit] = object = Calendar.send(:create, unit, **conf[unit])
conf[unit][:page] = @params[:"#{unit}_#{@page_param}"] # requested page
# :nocov:
conf[unit][:counts] = yield(unit, conf[unit][:period]) if block_given? # nocov doesn't need to fail block_given?
# :nocov:
calendar[unit] = object \
= Calendar.send(:create, unit, **conf[unit])
end
[replace(calendar), object.from, object.to]
end
Expand Down
19 changes: 8 additions & 11 deletions gem/lib/pagy/extras/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ def pagy_calendar(collection, conf)

conf[:pagy] ||= {}
unless conf.key?(:active) && !conf[:active]
calendar, from, to = Calendar.send(:init, conf, pagy_calendar_period(collection), params)
if respond_to?(:pagy_calendar_counts)
calendar.each_key do |unit|
calendar[unit].vars[:counts] = pagy_calendar_counts(collection, unit, *calendar[unit].vars[:period])
end
end
calendar, from, to = Calendar.send(:init, conf, pagy_calendar_period(collection), params) do |unit, period|
pagy_calendar_counts(collection, unit, *period) if respond_to?(:pagy_calendar_counts)
end
collection = pagy_calendar_filter(collection, from, to)
end
pagy, results = send(conf[:pagy][:backend] || :pagy, collection, **conf[:pagy]) # use backend: :pagy when omitted
Expand Down Expand Up @@ -54,14 +51,14 @@ def pagy_anchor(pagy, anchor_string: nil)
left, right = %(<a#{anchor_string} href="#{pagy_url_for(pagy, PAGE_TOKEN)}").split(PAGE_TOKEN, 2)
# lambda used by all the helpers
lambda do |page, text = pagy.label_for(page), classes: nil, aria_label: nil|
count = counts[page - 1]
item_name = pagy_t('pagy.item_name', count:)
count = counts[page - 1]
if count.zero?
classes = "#{classes && (classes + ' ')}empty-page"
title = %( title="#{pagy_t('pagy.info.no_items', item_name:, count:)}")
classes = "#{classes && (classes + ' ')}empty-page"
info_key = 'pagy.info.no_items'
else
title = %( title="#{pagy_t('pagy.info.single_page', item_name:, count:)}")
info_key = 'pagy.info.single_page'
end
title = %( title="#{pagy_t(info_key, item_name: pagy_t('pagy.item_name', count:), count:)}")
classes = %( class="#{classes}") if classes
aria_label = %( aria-label="#{aria_label}") if aria_label
%(#{left}#{page}#{right}#{title}#{classes}#{aria_label}>#{text}</a>)
Expand Down

0 comments on commit f521502

Please sign in to comment.