pagy 5.6.2 → 5.6.6

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: 0a24ac286e97d1aacd7603d7ada3791006120004bf56c027320c4184b9488fc7
4
- data.tar.gz: 63a018d758c2ffff334de22fc8fdd433cdffe74030ba46ca5c3cf5dc8a563a9c
3
+ metadata.gz: 39e1594de6e9b2ca3919cf666e3b7735c96656447d78c39ce0a4aaf85a737487
4
+ data.tar.gz: ec8e032be1fe73b5890c1fc42c05d81c397015b468120a8cd97dcdd2bf88fcfa
5
5
  SHA512:
6
- metadata.gz: f5fd0f9dd9c8c8a151ea026e7e749ba6446fec53baf4bd9af276f8059f062d50d54b0d500e46762f5f4df8a3f89af475f4b07c7d8633f8194ee3da5d33d2b104
7
- data.tar.gz: cd09f6222ce1d6db7c92984c9382d080d386ea862f66b3b7104721ea6cc6fc5e788714ce2aa623517c6ad9f17c6c95fd52fdcbcf2ba9a017c1485b262ce86405
6
+ metadata.gz: 3df6ce4c03f578555b88c34691be3c667c1aaf2d4712382ebb1ddf905ad5f2f545987ce61270aa0a57705f1cfbe592ffbc649afb5c1137e8f69552388750e198
7
+ data.tar.gz: c40c023541aaf714d0bfaa0fbaa4034edcb81d2d8b627a05f4e696d9eaa165f5f1aff7596497c9b4ccb9bf19773b65bb1aa79c7ba19765f3c993eab9e260e864
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.6.2)
3
+ # Pagy initializer file (5.6.6)
4
4
  # Customize only what you really need and notice that the core 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
 
@@ -2,10 +2,10 @@
2
2
 
3
3
  // This code should be OK also with very old browsers
4
4
 
5
- // Container of the whole pagy stuff
5
+ // Container for the whole pagy stuff
6
6
  function Pagy(){}
7
7
 
8
- Pagy.version = '5.6.2'
8
+ Pagy.version = '5.6.6'
9
9
 
10
10
  // Used by the waitForMe function
11
11
  Pagy.delay = 100
@@ -54,6 +54,7 @@ Pagy.nav =
54
54
  else if (typeof(item) === 'string') { html += fill(tags.active, item, label) }
55
55
  }
56
56
  html += tags.after
57
+ this.innerHTML = ''
57
58
  this.insertAdjacentHTML('afterbegin', html)
58
59
  lastWidth = width
59
60
  }
data/lib/pagy/backend.rb CHANGED
@@ -8,7 +8,7 @@ class Pagy
8
8
  module Backend
9
9
  private
10
10
 
11
- # Return Pagy object and items
11
+ # Return Pagy object and paginated items/results
12
12
  def pagy(collection, vars = {})
13
13
  pagy = Pagy.new(pagy_get_vars(collection, vars))
14
14
  [pagy, pagy_get_items(collection, pagy)]
@@ -22,7 +22,7 @@ class Pagy # :nodoc:
22
22
 
23
23
  # Starting time for the page
24
24
  def starting_time_for(page)
25
- @initial + (snap(page) * DAY)
25
+ @initial + (offset_units_for(page) * DAY)
26
26
  end
27
27
  end
28
28
  end
@@ -13,8 +13,8 @@ class Pagy
13
13
  def setup_unit_vars
14
14
  super
15
15
  @months = self.class::MONTHS # number of months in the unit
16
- @initial = new_time(@starting.year, starting_month_including(@starting.month))
17
- @final = add_months_to(new_time(@ending.year, starting_month_including(@ending.month)), @months)
16
+ @initial = unit_starting_time_for(@starting)
17
+ @final = add_months_to(unit_starting_time_for(@ending), @months)
18
18
  @pages = @last = (months_in(@final) - months_in(@initial)) / @months
19
19
  @from = starting_time_for(@page)
20
20
  @to = add_months_to(@from, @months)
@@ -22,16 +22,18 @@ class Pagy
22
22
 
23
23
  # Starting time for the page
24
24
  def starting_time_for(page)
25
- add_months_to(@initial, snap(page) * @months)
26
- end
27
-
28
- # Starting month of the unit including the passed month
29
- def starting_month_including(month)
30
- (@months * ((month - 1) / @months)) + 1 # remove 1 month for 0-11 calculations and add it back for 1-12 conversion
25
+ add_months_to(@initial, offset_units_for(page) * @months)
31
26
  end
32
27
 
33
28
  private
34
29
 
