alphabetical_paginate 2.2.3 → 2.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63b2a6f96a526fed8baa1276a837d11bff0a5064
4
+ data.tar.gz: fec8f3848297ceda6e5230c1dfb6b5777de21105
5
+ SHA512:
6
+ metadata.gz: 88248d71f19d617dd2d34d1d633b55556d0dab449af255d8e1390b7fbbecd20e52783989cf93715acdbf4aea6f8b31fdadbc407c0514e04fb154c95f41b6d1c6
7
+ data.tar.gz: 3fbed90e8462cafc90b873eaab288675fec345c863fc4fa42077aa5002581befe2b8f27c113ded5705f656c9b919aaa93159bb3c97f2ee0acc8d7332b0f83d14
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -15,13 +15,21 @@ Some code was inspired by [will_paginate](https://github.com/mislav/will_paginat
15
15
 
16
16
  ## Installation
17
17
 
18
- Add this line to your application's Gemfile:
19
-
18
+ Add this line to your application's Gemfile:
19
+ ```
20
20
  gem 'alphabetical_paginate'
21
+ ```
21
22
 
22
- And then execute:
23
-
23
+ And then execute:
24
+ ```bash
24
25
  $ bundle install
26
+ ```
27
+
28
+ In case you're using the Rails 3.x assets pipeline remember to add it to your `production.rb` script:
29
+
30
+ ```rb
31
+ config.assets.precompile += %w( alphabetical_paginate.js )
32
+ ```
25
33
 
26
34
  ## Basic Setup
27
35
 
@@ -100,11 +108,11 @@ The available options are as follows:
100
108
 
101
109
  Key | Value | Default |Description
102
110
  --- | --- | --- | ---
103
- `:db_mode` | `Boolean` | `false` | Whether to activate low level SQL that are faster and more memory efficient (forces `:paginate_all` to enable)
111
+ `:db_mode` | `Boolean` | `false` | Whether to activate low level SQL that are faster and more memory efficient
104
112
  `:db_field` | `String` | `id` | Required if `db_mode` is `true`. The field to paginate / sort by (on the same collection).
105
113
  `:enumerate` | `Boolean` | `false` | Whether you want the number field collapsed (all numbers go into `0`) or separate (`0`, `1`, `2`...).
106
114
  `:default_field` | `String` | `"a"` | Which field you want the page to default to on first load (`"0"`, `"a"`. `"*"`).
107
- `:paginate_all` | `Boolean` | `false` | Whether you want empty fields to still render in pagination.
115
+ `:paginate_all` | `Boolean` | `false` | Whether you want empty fields to still render in pagination. If it's falsy and `db_mode` is thruty is will perform one more aggregation query: set it to true if performances matter.
108
116
  `:include_all` | `Boolean` | `true` | Whether you want `all` selector to be included in the pagination.
109
117
  `:numbers` | `Boolean` | `true` | Whether you want numbers to be included in the pagination at all, either collapsed, or expanded (depending on `:enumerate`).
110
118
  `:others` | `Boolean` | `true` | Whether you want all other characters (non alphanumeric) to be included in the pagination at all.
@@ -156,3 +164,11 @@ Also, there is one line in vendor/assets/javascripts that needs to be changed fo
156
164
  # Support
157
165
 
158
166
  Please feel free to reach out and contact if you find the gem useful at all! Also, feel free to report / fix any bugs or add features.
167
+
168
+ ## To run tests
169
+
170
+ rspec
171
+
172
+ ## To publish the gem
173
+
174
+ rake release
@@ -3,7 +3,8 @@ class Array
3
3
  def alpha_paginate current_field, params = {enumerate:false, default_field: "a",
4
4
  paginate_all: false, numbers: true, include_all: true,
5
5
  others: true, pagination_class: "pagination-centered",
6
- js: true, support_language: :en, bootstrap3: false}
6
+ js: true, support_language: :en, bootstrap3: false,
7
+ slugged_link: false, slug_field: "slug", all_as_link: true}
7
8
  params[:paginate_all] ||= false
