pagy 5.5.1 → 5.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e23922e820d30e39dee31d0b1931e79da25f9fd6fa4ee4c5f75a25f383ca99bc
4
- data.tar.gz: 863360e18514c2250c2746d7fdfac4b11e65e0b04488150bee078466877a8b33
3
+ metadata.gz: f567ea14d1f7bf5697ca416cf0cf1bc200986b804099f31aba5fd1ae6a946fa4
4
+ data.tar.gz: 8b09e574678ab6673c4e9d8fdc48eaf611657354d457da4856a70173d7194732
5
5
  SHA512:
6
- metadata.gz: 27819f1864544b94b0c916cca3d4d43a7bfd051c41a7a202b30818e9b5730fd754c2034247c912d48bb046d90ce46f95f4a18a636b2b75e8ed6b06203f1ddc81
7
- data.tar.gz: '031559fa58448415d4555613c76cb16f3e5f1b1262400d6ea928c0817dd45a6902a28014052860aa1ac74f6da59d2313d142226635fd6d351578f12477e34439'
6
+ metadata.gz: 2cf7845ce5b8d668cfa0de512bb6fb7ed676e3586167a9b95fdfd6eebb8b9f7bfd5cb688ab523ef1e7e512ce5f5bb379e305beb4531dd00c609e755620bd0004
7
+ data.tar.gz: f79e860f496368e67bf328b3359f5860162801600452482cf4b1c0b06439b20f9fb296d5593159410c1817fff4576c4e03ef526f265dbb5f9aa4d8877e9c558b
data/lib/config/pagy.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.5.1)
4
- # Customize only what you really need and notice that Pagy works also without any of the following lines.
3
+ # Pagy initializer file (5.6.3)
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
 
7
7
 
8
8
  # Pagy DEFAULT Variables
9
9
  # See https://ddnexus.github.io/pagy/api/pagy#variables
10
- # All the Pagy::DEFAULT are set for all the Pagy instances but can be overridden per instance
11
- # by just passing them to Pagy.new|Pagy::Countless.new|Pagy::Calendar.new or the #pagy controller method
10
+ # All the Pagy::DEFAULT are set for all the Pagy instances but can be overridden per instance by just passing them to
11
+ # Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods
12
12
 
13
13
 
14
14
  # Instance variables
@@ -40,24 +40,27 @@
40
40
  # See https://ddnexus.github.io/pagy/extras/array
41
41
  # require 'pagy/extras/array'
42
42
 
43
- # Calendar extra: Add pagination filtering by calendar time unit (Year, Month, Week Day)
43
+ # Calendar extra: Add pagination filtering by calendar time unit (year, quarter, month, week, day)
44
44
  # See https://ddnexus.github.io/pagy/extras/calendar
45
45
  # require 'pagy/extras/calendar'
46
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
47
+ # Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination
48
+ # Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format
49
49
  #
50
- # Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
51
- # Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
50
+ # Pagy::Calendar::Quarter::DEFAULT[:order] = :asc # Time direction of pagination
51
+ # Pagy::Calendar::Quarter::DEFAULT[:format] = '%Y-Q%q' # strftime format
52
52
  #
53
- # Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination
54
- # Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format
55
- # Pagy::Calendar::Week::DEFAULT[:offset] = 0 # Day offset from Sunday (0: Sunday; 1: Monday;... 6: Saturday)
53
+ # Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
54
+ # Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
56
55
  #
57
- # Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
58
- # Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
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
59
  #
60
- # Uncomment the following block, if you need calendar localization without using the I18n extra
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 lines, if you need calendar localization without using the I18n extra
61
64
  # module LocalizePagyCalendar
62
65
  # def localize(time, opts)
63
66
  # ::I18n.l(time, **opts)
@@ -97,8 +100,8 @@
97
100
  # you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
98
101
  # require 'pagy/extras/shared'
99
102
  # require 'pagy/extras/metadata'
100
- # For performance reason, you should explicitly set ONLY the metadata you use in the frontend
101
- # Pagy::DEFAULT[:metadata] = %i[scaffold_url count page prev next last] # example
103
+ # For performance reasons, you should explicitly set ONLY the metadata you use in the frontend
104
+ # Pagy::DEFAULT[:metadata] = %i[scaffold_url page prev next last] # example
102
105
 
