pagy 5.4.0 → 5.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32d028f5c02fafb87cc0bb10f1b28ebf1f49b89babf4cfcf2099367b69cf88af
4
- data.tar.gz: c5cf934095bfaed81c66afab017340855b8d7dcc1af5cc80c620e8fb62c70054
3
+ metadata.gz: a5d3090b61a9b11c0763a7c2d103ee36f4a36f0618e7e99b32357bab408a29bc
4
+ data.tar.gz: d5c3c376deda1c84558bbcff43809c9235cb2fc9ced7492ecdfe8df6397c873e
5
5
  SHA512:
6
- metadata.gz: 361df59b13de017f9bc99c7d4ed9c2316c265f0f50514fef3623c33f05095dbdbd8eee4be208bb420a3857ca4070dbebd75e6b30c0fc9ae827b1a7563e288aca
7
- data.tar.gz: ba1ca8e50d010dea86cd05d65969f1815d9056e845dd0f23a17a78df6e59d7692619ae4c6899fd356bf14a8d0a5ff9dbd9ffb8ab545bad3653a10e8de0662823
6
+ metadata.gz: 6591de1571e9b6bdf1a26668cf730dcbba4c83ef9f2d3727b3a1bec5f98c49a69ae2d55aab363444d85a3310e3de65fe119fb3606ca63cfdf56202c14c591568
7
+ data.tar.gz: 02fea4991aab7911107460e58e73a66f507f9fff990261f1536fddf016b9523003165ed0dfd024013282ef43fec221cb7fcfa27b7f6b23c60a742f175adc0eae
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.4.0)
3
+ # Pagy initializer file (5.6.1)
4
4
  # Customize only what you really need and notice that Pagy works also without any of the following lines.
5
5
  # Should you just cherry pick part of this file, please maintain the require-order of the extras
6
6
 
@@ -40,15 +40,33 @@
40
40
  # See https://ddnexus.github.io/pagy/extras/array
41
41
  # require 'pagy/extras/array'
42
42
 
43
- # Calendar extra: Paginate a collection by calendar Time unit (year, month, week or day)
43
+ # Calendar extra: Add pagination filtering by calendar time unit (year, quartr, month, week, day)
44
44
  # See https://ddnexus.github.io/pagy/extras/calendar
45
45
  # require 'pagy/extras/calendar'
46
- # DEFAULT[:year_format] = '%Y' # strftime format for :year unit
47
- # DEFAULT[:month_format] = '%Y-%m' # strftime format for :month unit
48
- # DEFAULT[:week_format] = '%Y-%W' # strftime format for :week unit
49
- # DEFAULT[:day_format] = '%Y-%m-%d' # strftime format for :day unit
50
- # DEFAULT[:week_offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
51
- # DEFAULT[:time_order] = :asc # Time direction of pagination
46
+ # Default for each unit
47
+ # Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination
48
+ # Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format
49
+ #
50
+ # Pagy::Calendar::Quarter::DEFAULT[:order] = :asc # Time direction of pagination
51
+ # Pagy::Calendar::Quarter::DEFAULT[:format] = '%Y-Q%q' # strftime format
52
+ #
53
+ # Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
54
+ # Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
55
+ #
56
+ # Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination
57
+ # Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format
58
+ # Pagy::Calendar::Week::DEFAULT[:offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
59
+ #
60
+ # Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
61
+ # Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
62
+ #
63
+ # Uncomment the following block, if you need calendar localization without using the I18n extra
64
+ # module LocalizePagyCalendar
65
+ # def localize(time, opts)
66
+ # ::I18n.l(time, **opts)
67
+ # end
68
+ # end
69
+ # Pagy::Calendar.prepend LocalizePagyCalendar
52
70
 
53
71
  # Countless extra: Paginate without any count, saving one query per rendering
54
72
  # See https://ddnexus.github.io/pagy/extras/countless
@@ -217,6 +235,5 @@
217
235
  # Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
218
236
 
219
237
 
220
- # When you are done setting your own defaults freeze it,
221
- # so it will not changed accidentally
238
+ # When you are done setting your own default freeze it, so it will not get changed accidentally
222
239
  Pagy::DEFAULT.freeze
@@ -5,7 +5,7 @@
5
5
  // Container of the whole pagy stuff