8
9
  params[:support_language] ||= :en
9
10
  params[:language] = AlphabeticalPaginate::Language.new(params[:support_language])
@@ -13,21 +14,30 @@ class Array
13
14
  params[:js] = true if !params.has_key? :js
14
15
  params[:default_field] ||= params[:include_all] ? "all" : params[:language].default_letter
15
16
  params[:pagination_class] ||= "pagination-centered"
17
+ params[:slugged_link] ||= false
18
+ params[:slugged_link] = params[:slugged_link] && defined?(Babosa)
19
+ params[:slug_field] ||= "slug"
20
+ params[:all_as_link] = true if !params.has_key? :all_as_link
21
+
16
22
  output = []
17
23
  availableLetters = {}
18
- if current_field == nil
19
- current_field = params[:default_field]
20
- end
24
+
25
+ current_field ||= params[:default_field]
21
26
  current_field = current_field.mb_chars.downcase.to_s
22
27
  all = params[:include_all] && current_field == "all"
23
28
 
24
29
  self.each do |x|
30
+ slug = eval("x.#{params[:slug_field]}") if params[:slugged_link]
31
+
25
32
  field_val = block_given? ? yield(x).to_s : x.id.to_s
26
33
  field_letter = field_val[0].mb_chars.downcase.to_s
34
+
27
35
  case field_letter
28
36
  when params[:language].letters_regexp
29
37
  availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
30
- output << x if all || (current_field =~ params[:language].letters_regexp && field_letter == current_field)
38
+ regexp = params[:slugged_link] ? params[:language].slugged_regexp : params[:language].letters_regexp
39
+ field = params[:slugged_link] ? slug : field_letter
40
+ output << x if all || (current_field =~ regexp && current_field == field)
31
41
  when /[0-9]/
32
42
  if params[:enumerate]
33
43
  availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
@@ -4,13 +4,15 @@ module AlphabeticalPaginate
4
4
  base.extend(self)
5
5
  end
6
6
 
7
- def alpha_paginate current_field, params = {enumerate:false, default_field: "a",
7
+ def alpha_paginate current_field, params = {enumerate:false, default_field: "a",
8
8
  paginate_all: false, numbers: true,
9
9
  others: true, pagination_class: "pagination-centered",
10
- batch_size: 500, db_mode: false,
10
+ batch_size: 500, db_mode: false,
11
11
  db_field: "id", include_all: true,
12
12
  js: true, support_language: :en,
13
- bootstrap3: false}
13
+ bootstrap3: false, slugged_link: false,
14
+ slug_field: "slug", all_as_link: true}
15
+ params[:default_field] ||= "a"
14
16
  params[:paginate_all] ||= false
15
17
  params[:support_language] ||= :en
16
18
  params[:language] = AlphabeticalPaginate::Language.new(params[:support_language])
@@ -20,15 +22,30 @@ module AlphabeticalPaginate
20
22
  params[:js] = true if !params.has_key? :js
21
23
  params[:pagination_class] ||= "pagination-centered"
22
24
  params[:batch_size] ||= 500
23
- params[:default_field] ||= params[:include_all] ? "all" : params[:language].default_letter
24
25
  params[:db_mode] ||= false
25
26
  params[:db_field] ||= "id"
27
+ params[:slugged_link] ||= false
28
+ params[:slugged_link] = params[:slugged_link] && defined?(Babosa)
29
+ params[:slug_field] ||= "slug"
30
+ params[:all_as_link] = true if !params.has_key? :all_as_link
26
31
 
27
32
  output = []
28
33
 
29
- if current_field == nil
30
- current_field = params[:default_field]
34
+ if params[:db_mode]
35
+ letters = nil
36
+ if !params[:paginate_all]
37
+ letters = filter_by_cardinality( find_available_letters(params[:db_field]) )
38
+ set_default_field letters, params
39
+ end
40
+ params[:availableLetters] = letters.nil? ? [] : letters
41
+ end
42
+
43
+ if params[:include_all]
44
+ current_field ||= 'all'
45
+ all = current_field == "all"
31
46
  end