30
+ # Unit starting time for time
31
+ def unit_starting_time_for(time)
32
+ # remove 1 month for 0-11 calculations and add it back for 1-12 conversion
33
+ starting_month = (@months * ((time.month - 1) / @months)) + 1
34
+ new_time(time.year, starting_month)
35
+ end
36
+
35
37
  # Number of months in time
36
38
  def months_in(time)
37
39
  (time.year * 12) + time.month
@@ -15,8 +15,8 @@ class Pagy # :nodoc:
15
15
  def setup_unit_vars
16
16
  setup_vars(offset: 0)
17
17
  super
18
- @initial = week_start(@starting)
19
- @final = week_start(@ending) + WEEK
18
+ @initial = unit_starting_time_for(@starting)
19
+ @final = unit_starting_time_for(@ending) + WEEK
20
20
  @pages = @last = (@final - @initial).to_i / WEEK
21
21
  @from = starting_time_for(@page)
22
22
  @to = @from + WEEK
@@ -24,15 +24,15 @@ class Pagy # :nodoc:
24
24
 
25
25
  # Starting time for the page
26
26
  def starting_time_for(page)
27
- @initial + (snap(page) * WEEK)
27
+ @initial + (offset_units_for(page) * WEEK)
28
28
  end
29
29
 
30
30
  private
31
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)
32
+ # Unit starting time for time
33
+ def unit_starting_time_for(time)
34
+ starting_time = time - (((time.wday - @offset) * DAY) % WEEK)
35
+ new_time(starting_time.year, starting_time.month, starting_time.day)
36
36
  end
37
37
  end
38
38
  end
@@ -22,7 +22,7 @@ class Pagy # :nodoc:
22
22
 
23
23
  # Starting time for the page
24
24
  def starting_time_for(page)
25
- new_time(@initial.year + snap(page))
25
+ new_time(@initial.year + offset_units_for(page))
26
26
  end
27
27
  end
28
28
  end
data/lib/pagy/calendar.rb CHANGED
@@ -39,14 +39,9 @@ class Pagy # :nodoc:
39
39
  localize(starting_time_for(page.to_i), opts) # page could be a string
40
40
  end
41
41
 
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
46
-
47
42
  protected
48
43
 
49
- # Base class method for the setup of the unit variables
44
+ # Base class method for the setup of the unit variables (subclasses must implement it and call super)
50
45
  def setup_unit_vars
51
46
  raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
52
47
  raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
@@ -63,8 +58,9 @@ class Pagy # :nodoc:
63
58
  time.strftime(opts[:format])
64
59
  end
65
60
 
66
- # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure
67
- def snap(page)
61
+ # Number of units to offset from the @initial time, in order to get the ordered starting time for the page.
62
+ # Used in starting_time_for(page) with a logic equivalent to: @initial + (offset_units_for(page) * unit_time_length)
63
+ def offset_units_for(page)
68
64
  @order == :asc ? page - 1 : @pages - page
69
65
  end
70
66
 
@@ -73,6 +69,11 @@ class Pagy # :nodoc:
73
69
  Time.new(year, month, day, 0, 0, 0, @utc_offset)
74
70
  end
75
71
 
72
+ # Period of the active page (used internally for nested units)
73
+ def active_period
74
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
75
+ end
76
+
76
77
  class << self
77
78
  # Create a subclass instance by unit name (internal use)
78
79
  def create(unit, vars)
@@ -84,6 +85,6 @@ class Pagy # :nodoc:
84
85
  end
85
86
  end
86
87
  end
87
-
88
+ # Require the subclass files in UNITS (no custom unit at this point yet)
88
89
  Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
89
90
  end
@@ -6,11 +6,13 @@ class Pagy # :nodoc:
6
6
  module ArelExtra
7
7
  private
8
8
 
9
+ # Return Pagy object and paginated collection/results
9
10
  def pagy_arel(collection, vars = {})
10
11
  pagy = Pagy.new(pagy_arel_get_vars(collection, vars))
11
12
  [pagy, pagy_get_items(collection, pagy)]
12
13
  end
13
14
 
15
+ # Sub-method called only by #pagy_arel: here for easy customization of variables by overriding
14
16
  def pagy_arel_get_vars(collection, vars)
15
17
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
16
18
  vars[:count] ||= pagy_arel_count(collection)
@@ -18,6 +20,7 @@ class Pagy # :nodoc:
18
20
  vars
19
21
  end
20
22
 
23
+ # Count using Arel when grouping
21
24
  def pagy_arel_count(collection)
22
25
  if collection.group_values.empty?
23
26
  # COUNT(*)