6
6
  function Pagy(){}
7
7
 
8
- Pagy.version = '5.4.0'
8
+ Pagy.version = '5.6.1'
9
9
 
10
10
  // Used by the waitForMe function
11
11
  Pagy.delay = 100
@@ -0,0 +1,29 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ class Pagy # :nodoc:
5
+ class Calendar # :nodoc:
6
+ # Calendar day subclass
7
+ class Day < Calendar
8
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
+ format: '%Y-%m-%d' }
10
+
11
+ protected
12
+
13
+ # Setup the calendar variables
14
+ def setup_unit_vars
15
+ super
16
+ @initial = new_time(@starting.year, @starting.month, @starting.day)
17
+ @final = new_time(@ending.year, @ending.month, @ending.day) + DAY
18
+ @pages = @last = (@final - @initial).to_i / DAY
19
+ @from = start_for(@page)
20
+ @to = @from + DAY
21
+ end
22
+
23
+ # Time for the page
24
+ def start_for(page)
25
+ @initial + (snap(page) * DAY)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'month_mixin'
5
+
6
+ class Pagy # :nodoc:
7
+ class Calendar # :nodoc:
8
+ # Calendar month subclass
9
+ class Month < Calendar
10
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
11
+ format: '%Y-%m' }
12
+ MONTHS = 1 # number of months in the unit
13
+ include MonthMixin
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pagy
4
+ class Calendar
5
+ # Mixin for month based unit periods
6
+ # It is used for month and quarter, but you could use it to implement less common unit of 6, 4, 2 months
7
+ # (see the https://ddnexus.github.io/pagy/api/calendar#custom-units sections for details).
8
+ # The class that includes it needs to set the MONTH duration for the unit and the usual DEFAULT.
9
+ module MonthMixin
10
+ protected
11
+
12
+ # Setup the calendar variables
13
+ def setup_unit_vars
14
+ super
15
+ @months = self.class::MONTHS # number of months in the unit
16
+ @initial = new_time(@starting.year, beginning_month(@starting.month))
17
+ @final = add_to(new_time(@ending.year, beginning_month(@ending.month)), @months)
18
+ @pages = @last = (months_in(@final) - months_in(@initial)) / @months
19
+ @from = start_for(@page)
20
+ @to = add_to(@from, @months)
21
+ end
22
+
23
+ # Time for the page
24
+ def start_for(page)
25
+ add_to(@initial, snap(page) * @months)
26
+ end
27
+
28
+ # Return the beginning month for the unit (e.g. quarter) that includes the month argument
29
+ def beginning_month(month)
30
+ (@months * ((month - 1) / @months)) + 1
31
+ end
32
+
33
+ private
34
+
35
+ # Months in time
36
+ def months_in(time)
37
+ (time.year * 12) + time.month
38
+ end
39
+
40
+ # Add months to time
41
+ def add_to(time, months)
42
+ months += months_in(time)
43
+ year = months / 12
44
+ month = months % 12
45
+ month.zero? ? new_time(year - 1, 12) : new_time(year, month)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'month_mixin'
5
+
6
+ class Pagy # :nodoc:
7
+ class Calendar # :nodoc:
8
+ # Calendar quarter subclass
9
+ class Quarter < Calendar
10
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
11
+ format: '%Y-Q%q' } # '%q' token
12
+ MONTHS = 3 # number of months of the unit
13
+ include MonthMixin
14
+
15
+ # The label for any page, with the substitution of the '%q' token
16
+ def label_for(page, opts = {})
17
+ start_time = start_for(page.to_i)
18
+ opts[:format] = (opts[:format] || @vars[:format]).gsub('%q') { (start_time.month / 4) + 1 }
19
+ localize(start_time, opts)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ class Pagy # :nodoc:
5
+ class Calendar # :nodoc:
6
+ # Calendar week subclass
7
+ class Week < Calendar
8
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
+ format: '%Y-%W',
10
+ offset: 0 }
11
+
12
+ protected
13
+
14
+ # Setup the calendar variables
15
+ def setup_unit_vars
16
+ setup_vars(offset: 0)
17
+ super
18
+ @initial = week_start(@starting)
19
+ @final = week_start(@ending) + WEEK
20
+ @pages = @last = (@final - @initial).to_i / WEEK
21
+ @from = start_for(@page)
22
+ @to = @from + WEEK
23
+ end
24
+
25
+ # Time for the page
26
+ def start_for(page)
27
+ @initial + (snap(page) * WEEK)
28
+ end
29
+
30
+ private
31
+
32
+ # Return the start of the week for time
33
+ def week_start(time)
34
+ start = time - (((time.wday - @offset) * DAY) % WEEK)
35
+ new_time(start.year, start.month, start.day)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ class Pagy # :nodoc:
5
+ class Calendar # :nodoc:
6
+ # Calendar year subclass
7
+ class Year < Calendar
8
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
+ format: '%Y' }
10
+
11
+ protected
12
+
13
+ # Setup the calendar variables
14
+ def setup_unit_vars
15
+ super
16
+ @initial = new_time(@starting.year)
17
+ @final = new_time(@ending.year + 1)
18
+ @pages = @last = @final.year - @initial.year
19
+ @from = start_for(@page)
20
+ @to = new_time(@from.year + 1)
21
+ end
22
+
23
+ # Time for the page
24
+ def start_for(page)
25
+ new_time(@initial.year + snap(page))
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/pagy/calendar.rb CHANGED
@@ -2,35 +2,23 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'pagy'
5
- require 'date'
6
5
 