47
+
48
+ current_field ||= params[:default_field]
32
49
  current_field = current_field.mb_chars.downcase.to_s
33
50
  all = params[:include_all] && current_field == "all"
34
51
 
@@ -36,38 +53,46 @@ module AlphabeticalPaginate
36
53
  if !ActiveRecord::Base.connection.adapter_name.downcase.include? "mysql"
37
54
  raise "You need a mysql database to use db_mode with alphabetical_paginate"
38
55
  end
39
- params[:paginate_all] = true
40
- params[:availableLetters] = []
41
56
 
42
57
  if all
43
58
  output = self
44
59
  else
45
- case current_field
46
- when params[:language].letters_regexp
47
- output = self.where("LOWER(%s) REGEXP '^%s.*'" % [params[:db_field], current_field])
48
- when /[0-9]/
49
- if params[:enumerate]
50
- output = self.where("LOWER(%s) REGEXP '^%s.*'" % [params[:db_field], current_field])
51
- else
52
- output = self.where("LOWER(%s) REGEXP '^[0-9].*'" % [params[:db_field], current_field])
53
- end
60
+
61
+ # In this case we can speed up the search taking advantage of the indices
62
+ can_go_quicker = (current_field =~ params[:language].letters_regexp) || (current_field =~ /[0-9]/ && params[:enumerate])
63
+
64
+
65
+ # Use LIKE the most as you can to take advantage of indeces on the field when available
66
+ # REGEXP runs always a full scan of the table!
67
+ # For more information about LIKE and indeces have a look at
68
+ # http://myitforum.com/cs2/blogs/jnelson/archive/2007/11/16/108354.aspx
69
+
70
+ # Also use some sanitization from ActiveRecord for the current field passed
71
+ if can_go_quicker
72
+ output = self.where("LOWER(%s) LIKE ?" % params[:db_field], current_field+'%')
54
73
  else
55
- output = self.where("LOWER(%s) REGEXP '^[^a-z0-9].*'" % [params[:db_field], current_field])
74
+ regexp_to_check = current_field =~ /[0-9]/ ? '^[0-9]' : '^[^a-z0-9]'
75
+ output = self.where("LOWER(%s) REGEXP '%s.*'" % [params[:db_field], regexp_to_check])
56
76
  end
57
77
  end
58
78
  else
59
79
  availableLetters = {}
60
80
  self.find_each({batch_size: params[:batch_size]}) do |x|
81
+ slug = eval("x.#{params[:slug_field]}") if params[:slugged_link]
82
+
61
83
  field_val = block_given? ? yield(x).to_s : x.id.to_s
62
84
  field_letter = field_val[0].mb_chars.downcase.to_s
85
+
63
86
  case field_letter
64
87
  when params[:language].letters_regexp
65
88
  availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
66
- output << x if all || (current_field =~ params[:language].letters_regexp && field_letter == current_field)
89
+ regexp = params[:slugged_link] ? params[:language].slugged_regexp : params[:language].letters_regexp
90
+ field = params[:slugged_link] ? slug : field_letter
91
+ output << x if all || (current_field =~ regexp && current_field == field)
67
92
  when /[0-9]/
68
93
  if params[:enumerate]
69
94
  availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
70
- output << x if all || (current_field =~ /[0-9]/ && field_letter == current_field)
95
+ output << x if all || (current_field =~ /[0-9]/ && field_letter == current_field)
71
96
  else
72
97
  availableLetters['0-9'] = true if !availableLetters.has_key? 'numbers'
73
98
  output << x if all || current_field == "0-9"
@@ -83,5 +108,39 @@ module AlphabeticalPaginate
83
108
  params[:currentField] = current_field.mb_chars.capitalize.to_s
84
109
  return ((params[:db_mode] && params[:db_field]) ? output.order("#{params[:db_field]} ASC") : output), params