103
106
  # Searchkick extra: Paginate `Searchkick::Results` objects
104
107
  # See https://ddnexus.github.io/pagy/extras/searchkick
@@ -209,18 +212,18 @@
209
212
  #
210
213
  # load the "de", "en" and "es" built-in locales:
211
214
  # (the first passed :locale will be used also as the default_locale)
212
- # Pagy::I18n.load({locale: 'de'},
213
- # {locale: 'en'},
214
- # {locale: 'es'})
215
+ # Pagy::I18n.load({ locale: 'de' },
216
+ # { locale: 'en' },
217
+ # { locale: 'es' })
215
218
  #
216
219
  # load the "en" built-in locale, a custom "es" locale,
217
220
  # and a totally custom locale complete with a custom :pluralize proc:
218
221
  # (the first passed :locale will be used also as the default_locale)
219
- # Pagy::I18n.load({locale: 'en'},
220
- # {locale: 'es', filepath: 'path/to/pagy-es.yml'},
221
- # {locale: 'xyz', # not built-in
222
- # filepath: 'path/to/pagy-xyz.yml',
223
- # pluralize: lambda{ |count| ... } )
222
+ # Pagy::I18n.load({ locale: 'en' },
223
+ # { locale: 'es', filepath: 'path/to/pagy-es.yml' },
224
+ # { locale: 'xyz', # not built-in
225
+ # filepath: 'path/to/pagy-xyz.yml',
226
+ # pluralize: lambda{ |count| ... } )
224
227
 
225
228
 
226
229
  # I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
@@ -5,7 +5,7 @@
5
5
  // Container of the whole pagy stuff
6
6
  function Pagy(){}
7
7
 
8
- Pagy.version = '5.5.1'
8
+ Pagy.version = '5.6.3'
9
9
 
10
10
  // Used by the waitForMe function
11
11
  Pagy.delay = 100
@@ -0,0 +1,22 @@
1
+ # :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
2
+
3
+ ta:
4
+ pagy:
5
+
6
+ item_name:
7
+ one: "பதிவு"
8
+ other: "பதிவுகள்"
9
+
10
+ nav:
11
+ prev: "‹ முந்தையது"
12
+ next: "அடுத்தது ›"
13
+ gap: "…"
14
+
15
+ info:
16
+ no_items: "%{item_name} கிடைக்கவில்லை"
17
+ single_page: "<b>%{count}</b> %{item_name} காட்டப்படுகின்றது"
18
+ multiple_pages: "மொத்தம் <b>%{count}</b> %{item_name}, காட்டப்படுபவை <b>%{from}-%{to}</b>"
19
+
20
+ combo_nav_js: "<label>%{pages}-இல் %{page_input}-வது பக்கம்</label>"
21
+
22
+ items_selector_js: "<label>ஒரு பக்கத்திற்கு %{items_input} %{item_name} காட்டு</label>"
@@ -16,13 +16,13 @@ class Pagy # :nodoc:
16
16
  @initial = new_time(@starting.year, @starting.month, @starting.day)
17
17
  @final = new_time(@ending.year, @ending.month, @ending.day) + DAY
18
18
  @pages = @last = (@final - @initial).to_i / DAY
19
- @from = start_for(@page)
19
+ @from = starting_time_for(@page)
20
20
  @to = @from + DAY
21
21
  end
22
22
 
23
- # Time for the page
24
- def start_for(page)
25
- @initial + (snap(page) * DAY)
23
+ # Starting time for the page
24
+ def starting_time_for(page)
25
+ @initial + (offset_units_for(page) * DAY)
26
26
  end
27
27
  end
28
28
  end
@@ -1,44 +1,16 @@
1
1
  # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative 'month_mixin'
5
+
4
6
  class Pagy # :nodoc:
5
7
  class Calendar # :nodoc:
6
8
  # Calendar month subclass
7
9
  class Month < Calendar
8
10
  DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
11
  format: '%Y-%m' }