7
6
  class Pagy # :nodoc:
8
- DEFAULT[:year_format] = '%Y' # strftime format for :year unit
9
- DEFAULT[:month_format] = '%Y-%m' # strftime format for :month unit
10
- DEFAULT[:week_format] = '%Y-%W' # strftime format for :week unit
11
- DEFAULT[:day_format] = '%Y-%m-%d' # strftime format for :day unit
12
- DEFAULT[:week_offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
13
- DEFAULT[:time_order] = :asc # Time direction of pagination
14
-
15
- # Base class for time units subclasses (Year, Month, Week, Day)
7
+ # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
16
8
  class Calendar < Pagy
17
- DAY = 60 * 60 * 24
18
- WEEK = DAY * 7
9
+ # List of units in desc order of duration. It can be used for custom units.
10
+ UNITS = %i[year quarter month week day] # rubocop:disable Style/MutableConstant
11
+ DAY = 60 * 60 * 24 # One day in seconds
12
+ WEEK = DAY * 7 # One week in seconds
19
13
 
20
- attr_reader :utc_from, :utc_to, :time_order
21
-
22
- # Create a subclass instance
23
- def self.create(unit, vars)
24
- raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)
25
-
26
- UNITS[unit].new(vars)
27
- end
14
+ attr_reader :order
28
15
 
29
16
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
30
17
  def initialize(vars) # rubocop:disable Lint/MissingSuper
31
- raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses.' if instance_of?(Pagy::Calendar)
18
+ raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
32
19
 
33
- normalize_vars(vars)
20
+ vars = self.class::DEFAULT.merge(vars) # subclass specific default
21
+ normalize_vars(vars) # general default
34
22
  setup_vars(page: 1)
35
23
  setup_unit_vars
36
24
  setup_params_var
@@ -41,28 +29,33 @@ class Pagy # :nodoc:
41
29
  end
42
30
 
43
31
  # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
44
- def label(**opts)
45
- label_for(@page, **opts)
32
+ def label(opts = {})
33
+ label_for(@page, opts)
46
34
  end
47
35
 
48
- # Return the minmax of the current page/unit (used by the extra for the next level minmax)
49
- def current_unit_minmax
50
- [@utc_from.getlocal(@utc_offset), @utc_to.getlocal(@utc_offset)]
36
+ # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
37
+ def label_for(page, opts = {})
38
+ opts[:format] ||= @vars[:format]
39
+ localize(start_for(page.to_i), opts)
51
40
  end
52
41
 
53
- protected
42
+ # Period of the active page (used for nested units)
43
+ def active_period
44
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
45
+ end
54
46
 
55
- def setup_unit_vars(format)
56
- raise VariableError.new(self, format, 'to be a strftime format', @vars[format]) unless @vars[format].is_a?(String)
57
- raise VariableError.new(self, :time_order, 'to be in [:asc, :desc]', @time_order) \
58
- unless %i[asc desc].include?(@time_order = @vars[:time_order])
47
+ protected
59
48
 