85
110
  end
111
+
112
+ private
113
+
114
+ def set_default_field(letters, params)
115
+ if letters.any?
116
+ params[:default_field] = letters.first
117
+ elsif params[:include_all]
118
+ params[:default_field] = 'all'
119
+ else
120
+ params[:default_field] = params[:language].default_letter
121
+ end
122
+ end
123
+
124
+ def filter_by_cardinality(letters)
125
+ letters.collect do |letter, count|
126
+ if count > 0
127
+ letter = letter.mb_chars.capitalize.to_s
128
+ (letter =~ /[A-Z]/).nil? ? '*' : letter
129
+ else
130
+ nil
131
+ end
132
+ # repass again to filter duplicates *
133
+ end.uniq
134
+ end
135
+
136
+ def find_available_letters(db_field)
137
+ # safe the field (look for the ActiveRecord valid attributes)
138
+ # but allow explict table name prefixes in order to avoid 'field list is ambiguous' errors
139
+ if db_field.nil? || !self.attribute_names.include?(db_field.to_s.split('.').last)
140
+ db_field = 'id'
141
+ end
142
+ criteria = Arel.sql("substr( %s, 1 , 1)" % db_field)
143
+ self.select(criteria).group(criteria).order(criteria).count(db_field)
144
+ end
86
145
  end
87
146
  end
@@ -1,6 +1,11 @@
1
1
  # coding: utf-8
2
2
  module AlphabeticalPaginate
3
3
  class Language
4
+ APPROXIMATIONS = {
5
+ "Э" => "je",
6
+ "Ю" => "yu"
7
+ }
8
+
4
9
  attr_reader :code
5
10
 
6
11
  def initialize(code)
@@ -15,6 +20,10 @@ module AlphabeticalPaginate
15
20
  russian? ? /[а-яА-Я]/ : /[a-zA-Z]/
16
21
  end
17
22
 
