alphabetical_paginate 0.2.8 → 0.2.10

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.
@@ -2,11 +2,14 @@ class Array
2
2
  def alpha_paginate current_field, params = {enumerate:false, default_field: "a",
3
3
  paginate_all: false, numbers: true,
4
4
  others: true, pagination_class: "pagination-centered"}
5
+ params[:paginate_all] ||= false
6
+ params[:numbers] = true if !params.has_key? :numbers
7
+ params[:others] = true if !params.has_key? :numbers
8
+ params[:pagination_class] ||= "pagination-centered"
5
9
  output = []
6
- params = {}
7
10
  availableLetters = {}
8
11
  if current_field == nil
9
- current_field = default_field
12
+ current_field = params[:default_field]
10
13
  end
11
14
  self.each do |x|
12
15
  field_val = block_given? ? yield(x).to_s : x.id.to_s
@@ -20,20 +23,16 @@ class Array
20
23
  availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
21
24
  output << x if current_field =~ /[0-9]/ && field_letter == current_field
22
25
  else
23
- availableLetters['numbers'] = true if !availableLetters.has_key? 'numbers'
24
- output << x if current_field == "numbers"
26
+ availableLetters['0'] = true if !availableLetters.has_key? 'numbers'
27
+ output << x if current_field == "0"
25
28
  end
26
29
  else
27
- availableLetters['other'] = true if !availableLetters.has_key? 'other'
28
- output << x if current_field == "other"
30
+ availableLetters['#'] = true if !availableLetters.has_key? 'other'
31
+ output << x if current_field == "#"
29
32
  end
30
33
  end
31
34
  params[:availableLetters] = availableLetters.collect{|k,v| k.to_s}
32
35
  params[:currentField] = current_field
33
- params[:paginate_all] ||= false
34
- params[:numbers] ||= true
35
- params[:others] ||= true
36
- params[:pagination_class] ||= "pagination-centered"
37
36
  return output, params
38
37
  end
39
38
  end
@@ -1,3 +1,3 @@
1
1
  module AlphabeticalPaginate
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.10"
3
3
  end
@@ -3,6 +3,7 @@ module AlphabeticalPaginate
3
3
  def alphabetical_paginate params
4
4
 
5
5
  output = javascript_include_tag 'alphabetical_paginate'
6
+ links = ""
6
7
 
7
8
  if params[:paginate_all]
8
9
  range = ('a'..'z').to_a
@@ -14,16 +15,17 @@ module AlphabeticalPaginate
14
15
  elsif params[:numbers]
15
16
  range = ["0"] + range
16
17
  end
17
- range.each.do |l|
18
+ range.each do |l|
18
19
  if l == params[:currentField]
19
20
  links += '<li class="active"><a href="#" data-letter="' + l + '">' + l + "</a></li>\n"
20
- elsif params[:availableLetters].contains? l
21
+ elsif params[:availableLetters].include? l
21
22
  links += '<li><a href="#" data-letter="' + l + '">' + l + "</a></li>\n"
22
23
  else
23
24
  links += '<li class="disabled"><a href="#" data-letter="' + l + '">' + l + "</a></li>\n"
24
25
  end
26
+ end
25
27
  else
26
- params[:availableLetters] -= (0..9).to_a.map{|x| x.to_s} if !params[:numbers]
28
+ params[:availableLetters] -= (1..9).to_a.map{|x| x.to_s} if !params[:numbers]
27
29
  params[:availableLetters] -= ["#"] if !params[:others]
28
30
 
29
31
  params[:availableLetters].each do |l|
@@ -43,8 +45,8 @@ module AlphabeticalPaginate
43
45
  end
44
46
  pagination +=
45
47
  "<ul>\n" +
46
- "<li><a id='paginate-prev' href='#'>Prev</a></li>"
47
- "%s" % links +
48
+ "<li><a id='paginate-prev' href='#'>Prev</a></li>" +
49
+ links +
48
50
  "</ul\n" +
49
51
  "<li><a id='paginate-next' href='#'>Next</a></li>" +
50
52
  "</div>"
@@ -1,5 +1,16 @@
1
1
  require 'alpha_example'
2
2
  require 'alphabetical_paginate'
3
+ require_relative '#{File.dirname(__FILE__)}/../../lib/alphabetical_paginate/view_helpers'
4
+
5
+ # stub methods
6
+ def javascript_include_tag x
7
+ return ""
8
+ end
9
+ class String
10
+ def html_safe
11
+ return self
12
+ end
13
+ end
3
14
 
4
15
  module AlphabeticalPaginate
5
16
 
@@ -48,6 +59,78 @@ module AlphabeticalPaginate
48
59
  expectedParams.to_s
49
60
  end
50
61
  end