10
-
11
- protected
12
-
13
- # Setup the calendar variables
14
- def setup_unit_vars
15
- super
16
- @initial = new_time(@starting.year, @starting.month)
17
- @final = bump_month(@ending)
18
- @pages = @last = months(@final) - months(@initial)
19
- @from = start_for(@page)
20
- @to = bump_month(@from)
21
- end
22
-
23
- # Time for the page
24
- def start_for(page)
25
- bump_month(@initial, snap(page))
26
- end
27
-
28
- private
29
-
30
- # Months in local time
31
- def months(time)
32
- (time.year * 12) + time.month
33
- end
34
-
35
- # Add 1 or more months to local time
36
- def bump_month(time, months = 1)
37
- months += months(time)
38
- year = months / 12
39
- month = months % 12
40
- month.zero? ? new_time(year - 1, 12) : new_time(year, month)
41
- end
12
+ MONTHS = 1 # number of months in the unit
13
+ include MonthMixin
42
14
  end
43
15
  end
44
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 including class must set the MONTHS 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 = unit_starting_time_for(@starting)
17
+ @final = add_months_to(unit_starting_time_for(@ending), @months)
18
+ @pages = @last = (months_in(@final) - months_in(@initial)) / @months
19
+ @from = starting_time_for(@page)
20
+ @to = add_months_to(@from, @months)
21
+ end
22
+
23
+ # Starting time for the page
24
+ def starting_time_for(page)
25
+ add_months_to(@initial, offset_units_for(page) * @months)
26
+ end
27
+
28
+ private
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
+
37
+ # Number of months in time
38
+ def months_in(time)
39
+ (time.year * 12) + time.month
40
+ end
41
+
42
+ # Add months to time
43
+ def add_months_to(time, months)
44
+ months += months_in(time) - 1 # remove 1 month for 0-11 calculations
45
+ new_time(months / 12, (months % 12) + 1) # add 1 month back for 1-12 conversion
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
+ starting_time = starting_time_for(page.to_i) # page could be a string
18
+ opts[:format] = (opts[:format] || @vars[:format]).gsub('%q') { (starting_time.month / 4) + 1 }
19
+ localize(starting_time, opts)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -15,24 +15,24 @@ 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
- @from = start_for(@page)
21
+ @from = starting_time_for(@page)
22
22
  @to = @from + WEEK
23
23
  end
24
24
 
25
- # Time for the page
26
- def start_for(page)
27
- @initial + (snap(page) * WEEK)
25
+ # Starting time for the page
26
+ def starting_time_for(page)
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 local 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
@@ -16,13 +16,13 @@ class Pagy # :nodoc:
16
16
  @initial = new_time(@starting.year)
17
17
  @final = new_time(@ending.year + 1)
18
18
  @pages = @last = @final.year - @initial.year
19
- @from = start_for(@page)
19
+ @from = starting_time_for(@page)
20
20
  @to = new_time(@from.year + 1)
21
21
  end
22
22
 
23
- # Time for the page
24
- def start_for(page)
25
- new_time(@initial.year + snap(page))
23
+ # Starting time for the page
24
+ def starting_time_for(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
@@ -4,10 +4,12 @@
4
4
  require 'pagy'
5
5
 
6
6
  class Pagy # :nodoc:
7
- # Base class for time units subclasses (Year, Month, Week, Day)
7
+ # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
8
8
  class Calendar < Pagy
9
- DAY = 60 * 60 * 24
10
- 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
11
13
 
12
14
  attr_reader :order
13
15
 
@@ -15,8 +17,8 @@ class Pagy # :nodoc:
15
17
  def initialize(vars) # rubocop:disable Lint/MissingSuper
16
18
  raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
17
19
 
18
- normalize_vars(vars)
19
- @vars = self.class::DEFAULT.merge(@vars)
20
+ vars = self.class::DEFAULT.merge(vars) # subclass specific default
21
+ normalize_vars(vars) # general default
20
22
  setup_vars(page: 1)
21
23
  setup_unit_vars
22
24
  setup_params_var
@@ -27,19 +29,14 @@ class Pagy # :nodoc:
27
29
  end
28
30
 
29
31
  # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
30
- def label(**opts)
31
- label_for(@page, **opts)
32
+ def label(opts = {})
33
+ label_for(@page, opts)
32
34
  end
33
35
 
34
36
  # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
35
- def label_for(page, **opts)
37
+ def label_for(page, opts = {})
36
38
  opts[:format] ||= @vars[:format]
37
- localize(start_for(page.to_i), **opts)
38
- end
39
-
40
- # Period of the active page (used for nested units)
41
- def active_period
42
- [[@starting, @from].max, [@to - DAY, @ending].min] # include only last unit day
39
+ localize(starting_time_for(page.to_i), opts) # page could be a string
43
40
  end
44
41
 
45
42
  protected
@@ -61,8 +58,9 @@ class Pagy # :nodoc:
61
58
  time.strftime(opts[:format])
62
59
  end
63
60
 
64
- # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure.
65
- 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)
66
64
  @order == :asc ? page - 1 : @pages - page
