pagy 5.6.0 → 5.6.4

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: 4a62cb4c6decfdc1013da6d14b38220aa361246637301a8d92b2d781440397bc
4
- data.tar.gz: 2555195198def80d6ec002928da8cef07419a76da9a196d8971d50e7fb5095e2
3
+ metadata.gz: 5436ee5b008a1f9db93ba52363fbae559f6444600c5925fcd87541b606e2a8ad
4
+ data.tar.gz: 6159c1b56724cf1e10af6c15c2a141200d1795ed3da6fa6655cb1e7ee3f7056c
5
5
  SHA512:
6
- metadata.gz: f665f63cb281c25d6fcf430c12888def46ea46911e45a566fd52cb4943763efe775b094015d3b658b8322ade23a9a43a5888c60f10471bd567f000d5a67352c7
7
- data.tar.gz: 8c22ab2ad5efc0bd91b648d89217d1b71d77fbf669e1153b015954b79c2506c8f5ffe29d80e7d5988bea76b4bed35e7a76bb9c3657f60de0db3cde84b38e62e2
6
+ metadata.gz: 6d2e0aa3e7a4a9bfee7b4360b1d6ae35d931a1aa7fd4959436adf78cb43c153149ebb80455d9a63f7ef36558c5d673191fcd5ad10ce7e4be2e75ca42be645d05
7
+ data.tar.gz: ee07cd90d3908f52c879cebf1956ee5cb1c12739b21e4a0519e7a8432e3ead0304a2bca94ca6a9444035e72563e2de2dd4a5191c279183fad39d22b7703babae
data/lib/config/pagy.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.6.0)
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.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
 
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
@@ -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.0'
8
+ Pagy.version = '5.6.4'
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>"
@@ -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
@@ -8,9 +8,16 @@ class Pagy # :nodoc:
8
8
  # Calendar quarter subclass
9
9
  class Quarter < Calendar
10
10
  DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
11
- format: '%Y-Q%q' }
11
+ format: '%Y-Q%q' } # '%q' token
12
12
  MONTHS = 3 # number of months of the unit
13
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
14
21
  end
15
22
  end
16
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 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
@@ -29,21 +29,14 @@ class Pagy # :nodoc:
29
29
  end
30
30
 
31
31
  # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
32
- def label(**opts)
33
- label_for(@page, **opts)
32
+ def label(opts = {})
33
+ label_for(@page, opts)
34
34
  end
35
35
 
36
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)
37
+ def label_for(page, opts = {})
38
38
  opts[:format] ||= @vars[:format]
39
- start = start_for(page.to_i)
40
- opts[:format] = opts[:format].gsub('%q') { (start.month / 4) + 1 }
41
- localize(start, **opts)
42
- end
43
-
44
- # Period of the active page (used for nested units)
45
- def active_period
46
- [[@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
47
40
  end
48
41
 
49
42
  protected
@@ -65,8 +58,9 @@ class Pagy # :nodoc:
65
58
  time.strftime(opts[:format])
66
59
  end
67
60
 
68
- # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure
69
- 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)
70
64
  @order == :asc ? page - 1 : @pages - page
71
65
  end
72
66
 
@@ -75,6 +69,11 @@ class Pagy # :nodoc:
75
69
  Time.new(year, month, day, 0, 0, 0, @utc_offset)
76
70
  end
77
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
+
78
77
  class << self
79
78
  # Create a subclass instance by unit name (internal use)
80
79
  def create(unit, vars)
@@ -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,8 +20,8 @@ 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
@@ -34,41 +29,34 @@ class Pagy # :nodoc:
34
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.send(: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.6.0'
8
+ VERSION = '5.6.4'
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)
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.0
4
+ version: 5.6.4
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-26 00:00:00.000000000 Z
11
+ date: 2021-12-03 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