@@ -6,7 +6,7 @@ class Pagy # :nodoc:
6
6
  module ArrayExtra
7
7
  private
8
8
 
9
- # Return Pagy object and items
9
+ # Return Pagy object and paginated items
10
10
  def pagy_array(array, vars = {})
11
11
  pagy = Pagy.new(pagy_array_get_vars(array, vars))
12
12
  [pagy, array[pagy.offset, pagy.items]]
@@ -4,7 +4,7 @@
4
4
  require 'pagy/calendar'
5
5
 
6
6
  class Pagy # :nodoc:
7
- # Paginate based on calendar periods (year month week day) plus the regular pagination
7
+ # Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
8
8
  module CalendarExtra
9
9
  # Additions for the Backend module
10
10
  module Backend
@@ -12,12 +12,7 @@ class Pagy # :nodoc:
12
12
 
13
13
  private
14
14
 
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`
17
- # Return a hash with 3 items:
18
- # 0. Array of pagy calendar unit objects
19
- # 1. Pagy object
20
- # 2. Array of results
15
+ # Take a collection and a conf Hash with keys in CONF_KEYS and return an array with 3 items: [calendar, pagy, results]
21
16
  def pagy_calendar(collection, conf)
22
17
  unless conf.is_a?(Hash) && (conf.keys - CONF_KEYS).empty? && conf.all? { |k, v| v.is_a?(Hash) || k == :active }
23
18
  raise ArgumentError, "keys must be in #{CONF_KEYS.inspect} and object values must be Hashes; got #{conf.inspect}"
@@ -25,50 +20,45 @@ class Pagy # :nodoc:
25
20
 
26
21
  conf[:pagy] = {} unless conf[:pagy] # use default Pagy object when omitted
27
22
  calendar, collection = pagy_setup_calendar(collection, conf) unless conf.key?(:active) && !conf[:active]
28
- pagy, result = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
29
- [calendar, pagy, result]
23
+ pagy, results = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
24
+ [calendar, pagy, results]
30
25
  end
31
26
 
32
27
  # Setup and return the calendar objects and the filtered collection
33
28
  def pagy_setup_calendar(collection, conf)
34
- units = Calendar::UNITS & conf.keys # get the units in time length desc order
29
+ units = Calendar::UNITS & conf.keys # get the units in time length desc order
30
+ raise ArgumentError, 'no calendar unit found in pagy_calendar configuration' if units.empty?
31
+
35
32
  page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
36
33
  units.each do |unit| # set all the :page_param vars for later deletion
37
- unit_page_param = :"#{unit}_#{page_param}"
34
+ unit_page_param = :"#{unit}_#{page_param}"
38
35
  conf[unit][:page_param] = unit_page_param
39
36
  conf[unit][:page] = params[unit_page_param]
40
37
  end
41
38
  calendar = {}
42
- period = pagy_calendar_period(collection)
39
+ last_obj = nil
43
40
  units.each_with_index do |unit, index|
44
41
  params_to_delete = units[(index + 1), units.size].map { |sub| conf[sub][:page_param] } + [page_param]
45
42
  conf[unit][:params] = lambda do |params| # delete page_param from the sub-units
46
- params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from 2.5 baseline
43
+ params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from ruby 2.5 baseline
47
44
  params
48
45
  end
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
46
+ conf[unit][:period] = last_obj&.send(:active_period) || pagy_calendar_period(collection)
47
+ calendar[unit] = last_obj = Calendar.send(:create, unit, conf[unit])
52
48
  end
53
- [calendar, pagy_calendar_filter(collection, calendar[units.last].from, calendar[units.last].to)]
49
+ [calendar, pagy_calendar_filter(collection, last_obj.from, last_obj.to)]
54
50
  end
55
51
 
56
- # This method must be implemented by the application.
57
- # It must return the the starting and ending local Time objects defining the calendar :period
52
+ # This method must be implemented by the application
58
53
  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'
54
+ raise NoMethodError, 'the pagy_calendar_period method must be implemented by the application ' \
55
+ '(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_periodcollection)'
62
56
  end
63
57
 
64
- # This method must be implemented by the application.
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}
58
+ # This method must be implemented by the application
67
59
  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}'
60
+ raise NoMethodError, 'the pagy_calendar_filter method must be implemented by the application ' \
61
+ '(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_filtercollection-from-to)'
72
62
  end
73
63
  end
74
64
  end
@@ -18,7 +18,7 @@ class Pagy # :nodoc:
18
18
  # "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
19
19
  # "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
20
20
  # Notice: if :steps is false it will use the single {0 => @vars[:size]} size
21
- def sequels(steps: @vars[:steps] || { 0 => @vars[:size] })
21
+ def sequels(steps: @vars[:steps] || { 0 => @vars[:size] }, **_)
22
22
  raise VariableError.new(self, :steps, 'to define the 0 width', steps) unless steps.key?(0)
23
23
 
24
24
  {}.tap do |sequels|
@@ -28,11 +28,11 @@ class Pagy # :nodoc:
28
28
  @pages = @last = (if @count > (sum = gearbox_items.sum)
29
29
  [((@count - sum).to_f / gearbox_items.last).ceil, 1].max + gearbox_items.count
30
30
  else
31
- pages = 0
32
- reminder = @count
33
- while reminder.positive?
34
- pages += 1
35
- reminder -= gearbox_items[pages - 1]
31
+ pages = 0
32
+ remainder = @count
33
+ while remainder.positive?
34
+ pages += 1
35
+ remainder -= gearbox_items[pages - 1]
36
36
  end
37
37
  [pages, 1].max
38
38
  end)
@@ -4,14 +4,14 @@
4
4
  class Pagy # :nodoc:
5
5
  # Use ::I18n gem
6
6
  module I18nExtra
7
- # Frontend overriding
7
+ # Frontend overriding for translation
8
8
  module Frontend
9
9
  def pagy_t(key, **opts)
10
10
  ::I18n.t(key, **opts)
11
11
  end
12
12
  end
13
13
 
14
- # Calendar overriding
14
+ # Calendar overriding for localization (see also the block in the calendar section of the config/pagy.rb initializer)
15
15
  module Calendar
16
16
  def localize(time, opts)
17
17
  ::I18n.l(time, **opts)
data/lib/pagy/frontend.rb CHANGED
@@ -77,7 +77,7 @@ class Pagy
77
77
  # Similar to I18n.t: just ~18x faster using ~10x less memory
78
78
  # (@pagy_locale explicitly initialized in order to avoid warning)
79
79
  def pagy_t(key, opts = {})
80
- Pagy::I18n.t(@pagy_locale ||= nil, key, opts)
80
+ Pagy::I18n.translate(@pagy_locale ||= nil, key, opts)
81
81
  end
82
82
  end
83
83
  end
data/lib/pagy/i18n.rb CHANGED
@@ -153,11 +153,12 @@ class Pagy
153
153
  end
154
154
 
155
155
  # Translate and pluralize the key with the locale DATA
156
- def t(locale, key, opts = {})
156
+ def translate(locale, key, opts = {})
157
157
  data, pluralize = DATA[locale]
158
158
  translation = data[key] || (opts[:count] && data[key += ".#{pluralize.call(opts[:count])}"]) \
159
159
  or return %([translation missing: "#{key}"])
160
160
  translation.gsub(/%{[^}]+?}/) { |match| opts[:"#{match[2..-2]}"] || match }
161
161
  end
162
+ alias t translate
162
163
  end
163
164
  end
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.6.2'
8
+ VERSION = '5.6.6'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
@@ -44,7 +44,7 @@ class Pagy
44
44
  end
45
45
 
46
46
  # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
47
- def series(size: @vars[:size])
47
+ def series(size: @vars[:size], **_)
48
48
  return [] if size.empty?
49
49
  raise VariableError.new(self, :size, 'to contain 4 items >= 0', size) \
50
50
  unless size.is_a?(Array) && size.size == 4 && size.all? { |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
@@ -59,11 +59,11 @@ class Pagy
59
59
  series = []
60
60
  start = 1
61
61
  if (left_gap_end - left_gap_start).positive?
62
- series.push(*start..(left_gap_start - 1), :gap)
62
+ series.push(*start...left_gap_start, :gap)
63
63
  start = left_gap_end + 1
64
64
  end
65
65
  if (right_gap_end - right_gap_start).positive?
66
- series.push(*start..(right_gap_start - 1), :gap)
66
+ series.push(*start...right_gap_start, :gap)
67
67
  start = right_gap_end + 1
68
68
  end
69
69
  series.push(*start..@last)
@@ -76,6 +76,7 @@ class Pagy
76
76
  page.to_s
77
77
  end
78
78
 
79
+ # Allow the customization of the output (overridden by the calendar extra)
79
80
  def label
80
81
  @page.to_s
81
82
  end
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.6.2
4
+ version: 5.6.6
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-29 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email:
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
128
  requirements: []
129
- rubygems_version: 3.2.3
129
+ rubygems_version: 3.2.32
130
130
  signing_key:
131
131
  specification_version: 4
132
132
  summary: The kick-ass pagination ruby gem