2
2
3
3
class Pagy
4
4
# Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
5
- # Additions for the Backend module
6
- module CalendarBackendAddOn
7
- CALENDAR_CONF_KEYS = ( Offset ::Calendar ::UNITS + %i[ pagy active ] ) . freeze
8
-
5
+ Backend . module_eval do
9
6
private
10
7
11
8
# Take a collection and a conf Hash with keys in CONF_KEYS and return an array with 3 items: [calendar, pagy, results]
12
9
def pagy_calendar ( collection , conf )
13
- raise ArgumentError , "keys must be in #{ CALENDAR_CONF_KEYS . inspect } " \
14
- unless conf . is_a? ( Hash ) && ( conf . keys - CALENDAR_CONF_KEYS ) . empty?
10
+ conf_keys = Offset ::Calendar ::UNITS + %i[ pagy active ]
11
+ raise ArgumentError , "keys must be in #{ conf_keys . inspect } " \
12
+ unless conf . is_a? ( Hash ) && ( conf . keys - conf_keys ) . empty?
15
13
16
14
conf [ :pagy ] ||= { }
17
15
unless conf . key? ( :active ) && !conf [ :active ]
@@ -36,39 +34,4 @@ def pagy_calendar_filter(*)
36
34
'(see https://ddnexus.github.io/pagy/docs/extras/calendar/#pagy-calendar-filter-collection-from-to)'
37
35
end
38
36
end
39
- Backend . prepend CalendarBackendAddOn
40
-
41
- # Override the pagy_anchor
42
- module CalendarFrontendOverride
43
- # Consider the vars[:counts]
44
- def pagy_anchor ( pagy , anchor_string : nil , **vars )
45
- return super unless ( counts = pagy . vars [ :counts ] )
46
-
47
- anchor_string &&= %( #{ anchor_string } )
48
- left , right = %(<a#{ anchor_string } href="#{ pagy_page_url ( pagy , PAGE_TOKEN , **vars ) } ") . split ( PAGE_TOKEN , 2 )
49
- # lambda used by all the helpers
50
- lambda do |page , text = pagy . label ( page : page ) , classes : nil , aria_label : nil |
51
- count = counts [ page - 1 ]
52
- if count . zero?
53
- classes = "#{ classes && ( classes + ' ' ) } empty-page"
54
- info_key = 'pagy.info.no_items'
55
- else
56
- info_key = 'pagy.info.single_page'
57
- end
58
- title = %( title="#{ pagy_t ( info_key , item_name : pagy_t ( 'pagy.item_name' , count :) , count :) } ")
59
- classes &&= %( class="#{ classes } ")
60
- aria_label &&= %( aria-label="#{ aria_label } ")
61
- %(#{ left } #{ page } #{ right } #{ title } #{ classes } #{ aria_label } >#{ text } </a>)
62
- end
63
- end
64
- end
65
- Frontend . prepend CalendarFrontendOverride
66
-
67
- # Additions for the Frontend module
68
- Url . module_eval do
69
- # Return the url for the calendar page at time
70
- def pagy_calendar_url_at ( calendar , time , **)
71
- pagy_page_url ( calendar . send ( :calendar_at , time , **) , 1 , **)
72
- end
73
- end
74
37
end
0 commit comments