60
- min, max = @vars[:minmax]
61
- raise VariableError.new(self, :minmax, 'to be a an Array of min and max local Time instances', @vars[:minmax]) \
62
- unless min.is_a?(Time) && max.is_a?(Time) && !min.utc? && !max.utc? && min <= max \
63
- && (@utc_offset = min.utc_offset) == max.utc_offset
49
+ # Base class method for the setup of the unit variables
50
+ def setup_unit_vars
51
+ raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
52
+ raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
53
+ unless %i[asc desc].include?(@order = @vars[:order])
64
54
 
65
- [min, max]
55
+ @starting, @ending = @vars[:period]
56
+ raise VariableError.new(self, :period, 'to be a an Array of min and max local Time instances', @vars[:period]) \
57
+ unless @starting.is_a?(Time) && @ending.is_a?(Time) && !@starting.utc? && !@ending.utc? && @starting <= @ending \
58
+ && (@utc_offset = @starting.utc_offset) == @ending.utc_offset
66
59
  end
67
60
 
68
61
  # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
@@ -70,9 +63,9 @@ class Pagy # :nodoc:
70
63
  time.strftime(opts[:format])
71
64
  end
72
65
 
73
- # Simple trick to snap the page into its ordered position, without actually reordering anything in the internal structure
74
- def snap(page = @page)
75
- @time_order == :asc ? page - 1 : @pages - page
66
+ # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure
67
+ def snap(page)
68
+ @order == :asc ? page - 1 : @pages - page
76
69
  end
77
70
 
78
71
  # Create a new local time at the beginning of the day
@@ -80,108 +73,17 @@ class Pagy # :nodoc:
80
73
  Time.new(year, month, day, 0, 0, 0, @utc_offset)
81
74
  end
82
75
 
83
- # Calendar year subclass
84
- class Year < Calendar
85
- # Setup the calendar vars when the unit is :year
86
- def setup_unit_vars
87
- min, max = super(:year_format)
88
- @initial = new_time(min.year)
89
- @final = new_time(max.year + 1)
90
- @pages = @last = @final.year - @initial.year
91
- @utc_from = new_time(@initial.year + snap).utc
92
- @utc_to = new_time(@initial.year + snap + 1).utc
93
- end
76
+ class << self
77
+ # Create a subclass instance by unit name (internal use)
78
+ def create(unit, vars)
79
+ raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
94
80
 
95
- # Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
96
- def label_for(page, **opts)
97
- opts[:format] ||= @vars[:year_format]
98
- localize(new_time(@initial.year + snap(page.to_i)), **opts)
81
+ name = unit.to_s
82
+ name[0] = name[0].capitalize
83
+ Object.const_get("Pagy::Calendar::#{name}").new(vars)
99
84
  end
100
85
  end
