-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
143 lines (84 loc) · 4.09 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
SimplyPaginate
==============
SimplyPaginate is a Rails plugin for pagination. Loosely based on Mislav Marohnić
and PJ Hyett's work on will_paginate (http://github.com/mislav/will_paginate).
It is a very trimmed down version of will_paginate plus a few additions to suit my needs.
Requirements
============
Rails version 2.3.x
Gem dependencies:
redgreen (for testing) [OPTIONAL]
sqlite3-ruby (for testing) [OPTIONAL]
Installation
============
git submodule add git://github.com/pjg/simply_paginate.git vendor/plugins/simply_paginate
git commit -m "Add simply_paginate plugin as a submodule"
Example usage
=============
config/routes.rb
map.comments_pages '/comments/page/:page', :controller => 'comments', :page => /[\d]{1,}/
app/controllers/comments_controller.rb
def index
@comments = Comment.paginate(:per_page => 50,
:page => params[:page],
:include => [:document],
:conditions => {:is_hidden => false},
:order => 'comments.id DESC')
end
app/views/comments/index.html.erb
<%= pagination_for @comments %>
Will render pagination links Flickr style:
[« Previous page][1][2][3]...[154][Next page »]
To display the pagination summary:
app/views/comments/index.html.erb
<p>Total: <%= @comments.total_entries %> comments. Showing: <%= @comments.current_results_first %> - <%= @comments.current_results_last %>.</p>
To customize the 'previous page' and 'next page' labels:
config/initializers/simply_paginate.rb
SimplyPaginate::ViewHelpers.pagination_options[:previous_label] = 'Previous'
SimplyPaginate::ViewHelpers.pagination_options[:next_label] = 'Next'
Integration with the act_as_ferret full text search:
app/controllers/search_controller.rb
def index
@query = params[:query]
page = params[:page].blank? ? 1 : params[:page]
@results = SimplyPaginate::Collection.create(page, 10) do |pager|
pager.total_entries, results = Document.full_text_search(@query, :page => page)
pager.replace results
end
end
It does work correctly with named scopes, for example the following will yield the expected results:
Comment.visible.ordered.paginate(:page => 2, :per_page => 5)
It also works with Arrays:
[1, 2, 3, 4 ,5].paginate(:page => 2, :per_page => 2)
=> [3, 4]
CAVEATS
=======
Unfortunately, there are some. Comment.all returns an array (instead of ActiveRecord::NamedScope::Scope), so doing one of the following:
Comment.all.paginate(:per_page => 2)
Comment.find(:all).paginate(:per_page => 2)
>> Comment Load (26.5ms) SELECT * FROM `comments`
is highly inefficient as it will first fetch all records from the database and only then paginate the returned Array. Use this instead:
Comment.paginate(:per_page => 2)
>> SQL (0.1ms) SELECT count(*) AS count_all FROM `comments`
>> Comment Load (0.3ms) SELECT * FROM `comments` LIMIT 0, 2
I don't recommend using it with named scopes which use the :include parameter. For example, given:
model Comment
named_scope :with_document, :include => :document
end
Don't do the following (as it will generate an ugly and very resource hungry count query):
Comment.with_document.paginate(:page => params[:page], :per_page => 50)
>> SQL (85.7ms) SELECT count(DISTINCT `comments`.id) AS count_all FROM `comments` LEFT OUTER JOIN `documents` ON `documents`.id = `comments`.document_id
>> Comment Load (0.1ms) SELECT * FROM `comments` LIMIT 0, 50
...
Instead do it old school:
Comment.paginate(:include => :document, :page => params[:page], :per_page => 50)
>> SQL (0.2ms) SELECT count(*) AS count_all FROM `comments`
>> Comment Load (0.1ms) SELECT * FROM `comments` LIMIT 0, 50
...
The difference in performance is striking (in part it is due to the fact, that MySQL keeps the table count at hand).
TESTING
=======
If you'd like to run tests, you must have the sqlite3 installed (both application and gem):
aptitude install sqlite3 libsqlite3-dev
gem install sqlite3-ruby
Copyright (c) 2008-2010 Paweł Gościcki, released under the MIT license