pagy 5.6.1 → 5.6.5

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: a5d3090b61a9b11c0763a7c2d103ee36f4a36f0618e7e99b32357bab408a29bc
4
- data.tar.gz: d5c3c376deda1c84558bbcff43809c9235cb2fc9ced7492ecdfe8df6397c873e
3
+ metadata.gz: 3bc0ec346bfde6c907da343d7c3f7ad611cbb69f4ed8e36aa09b69b48276ecb6
4
+ data.tar.gz: 9d936c804c00f7eb530c9a7f5d1ecf763704daf84e35b9a3288eae2f88d3e3be
5
5
  SHA512:
6
- metadata.gz: 6591de1571e9b6bdf1a26668cf730dcbba4c83ef9f2d3727b3a1bec5f98c49a69ae2d55aab363444d85a3310e3de65fe119fb3606ca63cfdf56202c14c591568
7
- data.tar.gz: 02fea4991aab7911107460e58e73a66f507f9fff990261f1536fddf016b9523003165ed0dfd024013282ef43fec221cb7fcfa27b7f6b23c60a742f175adc0eae
6
+ metadata.gz: a2e92a735ed04def206a2cdd81577ff39042cc03f98a4343b6e8889c008bb5ba8277bd76168b1d3f39067bc68b9e9b4b1805721cac9a12cded2a491b106023db
7
+ data.tar.gz: 825ad4c0c076d4dae52ba69772b0c827662181ddd9bd077c41ec5a167b0da506bf7153493a99ab5a704043f08918b1d0e460430c8fa7ee08d0675b1f3d1a5619
data/lib/config/pagy.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.6.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.5)
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,7 +40,7 @@
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, quartr, 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
@@ -60,7 +60,7 @@
60
60
  # Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
61
61
  # Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
62
62
  #
63
- # Uncomment the following block, if you need calendar localization without using the I18n extra
63
+ # Uncomment the following lines, if you need calendar localization without using the I18n extra
64
64
  # module LocalizePagyCalendar
65
65
  # def localize(time, opts)
66
66
  # ::I18n.l(time, **opts)
@@ -100,8 +100,8 @@
100
100
  # you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
101
101
  # require 'pagy/extras/shared'
102
102
  # require 'pagy/extras/metadata'
103
- # For performance reason, you should explicitly set ONLY the metadata you use in the frontend
104
- # 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
105
105
 
106
106
  # Searchkick extra: Paginate `Searchkick::Results` objects
107
107
  # See https://ddnexus.github.io/pagy/extras/searchkick
@@ -212,18 +212,18 @@
212
212
  #
213
213
  # load the "de", "en" and "es" built-in locales:
214
214
  # (the first passed :locale will be used also as the default_locale)
215
- # Pagy::I18n.load({locale: 'de'},
216
- # {locale: 'en'},
217
- # {locale: 'es'})
215
+ # Pagy::I18n.load({ locale: 'de' },
216
+ # { locale: 'en' },
217
+ # { locale: 'es' })
218
218
  #
219
219
  # load the "en" built-in locale, a custom "es" locale,
220
220
  # and a totally custom locale complete with a custom :pluralize proc:
221
221
  # (the first passed :locale will be used also as the default_locale)
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| ... } )
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| ... } )
227
227
 
228
228
 
229
229
  # I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
@@ -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.1'
8
+ Pagy.version = '5.6.5'
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
  }
@@ -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>"
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)]
@@ -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
@@ -5,7 +5,7 @@ class Pagy
5
5
  # Mixin for month based unit periods
6
6
  # It is used for month and quarter, but you could use it to implement less common unit of 6, 4, 2 months
7
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.
8
+ # The including class must set the MONTHS duration for the unit and the usual DEFAULT.
9
9
  module MonthMixin
10
10
  protected
11
11
 
@@ -13,36 +13,36 @@ 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, beginning_month(@starting.month))
17
- @final = add_to(new_time(@ending.year, beginning_month(@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
- @from = start_for(@page)
20
- @to = add_to(@from, @months)
19
+ @from = starting_time_for(@page)
20
+ @to = add_months_to(@from, @months)
21
21
  end
22
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
23
+ # Starting time for the page
24
+ def starting_time_for(page)
25
+ add_months_to(@initial, offset_units_for(page) * @months)
31
26
  end
32
27
 
33
28
  private
34
29
 
35
- # Months in time
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
36
38
  def months_in(time)
37
39
  (time.year * 12) + time.month
38
40
  end
39
41
 
40
42
  # 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)
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
46
  end
47
47
  end
48
48
  end
@@ -14,9 +14,9 @@ class Pagy # :nodoc:
14
14
 
15
15
  # The label for any page, with the substitution of the '%q' token
16
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)
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
20
  end
21
21
  end
22
22
  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 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
@@ -36,17 +36,12 @@ class Pagy # :nodoc:
36
36
  # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
37
37
  def label_for(page, opts = {})
38
38
  opts[:format] ||= @vars[:format]
39
- localize(start_for(page.to_i), opts)
40
- end
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
39
+ localize(starting_time_for(page.to_i), opts) # page could be a string
45
40
  end
46
41
 
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
@@ -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
@@ -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.translate(@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,11 +153,12 @@ 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 translate(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}"])
156
160
  translation.gsub(/%{[^}]+?}/) { |match| opts[:"#{match[2..-2]}"] || match }
157
161
  end
162
+ alias t translate
158
163
  end
159
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.1'
8
+ VERSION = '5.6.5'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
@@ -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.1
4
+ version: 5.6.5
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-28 00:00:00.000000000 Z
11
+ date: 2021-12-08 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