23
+ def slugged_regexp
24
+ /^(#{slugged_letters.values.join("|")})$/
25
+ end
26
+
18
27
  def default_letter
19
28
  russian? ? "а" : "a" # First 'a' is russian, second - english
20
29
  end
@@ -22,14 +31,18 @@ module AlphabeticalPaginate
22
31
  # used in view_helper
23
32
  def letters_range
24
33
  if russian?
25
- letters = []
26
- "АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЭЮЯ".each_char{ |x| letters << x }
27
- letters
34
+ ["А","Б","В","Г","Д","Е","Ж","З","И","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Э","Ю","Я"]
28
35
  else
29
- ('A'..'Z').to_a
36
+ ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
30
37
  end
31
38
  end
32
39
 
40
+ def slugged_letters
41
+ hash = { "All" => "all" }
42
+ letters_range.each{ |x| hash[x] = normalize(x) }
43
+ hash
44
+ end
45
+
33
46
  # used in view_helper
34
47
  def output_letter(l)
35
48
  (l == "All") ? all_field : l
@@ -39,5 +52,15 @@ module AlphabeticalPaginate
39
52
  def all_field
40
53
  russian? ? 'Все' : "All"
41
54
  end
55
+
56
+ private
57
+
58
+ def normalize(letter)
59
+ if russian?
60
+ APPROXIMATIONS[letter] || letter.to_s.to_slug.normalize(transliterations: :russian).to_s
61
+ else
62
+ letter.to_s.to_slug.normalize.to_s
63
+ end
64
+ end
42
65
  end
43
66
  end
@@ -1,3 +1,3 @@
1
1
  module AlphabeticalPaginate
2
- VERSION = "2.2.3"
2
+ VERSION = "2.3.4"
3
3
  end
@@ -1,62 +1,81 @@
1
1
  # coding: utf-8
2
2
  module AlphabeticalPaginate
3
3
  module ViewHelpers
4
- def alphabetical_paginate params
4
+ def alphabetical_paginate(options = {})
5
5
  output = ""
6
6
  links = ""
7
+ output += javascript_include_tag 'alphabetical_paginate' if options[:js] == true
8
+ options[:scope] ||= main_app
7
9
 
8
- output += javascript_include_tag 'alphabetical_paginate' if params[:js]
9
-
10
- if params[:paginate_all]
11
- range = params[:language].letters_range
12
- if params[:others]
10
+ if options[:paginate_all]
11
+ range = options[:language].letters_range
12
+ if options[:others]
13
13
  range += ["*"]
14
14
  end
15
- if params[:enumerate] && params[:numbers]
15
+ if options[:enumerate] && options[:numbers]
16
16
  range = (0..9).to_a.map{|x| x.to_s} + range
17
- elsif params[:numbers]
17
+ elsif options[:numbers]
18
18
  range = ["0-9"] + range
19
19
  end
20
- range.unshift "All" if (params[:include_all] && !range.include?("All"))
20
+ range.unshift "All" if (options[:include_all] && !range.include?("All"))
21
21
  range.each do |l|
22
- value = params[:language].output_letter(l)
23
- if l == params[:currentField]
24
- links += '<li class="active"><a href="#" data-letter="' + l + '">' + value + "</a></li>"
25
- elsif params[:db_mode] or params[:availableLetters].include? l
26
- links += '<li><a href="?letter=' + l + '" data-letter="' + l + '">' + value + "</a></li>"
22
+ link_letter = l
23
+ if options[:slugged_link] && (l =~ options[:language].letters_regexp || l == "All")
24
+ link_letter = options[:language].slugged_letters[l]
25
+ end
26
+ letter_options = { letter: link_letter }
27
+ if !options[:all_as_link] && (l == "All")
28
+ letter_options[:letter] = nil
29
+ end
30
+
31
+ url = options[:scope].url_for(letter_options)
32
+ value = options[:language].output_letter(l)
33
+ if l == options[:currentField]
34
+ links += content_tag(:li, link_to(value, "#", "data-letter" => l), :class => "active")
35
+ elsif options[:db_mode] or options[:availableLetters].include? l
36
+ links += content_tag(:li, link_to(value, url, "data-letter" => l))
27
37
  else
28
- links += '<li class="disabled"><a href="?letter=' + l + '" data-letter="' + l + '">' + value + "</a></li>"
38
+ links += content_tag(:li, link_to(value, url, "data-letter" => l), :class => "disabled")
29
39
  end
30
40
  end
31
41
  else
32
- params[:availableLetters].sort!
33
- params[:availableLetters] = params[:availableLetters][1..-1] + ["*"] if params[:availableLetters][0] == "*"
34
- params[:availableLetters].unshift "All" if (params[:include_all] && !params[:availableLetters].include?("All"))
35
- params[:availableLetters] -= (1..9).to_a.map{|x| x.to_s} if !params[:numbers]
36
- params[:availableLetters] -= ["*"] if !params[:others]
42
+ options[:availableLetters].sort!
43
+ options[:availableLetters] = options[:availableLetters][1..-1] + ["*"] if options[:availableLetters][0] == "*"
44
+ #Ensure that "All" is always at the front of the array
45
+ if options[:include_all]
46
+ options[:availableLetters].delete("All") if options[:availableLetters].include?("All")
47
+ options[:availableLetters].unshift("All")
48
+ end
49
+ options[:availableLetters] -= (1..9).to_a.map{|x| x.to_s} if !options[:numbers]
50
+ options[:availableLetters] -= ["*"] if !options[:others]
37
51
 
38
- params[:availableLetters].each do |l|
39
- value = params[:language].output_letter(l)
40
- if l == params[:currentField]
41
- links += '<li class="active"><a href="?letter=' + l + '" data-letter="' + l + '">' + value + '</a></li>'
42
- else
43
- links += '<li><a href="?letter=' + l + '" data-letter="' + l + '">' + value + "</a></li>"
52
+ options[:availableLetters].each do |l|
53
+ link_letter = l
54
+ if options[:slugged_link] && (l =~ options[:language].letters_regexp || l == "All")
55
+ link_letter = options[:language].slugged_letters[l]
56
+ end
57
+ letter_options = { letter: link_letter }
58
+ if !options[:all_as_link] && (l == "All")
59
+ letter_options[:letter] = nil
44
60
  end
61
+
62
+ url = options[:scope].url_for(letter_options)
63
+ value = options[:language].output_letter(l)
64
+ links += content_tag(:li, link_to(value, url, "data-letter" => l), :class => ("active" if l == options[:currentField] ))
45
65
  end
46
66
  end
47
67
 
48
-
49
- element = params[:bootstrap3] ? 'ul' : 'div'
50
- if params[:pagination_class] != "none"
51
- pagination = "<#{element} class='pagination %s alpha' style='height:35px;'>" % params[:pagination_class]
68
+ element = options[:bootstrap3] ? 'ul' : 'div'
69
+ if options[:pagination_class] != "none"
70
+ pagination = "<#{element} class='pagination %s alpha' style='height:35px;'>" % options[:pagination_class]
52
71
  else
53
72
  pagination = "<#{element} class='pagination alpha' style='height:35px;'>"
54
73
  end
55
74
  pagination +=
56
- (params[:bootstrap3] ? "" : "<ul>") +
75
+ (options[:bootstrap3] ? "" : "<ul>") +
57
76
  links +
58
- (params[:bootstrap3] ? "" : "</ul>") +
59
- (params[:bootstrap3] ? "</ul>" : "</div>")
77
+ (options[:bootstrap3] ? "" : "</ul>") +
78
+ "</#{element}>"
60
79
 
61
80
  output += pagination
62
81
  output.html_safe
@@ -13,6 +13,25 @@ class String
13
13
  end
14
14
  end
15
15
 
16
+ class RouterMock
17
+ def url_for(options)
18
+ options[:letter] ? '?letter='+options[:letter] : '/'
19
+ end
20
+ end
21
+
22
+ def main_app
23
+ RouterMock.new()
24
+ end
25
+
26
+ def link_to(value, url, options)
27
+ "<a href='#{url}' data-letter=\"#{options["data-letter"]}\">#{value}</a>"
28
+ end
29
+
30
+ def content_tag(type, el, html_options={})
31
+ "<#{type.to_s} class='#{html_options[:class] || ''}'>#{el}</#{type.to_s}>"
32
+ end
33
+
34
+
16
35
  module AlphabeticalPaginate
17
36
 
18
37
  describe AlphabeticalPaginate do
@@ -36,7 +55,7 @@ module AlphabeticalPaginate
36
55
  enumerate: false,
37
56
  }
