paginate_me 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +55 -53
- data/lib/paginate_me/paginate.rb +41 -30
- data/lib/paginate_me/version.rb +1 -1
- metadata +20 -39
data/README.markdown
CHANGED
@@ -1,68 +1,70 @@
|
|
1
|
-
|
1
|
+
# Paginate Me 0.0.5
|
2
2
|
|
3
|
-
**Adam Rensel's Code**
|
3
|
+
**Adam Rensel's Code**
|
4
4
|
|
5
|
-
|
5
|
+
Paginate me is a Ruby Gem that adds simple pagination functionality to views.
|
6
6
|
|
7
|
+
## Usage:
|
8
|
+
### Controller Code (users_controller.rb)
|
7
9
|
|
10
|
+
```ruby
|
11
|
+
class UsersController < ApplicationController
|
12
|
+
def index
|
13
|
+
@title = "All Users"
|
14
|
+
paginate_me :users
|
15
|
+
end
|
16
|
+
end
|
17
|
+
```
|
8
18
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def index
|
14
|
-
@title = "All Users"
|
15
|
-
|
16
|
-
paginate_me :users
|
17
|
-
end
|
18
|
-
end</pre>
|
19
|
-
|
20
|
-
<p>index.haml</p>
|
21
|
-
|
22
|
-
<pre>= paginate_for :users do |p|
|
19
|
+
### View code (index.haml)
|
20
|
+
|
21
|
+
```haml
|
22
|
+
= paginate_for :users do |p|
|
23
23
|
= p.link_to_first
|
24
24
|
= p.link_to_next
|
25
25
|
= p.page_out_of_total
|
26
26
|
= p.link_to_previous
|
27
|
-
= p.link_to_last
|
28
|
-
|
27
|
+
= p.link_to_last
|
28
|
+
```
|
29
|
+
### HTML output
|
30
|
+
|
31
|
+
```html
|
29
32
|
<div class="paginate_me users">
|
30
|
-
<a href="/users/page/
|
31
|
-
|
32
|
-
<a href="/users/page/10" class="next" title="next">Next</a>
|
33
|
-
|
33
|
+
<a href="/users/page/1" class="first" title="first">First</a>
|
34
|
+
<a href="/users/page/3" class="next" title="next">Next</a>
|
34
35
|
<span>2 of 10</span>
|
35
|
-
|
36
|
-
<a href="/users/page/10" class="previous" title="previous">Previous</a>
|
37
|
-
|
36
|
+
<a href="/users/page/1" class="previous" title="previous">Previous</a>
|
38
37
|
<a href="/users/page/10" class="last" title="last">Last</a>
|
39
38
|
</div>
|
40
|
-
|
41
|
-
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
*
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
*
|
64
|
-
|
65
|
-
|
39
|
+
```
|
40
|
+
## Options for paginate_me(item, options ={})
|
41
|
+
* `:base_url` – The plugin builds it's base path from the item passed in according to standard rails routing resource format. A different base url can be passed in instead. /users/page/:page_number (/users is the base_url)
|
42
|
+
* `:per_page` – results per page, defaults to 10
|
43
|
+
* `:params_var` – variable set in routes that will hold the current page number ex: ` match "/users/page/:page", :to => "users#index" ` :page is the :params_var , defaults to :page
|
44
|
+
* `:where` - Sets a where clause in the query, accepts a normal active record where object ex: `{comments: {post_id: 2}}`
|
45
|
+
* `:includes` - sets the include clause in the query, uses standard active record syntax
|
46
|
+
* `:order` - changes the order for the query, accepts the same string as an active record query ex: `"created_at DESC"`
|
47
|
+
|
48
|
+
|
49
|
+
## Options for paginate_for(item, options = {}, &block)
|
50
|
+
* `:class` – add classes to div container tag
|
51
|
+
* `:slug` – slug used for url, defaults to 'page', the controller will use the slug in the 'params' variable by default as well.
|
52
|
+
|
53
|
+
|
54
|
+
## Paginate Links
|
55
|
+
* `link_to_first(options={})` – label for first button, goes to page 1
|
56
|
+
* `link_to_next(options={})` – label for next button, increments page by +1
|
57
|
+
* `link_to_previous(options={})` – label for previous button subtracts pages by -1
|
58
|
+
* `link_to_last(options={})` – goes to the last page available, based on total count
|
59
|
+
**options**
|
60
|
+
* `:name` – name of link
|
61
|
+
* `:class` – classes for link pass an array for multiple classes
|
62
|
+
* `:title` – title for link
|
63
|
+
|
64
|
+
## Information Output
|
65
|
+
* `page_out_of_total` – formats pagination info '1 of 10' standard rails 'content_tag' options apply
|
66
|
+
|
67
|
+
## Additional Information
|
66
68
|
* If multiple pagination is needed on one page, for example at the top and bottom of the list, the block of paginate links only needs to be passed to the first 'paginate_for' The additional 'paginate_for' will use the same block, or new blocks can be passed if a different look is required
|
67
|
-
* Make sure you add the correct routes to your routes.rb. For example if your passing in :users and are using a standard resource routing setup, you will need
|
69
|
+
* Make sure you add the correct routes to your routes.rb. For example if your passing in :users and are using a standard resource routing setup, you will need: ` match "/users/page/:page", :to => "users#index" `
|
68
70
|
|
data/lib/paginate_me/paginate.rb
CHANGED
@@ -1,29 +1,40 @@
|
|
1
1
|
module PaginateMe
|
2
2
|
module PMController
|
3
3
|
def paginate_me(item, options = {})
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
4
|
+
# TODO this entire method needs to be cleaned up
|
5
|
+
#set generic param defaults
|
6
|
+
@options = options
|
7
|
+
@options[:params_var] ||= :page
|
8
|
+
@options[:per_page] ||= 10
|
9
|
+
@options[:current_page] = self.params[@options[:params_var]].to_i || 1
|
10
|
+
@options[:order] ||= "#{item.to_s}.created_at ASC"
|
11
|
+
|
12
|
+
current_page = @options[:current_page] <= 0 ? 1 : @options[:current_page]
|
13
|
+
|
14
|
+
model_name = item.to_s
|
15
|
+
model = model_name.singularize.camelize.constantize
|
16
|
+
|
17
|
+
@options[:base_url] ||= method("#{model_name}_path").call
|
18
|
+
|
19
|
+
instance_variable_set("@#{model_name}",
|
20
|
+
model
|
21
|
+
.includes(@options[:includes])
|
22
|
+
.where(@options[:where])
|
23
|
+
.order(@options[:order])
|
24
|
+
.limit(@options[:per_page])
|
25
|
+
.offset((current_page-1) * @options[:per_page]) )
|
26
|
+
|
27
|
+
@options[:page_total] = (model.includes(@options[:includes]).where(@options[:where]).count / @options[:per_page].to_f).ceil
|
28
|
+
|
29
|
+
# set bounds for the current page, this makes sure the current_page variable stays within
|
30
|
+
# the max and min number of items
|
31
|
+
if @options[:current_page] <= 0
|
32
|
+
@options[:current_page] = 1
|
33
|
+
elsif @options[:current_page] > @options[:page_total]
|
34
|
+
@options[:current_page] = @options[:page_total] - 1
|
25
35
|
end
|
26
36
|
end
|
37
|
+
end
|
27
38
|
|
28
39
|
module PMView
|
29
40
|
def paginate_for(item,options = {},&block)
|
@@ -45,8 +56,7 @@ module PaginateMe
|
|
45
56
|
content_tag(:div,content, :class => "paginate_me #{paginate_classes}")
|
46
57
|
end
|
47
58
|
|
48
|
-
|
49
|
-
class PaginateMeBuilder
|
59
|
+
class PaginateMeBuilder
|
50
60
|
include ActionView::Helpers
|
51
61
|
|
52
62
|
def initialize(options)
|
@@ -67,6 +77,12 @@ module PaginateMe
|
|
67
77
|
add_to_template paginate_link_to @next_page, options if @current_page < @page_total
|
68
78
|
end
|
69
79
|
|
80
|
+
def link_to_last(options = {})
|
81
|
+
options[:name] ||= "Last"
|
82
|
+
|
83
|
+
add_to_template paginate_link_to @page_total, options if @current_page < @page_total
|
84
|
+
end
|
85
|
+
|
70
86
|
def link_to_previous(options = {})
|
71
87
|
options[:name] ||= "Previous"
|
72
88
|
|
@@ -76,13 +92,7 @@ module PaginateMe
|
|
76
92
|
def link_to_first(options = {})
|
77
93
|
options[:name] ||= "First"
|
78
94
|
|
79
|
-
add_to_template paginate_link_to 1, options if @current_page
|
80
|
-
end
|
81
|
-
|
82
|
-
def link_to_last(options = {})
|
83
|
-
options[:name] ||= "Last"
|
84
|
-
|
85
|
-
add_to_template paginate_link_to @page_total, options if @current_page > 1
|
95
|
+
add_to_template paginate_link_to 1, options if @current_page > 1
|
86
96
|
end
|
87
97
|
|
88
98
|
def page_out_of_total(options = {})
|
@@ -104,5 +114,6 @@ module PaginateMe
|
|
104
114
|
template.html_safe
|
105
115
|
end
|
106
116
|
end
|
117
|
+
end
|
107
118
|
end
|
108
119
|
|
data/lib/paginate_me/version.rb
CHANGED
metadata
CHANGED
@@ -1,33 +1,24 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: paginate_me
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 4
|
10
|
-
version: 0.0.4
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Adam Rensel
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-09-17 00:00:00 Z
|
12
|
+
date: 2011-10-10 00:00:00.000000000Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
|
-
|
22
|
-
email:
|
14
|
+
description: for more information, please visit the homepage or https://github.com/renz45/paginate_me
|
15
|
+
for a full README
|
16
|
+
email:
|
23
17
|
- adamrensel@gmail.com
|
24
18
|
executables: []
|
25
|
-
|
26
19
|
extensions: []
|
27
|
-
|
28
20
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
21
|
+
files:
|
31
22
|
- .gitignore
|
32
23
|
- Gemfile
|
33
24
|
- README.markdown
|
@@ -39,36 +30,26 @@ files:
|
|
39
30
|
- paginate_me.gemspec
|
40
31
|
homepage: https://github.com/renz45/paginate_me
|
41
32
|
licenses: []
|
42
|
-
|
43
33
|
post_install_message:
|
44
34
|
rdoc_options: []
|
45
|
-
|
46
|
-
require_paths:
|
35
|
+
require_paths:
|
47
36
|
- lib
|
48
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
38
|
none: false
|
50
|
-
requirements:
|
51
|
-
- -
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
44
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
segments:
|
64
|
-
- 0
|
65
|
-
version: "0"
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
66
49
|
requirements: []
|
67
|
-
|
68
50
|
rubyforge_project: paginate_me
|
69
51
|
rubygems_version: 1.8.10
|
70
52
|
signing_key:
|
71
53
|
specification_version: 3
|
72
54
|
summary: paginate_me is a simple pagination gem for Rails 3.1
|
73
55
|
test_files: []
|
74
|
-
|