62
+ include ViewHelpers
63
+
64
+ describe "#alphabetical_paginate" do
65
+ before :each do
66
+ @list = []
67
+ (["@!#"] + (0..9).to_a.map{|x| x.to_s} + ("a".."z").to_a).each do |x|
68
+ ("a".."y").to_a.each do |y|
69
+ @list << x + y
70
+ end
71
+ end
72
+ @list.map! do |x|
73
+ AlphaExample.new(x)
74
+ end
75
+ end
76
+
77
+ it "should have div tags and pagination classes" do
78
+ index, params = @list.alpha_paginate(nil)
79
+ pagination = alphabetical_paginate(params)
80
+ pagination.should include "div", "pagination"
81
+ end
82
+
83
+ it "should include a numbers and others field" do
84
+ index, params = @list.alpha_paginate(nil)
85
+ pagination = alphabetical_paginate(params)
86
+ (["#"] + ["0"] + ("a".."z").to_a.map{|x|
87
+ 'data-letter="%s"'%x}).each do |x|
88
+ pagination.should include x
89
+ end
90
+ end
91
+
92
+ it "should default all values when necessary" do
93
+ index, params = @list.alpha_paginate(nil, {})
94
+ pagination = alphabetical_paginate(params)
95
+ (["#"] + ["0"] + ("a".."z").to_a.map{|x|
96
+ 'data-letter="%s"'%x}).each do |x|
97
+ pagination.should include x
98
+ end
99
+ end
100
+
101
+ it "should hide values that don't exist" do
102
+ index, params = @list.alpha_paginate(nil){|x| x.word}
103
+ pagination = alphabetical_paginate(params)
104
+ (("a".."y").to_a.map{|x|
105
+ 'data-letter="%s"'%x}).each do |x|
106
+ pagination.should include x
107
+ end
108
+ pagination.should_not include 'data-letter="z"', 'data-letter="#"',
109
+ 'data-letter="0"'
110
+ end
111
+
112
+ it "should enumerate when asked" do
113
+ index, params = @list.alpha_paginate(nil, {enumerate: true})
114
+ pagination = alphabetical_paginate(params)
115
+ (("0".."9").to_a.map{|x|
116
+ 'data-letter="%s"'% x.to_s }).each do |x|
117
+ pagination.should include x
118
+ end
119
+
120
+ end
121
+ it "should display all when asked" do
122
+ index, params = @list.alpha_paginate(nil, {paginate_all: true,
123
+ enumerate: true}){|x| x.word}
124
+ pagination = alphabetical_paginate(params)
125
+ (["#"] + (0..9).to_a.map{|x| x.to_s} + ("a".."z").to_a.map{|x|
126
+ 'data-letter="%s"'%x}).each do |x|
127
+ pagination.should include x
128
+ end
129
+ end
130
+
131
+
132
+
133
+ end
134
+ end
51
135
 
52
- end
53
136
  end
@@ -1,3 +1,4 @@
1
1
  module Helpers
2
- include ActionView::Base
2
+ include ActionView::Base::ViewHelpers
3
+ include ActionView::Helpers::AssetTagHelper
3
4
  end
@@ -1 +1,15 @@
1
- console.log("test");
1
+ $(function() {
2
+ var text = "<span class='loading'>Loading...</span>"
3
+
4
+ $(".pagination a").live("click", function(e) {
5
+ jQuery.getScript(location.href.replace("(letter=?)", "letter=" + this.data("letter")));
6
+ jQuery(".pagination").html(text);
7
+ history.pushState(null, document.title, location.href.replace("(letter=?)", "letter=" + this.data("letter")));
8
+ e.preventDefault();
9
+ });
10
+
11
+ $(window).bind("popstate", function() {
12
+ $.getScript(location.href);
13
+ $(".pagination").html(text);
14
+ });
15
+ });
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphabetical_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,6 +89,8 @@ files:
89
89
  - Rakefile
90
90
  - alphabetical_paginate.gemspec
91
91
  - lib/alphabetical_paginate.rb
92
+ - lib/alphabetical_paginate/.array.rb.swo
93
+ - lib/alphabetical_paginate/.view_helpers.rb.swo
92
94
  - lib/alphabetical_paginate/array.rb
93
95
  - lib/alphabetical_paginate/engine.rb
94
96
  - lib/alphabetical_paginate/railtie.rb
@@ -97,6 +99,7 @@ files:
97
99
  - spec/alpha_example.rb
98
100
  - spec/alphabetical_paginate_spec.rb
99
101
  - spec/support/helpers.rb
102
+ - vendor/assets/javascripts/.alphabetical_paginate.js.swp
100
103
  - vendor/assets/javascripts/alphabetical_paginate.js
101
104
  - vendor/assets/javascripts/index.js
102
105
  homepage: http://google.com