67
65
  end
68
66
 
@@ -70,20 +68,23 @@ class Pagy # :nodoc:
70
68
  def new_time(year, month = 1, day = 1)
71
69
  Time.new(year, month, day, 0, 0, 0, @utc_offset)
72
70
  end
73
- end
74
- require 'pagy/calendar/year'
75
- require 'pagy/calendar/month'
76
- require 'pagy/calendar/week'
77
- require 'pagy/calendar/day'
78
71
 
79
- class Calendar # :nodoc:
80
- UNITS = { year: Year, month: Month, week: Week, day: Day }.freeze
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
81
76
 
82
- # Create a subclass instance by unit name (internal use)
83
- def self.create(unit, vars)
84
- raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)
77
+ class << self
78
+ # Create a subclass instance by unit name (internal use)
79
+ def create(unit, vars)
80
+ raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
85
81
 
86
- UNITS[unit].new(vars)
82
+ name = unit.to_s
83
+ name[0] = name[0].capitalize
84
+ Object.const_get("Pagy::Calendar::#{name}").new(vars)
85
+ end
87
86
  end
88
87
  end
88
+
89
+ Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
89
90
  end
@@ -8,16 +8,11 @@ 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 active].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: :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,43 @@ 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.keys & conf.keys
29
+ units = Calendar::UNITS & conf.keys # get the units in time length desc order
35
30
  page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
36
31
  units.each do |unit| # set all the :page_param vars for later deletion
37
- unit_page_param = :"#{unit}_#{page_param}"
32
+ unit_page_param = :"#{unit}_#{page_param}"
38
33
  conf[unit][:page_param] = unit_page_param
39
34
  conf[unit][:page] = params[unit_page_param]
40
35
  end
41
36
  calendar = {}
42
- period = pagy_calendar_period(collection)
37
+ last_obj = nil
43
38
  units.each_with_index do |unit, index|
44
39
  params_to_delete = units[(index + 1), units.size].map { |sub| conf[sub][:page_param] } + [page_param]
45
40
  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
41
+ params_to_delete.each { |p| params.delete(p.to_s) } # Hash#except missing from ruby 2.5 baseline
47
42
  params
48
43
  end
49
- conf[unit][:period] = period
50
- calendar[unit] = Calendar.create(unit, conf[unit])
51
- period = calendar[unit].active_period # set the period for the next unit
44
+ conf[unit][:period] = last_obj&.send(:active_period) || pagy_calendar_period(collection)
45
+ calendar[unit] = last_obj = Calendar.send(:create, unit, conf[unit])
52
46
  end
53
- [calendar, pagy_calendar_filter(collection, calendar[units.last].from, calendar[units.last].to)]
47
+ [calendar, pagy_calendar_filter(collection, last_obj.from, last_obj.to)]
54
48
  end
55
49
 
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
50
+ # This method must be implemented by the application
58
51
  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'
52
+ raise NoMethodError, 'the pagy_calendar_period method must be implemented by the application ' \
53
+ '(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_periodcollection)'
62
54
  end
63
55
 
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}
56
+ # This method must be implemented by the application
67
57
  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}'
58
+ raise NoMethodError, 'the pagy_calendar_filter method must be implemented by the application ' \
59
+ '(see https://ddnexus.github.io/pagy/extras/calendar#pagy_calendar_filtercollection-from-to)'
72
60
  end
73
61
  end
74
62
  end
@@ -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)
data/lib/pagy/frontend.rb CHANGED
@@ -76,8 +76,8 @@ class Pagy
76
76
 
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
- def pagy_t(key, **opts)
80
- Pagy::I18n.t(@pagy_locale ||= nil, key, **opts)
79
+ def pagy_t(key, opts = {})
80
+ Pagy::I18n.t(@pagy_locale ||= nil, key, opts)
81
81
  end