101
-
102
- # Calendar month subclass
103
- class Month < Calendar
104
- # Setup the calendar vars when the unit is :month
105
- def setup_unit_vars
106
- min, max = super(:month_format)
107
- @initial = new_time(min.year, min.month)
108
- @final = bump_month(max)
109
- @pages = @last = months(@final) - months(@initial)
110
- @utc_from = bump_month(@initial, snap).utc
111
- @utc_to = bump_month(@initial, snap + 1).utc
112
- end
113
-
114
- # Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
115
- def label_for(page, **opts)
116
- opts[:format] ||= @vars[:month_format]
117
- localize(bump_month(@initial, snap(page.to_i)), **opts)
118
- end
119
-
120
- private
121
-
122
- # Months in local time
123
- def months(time)
124
- (time.year * 12) + time.month
125
- end
126
-
127
- # Add 1 or more months to local time
128
- def bump_month(time, months = 1)
129
- months += months(time)
130
- year = months / 12
131
- month = months % 12
132
- month.zero? ? new_time(year - 1, 12) : new_time(year, month)
133
- end
134
- end
135
-
136
- # Calendar week subclass
137
- class Week < Calendar
138
- attr_reader :week_offset
139
-
140
- # Setup the calendar vars when the unit is :week
141
- def setup_unit_vars
142
- setup_vars(week_offset: 0)
143
- min, max = super(:week_format)
144
- @initial = week_start(min)
145
- @final = week_start(max) + WEEK
146
- @pages = @last = (@final - @initial).to_i / WEEK
147
- @utc_from = (@initial + (snap * WEEK)).utc
148
- @utc_to = @utc_from + WEEK
149
- end
150
-
151
- # Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
152
- def label_for(page, **opts)
153
- opts[:format] ||= @vars[:week_format]
154
- localize(@initial + (snap(page.to_i) * WEEK), **opts)
155
- end
156
-
157
- private
158
-
159
- # Return the start of the week for local time
160
- def week_start(time)
161
- start = time - (((time.wday - @week_offset) * DAY) % WEEK)
162
- new_time(start.year, start.month, start.day)
163
- end
164
- end
165
-
166
- # Calendar day subclass
167
- class Day < Calendar
168
- # Setup the calendar vars when the unit is :day
169
- def setup_unit_vars
170
- min, max = super(:day_format)
171
- @initial = new_time(min.year, min.month, min.day)
172
- @final = new_time(max.year, max.month, max.day) + DAY
173
- @pages = @last = (@final - @initial).to_i / DAY
174
- @utc_from = (@initial + (snap * DAY)).utc
175
- @utc_to = @utc_from + DAY
176
- end
177
-
178
- # Generate a label for each page (it can pass along the I18n gem opts when it's used with the i18n extra)
179
- def label_for(page, **opts)
180
- opts[:format] ||= @vars[:day_format]
181
- localize(@initial + (snap(page.to_i) * DAY), **opts)
182
- end
183
- end
184
- # After all the subclasses are defined
185
- UNITS = { year: Year, month: Month, week: Week, day: Day }.freeze
186
86
  end
87
+
88
+ Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
187
89
  end
@@ -8,68 +8,67 @@ class Pagy # :nodoc:
8
8
  module CalendarExtra
9
9
  # Additions for the Backend module
10
10
  module Backend
11
- CONF_KEYS = %i[year month week day pagy skip].freeze
11
+ CONF_KEYS = (Calendar::UNITS + %i[pagy active]).freeze
12
12
 
13
13
  private
14
14
 
15
- # Take a collection and a conf Hash with keys in [:year, :month: week, :day, :pagy] and Hash values of pagy variables
15
+ # Take a collection and a conf Hash with keys in [:year, :quarter, :month: week, :day, :pagy: :active];
16
+ # The calendar is active by default, but it can be explicitly inactivated with `active: false`
16
17
  # Return a hash with 3 items:
17
18
  # 0. Array of pagy calendar unit objects
18
19
  # 1. Pagy object
19
20
  # 2. Array of results
20
21
  def pagy_calendar(collection, conf)
21
- unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :skip }
22
+ unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :active }
22
23
  raise ArgumentError, "keys must be in #{CONF_KEYS.inspect} and object values must be Hashes; got #{conf.inspect}"
23
24
  end
24
25
 
25
26
  conf[:pagy] = {} unless conf[:pagy] # use default Pagy object when omitted
26
- calendar, collection = pagy_setup_calendar(collection, conf) unless conf[:skip]
27
+ calendar, collection = pagy_setup_calendar(collection, conf) unless conf.key?(:active) && !conf[:active]
27
28
  pagy, result = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
28
29
  [calendar, pagy, result]
29
30
  end
30
31
 
31
- # Setup the calendar objects and return them with the filtered collection
32
+ # Setup and return the calendar objects and the filtered collection
32
33
  def pagy_setup_calendar(collection, conf)
33
- units = Calendar::UNITS.keys & conf.keys
34
+ units = Calendar::UNITS & conf.keys # get the units in time length desc order
34
35
  page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
35
36
  units.each do |unit| # set all the :page_param vars for later deletion
36
37
  unit_page_param = :"#{unit}_#{page_param}"
37
38
  conf[unit][:page_param] = unit_page_param
38
39
  conf[unit][:page] = params[unit_page_param]
39
40
  end
40
- calendar = {}
41
- last_calendar = nil
42
- last_minmax = minmax = pagy_calendar_minmax(collection)
41
+ calendar = {}
42
+ period = pagy_calendar_period(collection)
43
43
  units.each_with_index do |unit, index|