38
57
  collection, params = @list.alpha_paginate("b")
39
- collection.to_s.should ==
58
+ collection.to_s.should ==
40
59
  expectedCollection.to_s
41
60
  params.to_s.should include
42
61
  expectedParams.to_s
@@ -54,7 +73,7 @@ module AlphabeticalPaginate
54
73
  collection, params = @list.alpha_paginate("b") do |x|
55
74
  x.word
56
75
  end
57
- collection.to_s.should ==
76
+ collection.to_s.should ==
58
77
  expectedCollection.to_s
59
78
  params.to_s.should include
60
79
  expectedParams.to_s
@@ -81,7 +100,7 @@ module AlphabeticalPaginate
81
100
  enumerate: false,
82
101
  }
83
102
  collection, params = @list.alpha_paginate("с", { support_language: :ru })
84
- collection.to_s.should ==
103
+ collection.to_s.should ==
85
104
  expectedCollection.to_s
86
105
  params.to_s.should include
87
106
  expectedParams.to_s
@@ -97,7 +116,7 @@ module AlphabeticalPaginate
97
116
  enumerate: false,
98
117
  }
99
118
  collection, params = @list.alpha_paginate(nil, { include_all: false, support_language: :ru })
100
- collection.to_s.should ==
119
+ collection.to_s.should ==
101
120
  expectedCollection.to_s