82
82
  end
83
83
  end
data/lib/pagy/i18n.rb CHANGED
@@ -10,12 +10,16 @@ class Pagy
10
10
 
11
11
  # Pluralization rules
12
12
  module P11n
13
- # Utility variables
14
- from0to1 = [0, 1].freeze
15
- from2to4 = [2, 3, 4].freeze
16
- from5to9 = [5, 6, 7, 8, 9].freeze
17
- from11to14 = [11, 12, 13, 14].freeze
18
- from12to14 = [12, 13, 14].freeze
13
+ # Pluralization variables
14
+ from0to1 = (0..1).to_a.freeze
15
+ from2to4 = (2..4).to_a.freeze
16
+ from3to10 = (3..10).to_a.freeze
17
+ from5to9 = (5..9).to_a.freeze
18
+ from11to14 = (11..14).to_a.freeze
19
+ from11to99 = (11..99).to_a.freeze
20
+ from12to14 = (12..14).to_a.freeze
21
+
22
+ from0to1_from5to9 = from0to1 + from5to9
19
23
 
20
24
  # Store the proc defining each pluralization RULE
21
25
  # Logic adapted from https://github.com/svenfuchs/rails-i18n
@@ -24,12 +28,12 @@ class Pagy
24
28
  lambda do |n = 0|
25
29
  mod100 = n % 100
26
30
  case
27
- when n == 0 then 'zero' # rubocop:disable Style/NumericPredicate
28
- when n == 1 then 'one'
29
- when n == 2 then 'two'
30
- when (3..10).to_a.include?(mod100) then 'few'
31
- when (11..99).to_a.include?(mod100) then 'many'
32
- else 'other'
31
+ when n == 0 then 'zero' # rubocop:disable Style/NumericPredicate
32
+ when n == 1 then 'one'
33
+ when n == 2 then 'two'
34
+ when from3to10.include?(mod100) then 'few'
35
+ when from11to99.include?(mod100) then 'many'
36
+ else 'other'
33
37
  end
34
38
  end,
35
39
 
@@ -68,10 +72,10 @@ class Pagy
68
72
  mod10 = n % 10
69
73
  mod100 = n % 100
70
74
  case
71
- when n == 1 then 'one'
72
- when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
73
- when (from0to1 + from5to9).include?(mod10) || from12to14.include?(mod100) then 'many'
74
- else 'other'
75
+ when n == 1 then 'one'
76
+ when from2to4.include?(mod10) && !from12to14.include?(mod100) then 'few'
77
+ when from0to1_from5to9.include?(mod10) || from12to14.include?(mod100) then 'many'
78
+ else 'other'
75
79
  end
76
80
  end,
77
81
 
@@ -149,7 +153,7 @@ class Pagy
149
153
  end
150
154
 
151
155
  # Translate and pluralize the key with the locale DATA
152
- def t(locale, key, **opts)
156
+ def t(locale, key, opts = {})
153
157
  data, pluralize = DATA[locale]
154
158
  translation = data[key] || (opts[:count] && data[key += ".#{pluralize.call(opts[:count])}"]) \
155
159
  or return %([translation missing: "#{key}"])
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.5.1'
8
+ VERSION = '5.6.3'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
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.5.1
4
+ version: 5.6.3
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-23 00:00:00.000000000 Z
11
+ date: 2021-11-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email:
@@ -46,6 +46,7 @@ files:
46
46
  - lib/locales/sv-SE.yml
47
47
  - lib/locales/sv.yml
48
48
  - lib/locales/sw.yml
49
+ - lib/locales/ta.yml
49
50
  - lib/locales/tr.yml
50
51
  - lib/locales/uk.yml
51
52
  - lib/locales/zh-CN.yml
@@ -56,6 +57,8 @@ files:
56
57
  - lib/pagy/calendar.rb
57
58
  - lib/pagy/calendar/day.rb
58
59
  - lib/pagy/calendar/month.rb
60
+ - lib/pagy/calendar/month_mixin.rb
61
+ - lib/pagy/calendar/quarter.rb
59
62
  - lib/pagy/calendar/week.rb
60
63
  - lib/pagy/calendar/year.rb
61
64
  - lib/pagy/console.rb