44
44
  params_to_delete = units[(index + 1), units.size].map { |sub| conf[sub][:page_param] } + [page_param]
45
45
  conf[unit][:params] = lambda do |params| # delete page_param from the sub-units
46
- params_to_delete.each { |p| params.delete(p.to_s) } # except implemented after 2.5
46
+ params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from 2.5 baseline
47
47
  params
48
48
  end
49
- conf[unit][:minmax] = [[minmax.first, last_minmax.first].max, [minmax.last, last_minmax.last].min]
50
- calendar[unit] = last_calendar = Calendar.create(unit, conf[unit])
51
- last_minmax = calendar[unit].current_unit_minmax # set the minmax for the next unit
49
+ conf[unit][:period] = period
50
+ calendar[unit] = Calendar.send(:create, unit, conf[unit])
51
+ period = calendar[unit].active_period # set the period for the next unit
52
52
  end
53
- filtered = pagy_calendar_filtered(collection, last_calendar.utc_from, last_calendar.utc_to)
54
- [calendar, filtered]
53
+ [calendar, pagy_calendar_filter(collection, calendar[units.last].from, calendar[units.last].to)]
55
54
  end
56
55
 
57
56
  # This method must be implemented by the application.
58
- # It must return an Array with the minimum and maximum Time objects from the collection,
59
- # converted to the local time of the user
60
- def pagy_calendar_minmax(*)
61
- # collection.your_own_method_to_get_the_minmax
62
- raise NoMethodError, 'the pagy_calendar_minmax method must be implemented by the application and must return ' \
63
- 'an Array with the minimum and maximum local Time objects of the collection'
57
+ # It must return the the starting and ending local Time objects defining the calendar :period
58
+ def pagy_calendar_period(*)
59
+ # return_period_array_using(collection)
60
+ raise NoMethodError, 'the pagy_calendar_period method must be implemented by the application and must return ' \
61
+ 'the starting and ending local Time objects array defining the calendar :period'
64
62
  end
65
63
 
66
64
  # This method must be implemented by the application.
67
- # It receives the main collection argument and must return a filtered version of it.
68
- # The filter logic must be equivalent to {utc_time >= pagy.utc_from && utc_time < pagy.utc_to}
69
- def pagy_calendar_filtered(*)
70
- # collection.your_own_method_to_filter_with(pagy.utc_from, pagy.utc_to)
71
- raise NoMethodError, 'the pagy_calendar_filtered method must be implemented by the application and must return the ' \
72
- 'collection filtered by a logic equivalent to {utc_time >= pagy.utc_from && utc_time < pagy.utc_to}'
65
+ # It receives the main collection and must return a filtered version of it.
66
+ # The filter logic must be equivalent to {storage_time >= from && storage_time < to}
67
+ def pagy_calendar_filter(*)
68
+ # return_filtered_collection_using(collection, from, to)
69
+ raise NoMethodError, 'the pagy_calendar_filter method must be implemented by the application and must return the ' \
70
+ 'collection filtered by a logic equivalent to '\
71
+ '{storage_time >= from && storage_time < to}'
73
72
  end
74
73
  end
75
74
  end
@@ -15,25 +15,25 @@ class Pagy # :nodoc:
15
15
 
16
16
  # Add rescue clause for different behaviors
17
17
  def initialize(vars)
18
- @overflow ||= false # still true if :last_page re-run the method after an overflow
18
+ @overflow ||= false # still true if :last_page re-run the method after an overflow
19
19
  super
20
20
  rescue OverflowError
21
- @overflow = true # add the overflow flag
21
+ @overflow = true # add the overflow flag
22
22
  case @vars[:overflow]
23
23
  when :exception
24
- raise # same as without the extra
24
+ raise # same as without the extra
25
25
  when :last_page
26
- requested_page = @vars[:page] # save the requested page (even after re-run)
27
- initialize vars.merge!(page: @last) # re-run with the last page
28
- @vars[:page] = requested_page # restore the requested page
26
+ requested_page = @vars[:page] # save the requested page (even after re-run)
27
+ initialize vars.merge!(page: @last) # re-run with the last page
28
+ @vars[:page] = requested_page # restore the requested page
29
29
  when :empty_page