102
121
  params.to_s.should include
103
122
  expectedParams.to_s
@@ -125,6 +144,20 @@ module AlphabeticalPaginate
125
144
  pagination.should include "div", "pagination"
126
145
  end
127
146
 
147
+ it "should open and close the div tag" do
148
+ index, params = @list.alpha_paginate(nil)
149
+ pagination = alphabetical_paginate(params)
150
+ pagination.start_with?('<div').should be_truthy
151
+ pagination.end_with?('</div>').should be_truthy
152
+ end
153
+
154
+ it "should open and close the ul tag when run in bootstrap3 mode" do
155
+ index, params = @list.alpha_paginate(nil)
156
+ pagination = alphabetical_paginate(params.merge(bootstrap3: true))
157
+ pagination.start_with?('<ul').should be_truthy
158
+ pagination.end_with?('</ul>').should be_truthy
159
+ end
160
+
128
161
  it "should include a numbers and others field" do
129
162
  index, params = @list.alpha_paginate(nil)
130
163
  pagination = alphabetical_paginate(params)
@@ -182,6 +215,18 @@ module AlphabeticalPaginate
182
215
  pagination.should include x
183
216
  end
184
217
  end
218
+
219
+ it "should include All as link" do
220
+ index, params = @list.alpha_paginate("A", { include_all: true })
221
+ pagination = alphabetical_paginate(params)
222
+ pagination.should include "href='?letter=All'"
223
+ end
224
+
225
+ it "should not include All as link" do
226
+ index, params = @list.alpha_paginate("A", { include_all: true, all_as_link: false })
227
+ pagination = alphabetical_paginate(params)
228
+ pagination.should_not include "href='?letter=All'"
229
+ end
185
230
  end
186
231
 
187
232
  describe "#alphabetical_paginate in russian characters" do
@@ -3,6 +3,7 @@ require 'alphabetical_paginate'
3
3
 
4
4
  module AlphabeticalPaginate
5
5
  describe Language do
6
+
6
7
  context "English language" do
7
8
  before(:all) do
8
9
  I18n.locale = :en
