alphabetical_paginate 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.swp
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # AlphabeticalPaginate
2
2
 
3
- TODO: Write a gem description
3
+ A lightweight and highly customizable pagination gem for Ruby on Rails that generates alphabetic pagination categories from collection arrays. It allows you to select the field you want to paginate on.
4
+
5
+ By default, it works with [Bootstrap Pagination](http://twitter.github.io/bootstrap/components.html#pagination) CSS and it is also fully compatible with the [will_paginate](https://github.com/mislav/will_paginate) in case you want to use both.
6
+
7
+ AlphabeticalPaginate incorporates efficient partial page rerendering techniques and loading animations.
8
+
9
+ Some code was inspired by [will_paginate](https://github.com/mislav/will_paginate).
10
+
4
11
 
5
12
  ## Installation
6
13
 
@@ -10,20 +17,104 @@ Add this line to your application's Gemfile:
10
17
 
11
18
  And then execute:
12
19
 
13
- $ bundle
20
+ $ bundle install
21
+
22
+ ## Basic Setup
23
+
24
+ Basic Pagination is as simple as adding a few lines to the controller and the view.
25
+
26
+ ### Controller
27
+ You simply need to call alpha_paginate on the desired table (i.e. `User.all.alpha_paginate`). This method takes two parameters, the first always being `params[:letter]` and the second being an optional options hash.
28
+
29
+ Also, it takes a block in which you can specify the field you wish to paginate by (it can even be in another table). It returns the paginated subset of the collection, sorted by the pagination field. The method returns two values (the paginated array subsection, and an options hash), both of which must be stored as class variables.
30
+
31
+ *An example of its use is as such:*
32
+ ```ruby
33
+ #app/controllers/users_controllers.rb
34
+ class UsersController < ApplicationController
35
+
36
+ def index
37
+ @users, @alphaParams = User.all.alpha_paginate(params[:letter]){|user| user.name}
38
+ end
39
+
40
+ ...
41
+ end
42
+ ```
43
+
44
+ ### View
45
+ You need to call `alphabetical_paginate` that we just generated in the controller (i.e `<%= alphabetical_paginate @alphaParams %>`) in the view, whereever you would like to render the pagination selector div. You also **must wrap the content you want paginated in a div with id="pagination_table"**.
46
+
47
+ You can place as many `<%= alphabetical_paginate @alphaParams =>` as you wish on the page, if you want to render multiple pagination divs.
48
+
49
+ *An example is as such:*
50
+ ```html
51
+ #app/controllers/users/index.html.erb
52
+
53
+ <%= alphabetical_paginate @alphaParams %>
54
+
55
+ <div id="pagination_table">
56
+ <% User.all.each do |user| %>
57
+ ...
58
+ <% end %>
59
+ </div>
60
+ ```
61
+
62
+ ## Customization
63
+
64
+ ### Options
65
+ The gem is highly customizable. The `alpha_paginate ` method takes a hash as an optional second parameter like such:
66
+
67
+ ```ruby
68
+ #app/controllers/users_controllers.rb
69
+ class UsersController < ApplicationController
70
+
71
+ def index
72
+ @users, @alphaParams = User.all.alpha_paginate(params[:letter], {:enumerate => false}){|user| user.name}
73
+ end
74
+
75
+ ...
76
+ end
77
+ ```
78
+
79
+ The available options are as follows:
80
+
81
+ Key | Value | Default |Description
82
+ --- | --- | --- | ---
83
+ `:enumerate` | `Boolean` | `false` | Whether you want the number field collapsed (all numbers go into `0`) or separate (`0`, `1`, `2`...).
84
+ `:default_field` | `String` | `"a"` | Which field you want the page to default to on first load (`"0"`, `"a"`. `"*"`).
85
+ `:paginate_all` | `Boolean` | `false` | Whether you want empty fields to still render in pagination.
86
+ `:numbers` | `Boolean` | `true` | Whether you want numbers to be included in the pagination at all, either collapsed, or expanded (depending on `:enumerate`).
87
+ `:others` | `Boolean` | `true` | Whether you want all other characters (non alphanumeric) to be included in the pagination at all.
88
+ `:pagination_class` | `String` | `"pagination-centered"` | All the classes you would like to add to the rendered pagination selector div (for CSS purposes).
89
+
90
+ ## Advanced Pagination
91
+
92
+ You can select a complex field to paginate by. Be careful, as this may be slow in large data sets.
93
+
94
+ For instance, the following example paginates posts by the author's group's name (jumping across two tables).
95
+ It still returns the Post objects despite whatever field you use to paginate / sort it by (It still auto-sorts by the pagination field).
96
+ ```ruby
97
+ #app/controllers/users_controllers.rb
98
+ class UsersController < ApplicationController
14
99
 
15
- Or install it yourself as:
100
+ def index
101
+ @posts, @alphaParams = Post.all.alpha_paginate(params[:letter]) do |post|
102
+ author = post.author
103
+ group = author.group
104
+ end
105
+ end
106
+
107
+ ...
108
+ end
109
+ ```
16
110
 
17
- $ gem install alphabetical_paginate
111
+ ## Rails 3.0 and Lower
18
112
 
19
- ## Usage
113
+ The gem makes use of the Asset Pipeline, introduced in Rails 3.1. It still works with lower versions of Rails, however, you need to copy the assets over manually.
20
114
 
21
- TODO: Write usage instructions here
115
+ Copy the contents of `vendor/assets/javascripts` of this repo into the `public/javascripts` of your app
116
+ and also copy the contents of `vendor/assets/images` of this repo into the `public/images` of your app.
22
117
 
23
- ## Contributing
118
+ # Support
24
119
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
120
+ 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.
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["lz781@nyu.edu"]
11
11
  spec.description = "Alphabetical Pagination"
12
12
  spec.summary = "Pagination"
13
- spec.homepage = "http://google.com"
13
+ spec.homepage = "https://github.com/lingz/alphabetical_paginate"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -33,6 +33,6 @@ class Array
33
33
  end
34
34
  params[:availableLetters] = availableLetters.collect{|k,v| k.to_s}
35
35
  params[:currentField] = current_field
36
- return output, params
36
+ return output.sort, params
37
37
  end
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module AlphabeticalPaginate
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
Binary file
@@ -1,45 +1,45 @@
1
1
  $(function() {
2
- var once = false;
2
+ var once = false;
3
3
 
4
- var img = "<img src='/images/loader.gif' class='loading'/>"
5
-
6
- $(document).on("click", ".pagination#alpha a", function(e) {
7
- var url = location.href,
8
- letter = $(this).data("letter");
9
- if (/letter/.test(url)){
10
- url = url.replace(/letter=?./,"letter=" + letter);
4
+ var img = "<img src='/images/aloader.gif' class='loading'/>";
5
+
6
+ $(document).on("click", ".pagination#alpha a", function(e) {
7
+ var url = location.href,
8
+ letter = $(this).data("letter");
9
+ if (/letter/.test(url)){
10
+ url = url.replace(/letter=?./,"letter=" + letter);
11
+ }
12
+ else {
13
+ if (/\?/.test(url)) {
14
+ url += "&letter=" + letter;
11
15
  }
12
16
  else {
13
- if (/\?/.test(url)) {
14
- url += "&letter=" + letter
15
- }
16
- else {
17
- url += "?letter=" + letter
18
- }
17
+ url += "?letter=" + letter;
19
18
  }
20
- $(".pagination").html(img);
21
- //$.load(url + " #pagination_table");
22
- $.get(url, function(result) {
23
- $(".pagination").html($(".pagination", result));
24
- $("#pagination_table").html($("#pagination_table", result));
25
- });
26
- history.pushState(null, document.title, url);
27
- e.preventDefault();
28
- });
29
-
30
-
31
- // let navigate the browser throught the ajax history
32
- $(window).bind("popstate", function() {
33
- if (once) {
34
- $(".pagination").html(img);
35
- $.get(location.href, function(result) {
36
- $(".pagination").html($(".pagination", result));
19
+ }
20
+ $(".pagination").html(img);
21
+ //$.load(url + " #pagination_table");
22
+ $.get(url, function(result) {
23
+ $(".pagination").html($(".pagination", result));
37
24
  $("#pagination_table").html($("#pagination_table", result));
38
25
  });
39
- } else {
40
- once = true;
41
- }
42
- });
26
+ history.pushState(null, document.title, url);
27
+ e.preventDefault();
28
+ });
29
+
30
+
31
+ // let navigate the browser throught the ajax history
32
+ $(window).bind("popstate", function() {
33
+ if (once) {
34
+ $(".pagination").html(img);
35
+ $.get(location.href, function(result) {
36
+ $(".pagination").html($(".pagination", result));
37
+ $("#pagination_table").html($("#pagination_table", result));
38
+ });
39
+ } else {
40
+ once = true;
41
+ }
42
+ });
43
43
 
44
44
 
45
45
  });
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: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,21 +89,18 @@ files:
89
89
  - Rakefile
90
90
  - alphabetical_paginate.gemspec
91
91
  - lib/alphabetical_paginate.rb
92
- - lib/alphabetical_paginate/.array.rb.swp
93
- - lib/alphabetical_paginate/.view_helpers.rb.swp
94
92
  - lib/alphabetical_paginate/array.rb
95
93
  - lib/alphabetical_paginate/engine.rb
96
94
  - lib/alphabetical_paginate/railtie.rb
97
95
  - lib/alphabetical_paginate/version.rb
98
96
  - lib/alphabetical_paginate/view_helpers.rb
99
- - spec/.alpha_example.rb.swp
100
- - spec/.alphabetical_paginate_spec.rb.swp
101
97
  - spec/alpha_example.rb
102
98
  - spec/alphabetical_paginate_spec.rb
103
99
  - spec/support/helpers.rb
100
+ - vendor/assets/images/aloader.gif
104
101
  - vendor/assets/javascripts/alphabetical_paginate.js
105
102
  - vendor/assets/javascripts/index.js
106
- homepage: http://google.com
103
+ homepage: https://github.com/lingz/alphabetical_paginate
107
104
  licenses:
108
105
  - MIT
109
106
  post_install_message:
@@ -129,8 +126,6 @@ signing_key:
129
126
  specification_version: 3
130
127
  summary: Pagination
131
128
  test_files:
132
- - spec/.alpha_example.rb.swp
133
- - spec/.alphabetical_paginate_spec.rb.swp
134
129
  - spec/alpha_example.rb
135
130
  - spec/alphabetical_paginate_spec.rb
136
131
  - spec/support/helpers.rb
Binary file