30
- @offset = @items = @from = @to = 0 # vars relative to the actual page
31
- if defined?(Calendar) && is_a?(Calendar) # only for Calendar instances
32
- edge = @time_order == :asc ? @final : @initial # get the edge of the overflow side (neat, but any time would do)
33
- @utc_from = @utc_to = edge.getutc # set both to the edge utc time (a >=&&< query will get no records)
30
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
31
+ if defined?(Calendar) && is_a?(Calendar) # only for Calendar instances
32
+ edge = @order == :asc ? @final : @initial # get the edge of the overflow side (neat, but any time would do)
33
+ @from = @to = edge # set both to the edge utc time (a >=&&< query will get no records)
34
34
  end
35
- @prev = @last # prev relative to the actual page
36
- extend Series # special series for :empty_page
35
+ @prev = @last # prev relative to the actual page
36
+ extend Series # special series for :empty_page
37
37
  else
38
38
  raise VariableError.new(self, :overflow, 'to be in [:last_page, :empty_page, :exception]', @vars[:overflow])
39
39
  end
@@ -43,7 +43,7 @@ class Pagy # :nodoc:
43
43
  module Series
44
44
  def series(*)
45
45
  @page = @last # series for last page
46
- super.tap do |s| # call original series
46
+ super.tap do |s| # call original series
47
47
  s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
48
48
  @page = @vars[:page] # restore the actual page
49
49
  end
@@ -63,7 +63,7 @@ class Pagy # :nodoc:
63
63
  when :exception
64
64
  raise # same as without the extra
65
65
  when :empty_page
66
- @offset = @items = @from = @to = 0 # vars relative to the actual page
66
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
67
67
  @vars[:size] = [] # no page in the series
68
68
  self
69
69
  else
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.4.0'
8
+ VERSION = '5.6.1'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
@@ -5,7 +5,7 @@
5
5
  The link variable is set to a proc that returns the link tag.
6
6
  Usage: link.call( page_number [, text [, extra_attributes_string ]])
7
7
  -%>
8
- <% link = pagy_link_proc(pagy, 'class="page-link"') -%>
8
+ <% link = pagy_link_proc(pagy, link_extra: 'class="page-link"') -%>
9
9
  <%# -%><nav aria-label="pager" class="pagy-bootstrap-nav" role="navigation">
10
10
  <%# -%> <ul class="pagination">
11
11
  <% if pagy.prev -%> <li class="page-item prev"><%== link.call(pagy.prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"') %></li>
@@ -4,7 +4,7 @@
4
4
  -# The link variable is set to a proc that returns the link tag.
5
5
  -# Usage: link.call( page_number [, text [, extra_attributes_string ]])
6
6
 
7
- - link = pagy_link_proc(pagy, 'class="page-link"')
7
+ - link = pagy_link_proc(pagy, link_extra: 'class="page-link"')
8
8
 
9
9
  %nav.pagy-bootstrap-nav{"aria-label" => "pager", :role => "navigation"}
10
10
 
@@ -4,7 +4,7 @@
4
4
  / The link variable is set to a proc that returns the link tag.
5
5
  / Usage: link.call( page_number [, text [, extra_attributes_string ]])
6
6
 
7
- - link = pagy_link_proc(pagy, 'class="page-link"')
7
+ - link = pagy_link_proc(pagy, link_extra: 'class="page-link"')
8
8
 
9
9
  nav.pagy-bootstrap-nav role="navigation" aria-label="pager"
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.0
4
+ version: 5.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-17 00:00:00.000000000 Z
11
+ date: 2021-11-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email:
@@ -54,6 +54,12 @@ files:
54
54
  - lib/pagy.rb
55
55
  - lib/pagy/backend.rb
56
56
  - lib/pagy/calendar.rb
57
+ - lib/pagy/calendar/day.rb
58
+ - lib/pagy/calendar/month.rb
59
+ - lib/pagy/calendar/month_mixin.rb
60
+ - lib/pagy/calendar/quarter.rb
61
+ - lib/pagy/calendar/week.rb
62
+ - lib/pagy/calendar/year.rb
57
63
  - lib/pagy/console.rb
58
64
  - lib/pagy/countless.rb
59
65
  - lib/pagy/exceptions.rb