@@ -0,0 +1,18 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.include Rails.application.routes.url_helpers
12
+
13
+ # Run specs in random order to surface order dependencies. If you find an
14
+ # order dependency and want to debug it, you can fix the order by providing
15
+ # the seed, which is printed after each run.
16
+ # --seed 1234
17
+ config.order = 'random'
18
+ end
@@ -1,5 +1,6 @@
1
1
  $(function() {
2
- var once = false;
2
+ // deal with old browsers
3
+ var hasHistory = !!(window.history && window.history.pushState);
3
4
 
4
5
  var img = "<img src='/assets/aloader.gif' class='loading'/>";
5
6
  // RAILS 3.0 USERS -> Please delete the above line and uncomment the bottom line
@@ -12,44 +13,52 @@ $(function() {
12
13
 
13
14
  if (!handlers || -1 !== $.inArray(onNavbarClick, handlers.click)) {
14
15
  $(document).on("click", ".pagination.alpha a", onNavbarClick);
16
+ if(hasHistory){
17
+ // bind the popstate
18
+ bindPopState(location.href);
19
+ }
15
20
  }
16
21
 
17
22
  function onNavbarClick(e) {
18
23
  e.preventDefault();
19
- var url = location.href,
20
- letter = $(this).data("letter");
24
+ var url = location.href,
25
+ letter = $(this).data("letter");
21
26
  if (/letter/.test(url)) {
22
27
  url = url.replace(/letter=[^&]*/, "letter=" + letter);
23
- }
24
- else {
28
+ } else {
25
29
  if (/\?/.test(url)) {
26
30
  url += "&letter=" + letter;
27
- }
28
- else {
31
+ } else {
29
32
  url += "?letter=" + letter;
30
33
  }
31
34
  }
32
- $(".pagination").html(img);
33
- //$.load(url + " #pagination_table");
34
- $.get(url, function(result) {
35
- $(".pagination").html($(".pagination", result).html());
36
- $("#pagination_table").html($("#pagination_table", result).html());
37
- });
38
- history.pushState(null, document.title, url);
35
+ loadPage(url);
36
+ // deal with browser support
37
+ if(hasHistory){
38
+ history.pushState(null, document.title, url);
39
+ }
39
40
  }
40
41
 
41
- // let navigate the browser throught the ajax history
42
- $(window).bind("popstate", function() {
43
- if (once) {
44
- $(".pagination").html(img);
45
- $.get(location.href, function(result) {
46
- $(".pagination").html($(".pagination", result).html());
47
- $("#pagination_table").html($("#pagination_table", result).html());
48
- });
49
- } else {
50
- once = true;
42
+ // let navigate the browser throught the ajax history
43
+ function bindPopState(initialUrl){
44
+ $(window).bind("popstate", function() {
45
+ var newUrl = location.href;
46
+ var diff = newUrl.replace(initialUrl, '');
47
+ // skip initial popstate
48
+ // skip anchor links (used for JS links)
49
+ if (diff !== '' && diff !== '#') {
50
+ loadPage(newUrl);
51
51
  }
52
- });
52
+ });
53
+ }
54
+
55
+ function loadPage(url){
56
+ $(".pagination").html(img);
57
+ $.get(url, function (result) {
58
+ $(".pagination").html($(".pagination", result).html());
59
+ $("#pagination_table").html($("#pagination_table", result).html());
60
+ });
61
+ }
53
62
 
54
63
 
55
64
  });
metadata CHANGED
@@ -1,78 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphabetical_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
5
- prerelease:
4
+ version: 2.3.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - lingz
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-07-13 00:00:00.000000000 Z
11
+ date: 2019-09-27 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '2.6'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '2.6'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rails
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: Alphabetical Pagination
@@ -82,7 +73,8 @@ executables: []
82
73
  extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
- - .gitignore
76
+ - ".gitignore"
77
+ - ".rspec"
86
78
  - Gemfile
87
79
  - LICENSE.txt
88
80
  - README.md
@@ -99,6 +91,7 @@ files:
99
91
  - spec/alpha_example.rb
100
92
  - spec/alphabetical_paginate_spec.rb
101
93
  - spec/language_spec.rb
94
+ - spec/spec_helper.rb
102
95
  - spec/support/helpers.rb
103
96
  - vendor/assets/images/aloader.gif
104
97
  - vendor/assets/javascripts/alphabetical_paginate.js
@@ -106,30 +99,30 @@ files:
106
99
  homepage: https://github.com/lingz/alphabetical_paginate
107
100
  licenses:
108
101
  - MIT
102
+ metadata: {}
109
103
  post_install_message:
110
104
  rdoc_options: []
111
105
  require_paths:
112
106
  - lib
113
107
  required_ruby_version: !ruby/object:Gem::Requirement
114
- none: false
115
108
  requirements:
116
- - - ! '>='
109
+ - - ">="
117
110
  - !ruby/object:Gem::Version
118
111
  version: '0'
119
112
  required_rubygems_version: !ruby/object:Gem::Requirement
120
- none: false
121
113
  requirements:
122
- - - ! '>='
114
+ - - ">="
123
115
  - !ruby/object:Gem::Version
124
116
  version: '0'
125
117
  requirements: []
126
118
  rubyforge_project:
127
- rubygems_version: 1.8.25
119
+ rubygems_version: 2.6.14
128
120
  signing_key:
129
- specification_version: 3
121
+ specification_version: 4
130
122
  summary: Pagination
131
123
  test_files:
132
124
  - spec/alpha_example.rb
133
125
  - spec/alphabetical_paginate_spec.rb
134
126
  - spec/language_spec.rb
127
+ - spec/spec_helper.rb
135
128
  - spec/support/helpers.rb