paginate 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +78 -0
- data/Rakefile +22 -0
- data/lib/paginate.rb +20 -0
- data/lib/paginate/action_controller.rb +1 -0
- data/lib/paginate/active_record.rb +1 -0
- data/lib/paginate/active_record/ext.rb +17 -0
- data/lib/paginate/base.rb +43 -0
- data/lib/paginate/config.rb +15 -0
- data/lib/paginate/helper.rb +70 -0
- data/lib/paginate/locales/en.yml +5 -0
- data/lib/paginate/locales/pt.yml +8 -0
- data/lib/paginate/renderer.rb +73 -0
- data/lib/paginate/version.rb +8 -0
- data/test/paginate/action_view_test.rb +150 -0
- data/test/paginate/active_record_test.rb +28 -0
- data/test/paginate/base_test.rb +84 -0
- data/test/paginate/config_test.rb +23 -0
- data/test/paginate/renderer_test.rb +36 -0
- data/test/resources/controller.rb +3 -0
- data/test/resources/model.rb +2 -0
- data/test/resources/schema.rb +5 -0
- data/test/test_helper.rb +21 -0
- metadata +82 -0
data/README.rdoc
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
= Paginate
|
2
|
+
|
3
|
+
Paginate collections using SIZE+1 to determine if there is a next page. Includes ActiveRecord and ActionView support.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
sudo gem install paginate
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
You can use Paginate with or without ActiveRecord. Let's try a simple array pagination. Imagine that you have something like this in your controller.
|
12
|
+
|
13
|
+
@things = Array.new(11) {|i| "Item #{i}"}
|
14
|
+
|
15
|
+
Then on your view:
|
16
|
+
|
17
|
+
<%= paginate @things %>
|
18
|
+
|
19
|
+
That's it! This is all you have to do! In this case we're using the default page size (which is 10).
|
20
|
+
The url used on page links is taken from the current requested uri.
|
21
|
+
|
22
|
+
You can set default values globally:
|
23
|
+
|
24
|
+
Paginate.setup do |config|
|
25
|
+
config.size = 20
|
26
|
+
config.param_name = :p
|
27
|
+
end
|
28
|
+
|
29
|
+
More examples:
|
30
|
+
|
31
|
+
Post.paginate(1) # page 1 from Post model
|
32
|
+
Post.paginate(:page => 1) # page 1 from Post model
|
33
|
+
Post.paginate(:page => 1, :size => 5) # page 1 from Post model with custom size
|
34
|
+
@user.things.paginate(:page => params[:page]) # paginate association
|
35
|
+
|
36
|
+
<%= paginate @things, :size => 5 %>
|
37
|
+
<%= paginate @things, :url => proc {|page| things_path(:page => page) } %>
|
38
|
+
<%= paginate @things, "/some/path" %>
|
39
|
+
<%= paginate @things, :param_name => :p %>
|
40
|
+
|
41
|
+
To iterate the collection, you must use a helper called <tt>iterate</tt>. This is required cause we're always considering SIZE + 1, so if you use the regular +each+ you end up rendering one additional item.
|
42
|
+
|
43
|
+
<% iterate @things do |thing| %>
|
44
|
+
<% end %>
|
45
|
+
|
46
|
+
If you want the iteration index, you can expect it as a second block parameter.
|
47
|
+
|
48
|
+
<% iterate @things do |thing, i| %>
|
49
|
+
<% end %>
|
50
|
+
|
51
|
+
And if you're using some custom page size, you have to specify it as well.
|
52
|
+
|
53
|
+
<% iterate @things, :size => 5 do |thing| %>
|
54
|
+
<% end %>
|
55
|
+
|
56
|
+
== I18n support
|
57
|
+
|
58
|
+
If you want to translate links, you have implement the following scopes:
|
59
|
+
|
60
|
+
en:
|
61
|
+
paginate:
|
62
|
+
next: "Older"
|
63
|
+
previous: "Newer"
|
64
|
+
page: "Page {{page}}"
|
65
|
+
|
66
|
+
== License
|
67
|
+
|
68
|
+
(The MIT License)
|
69
|
+
|
70
|
+
Copyright © 2010:
|
71
|
+
|
72
|
+
* Nando Vieira - http://simplesideias.com.br
|
73
|
+
|
74
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
75
|
+
|
76
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
77
|
+
|
78
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rake/testtask"
|
2
|
+
require "jeweler"
|
3
|
+
require "lib/paginate/version"
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs += %w[ test lib ]
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
+
t.verbose = true
|
9
|
+
t.ruby_opts = %w[ -rubygems ]
|
10
|
+
end
|
11
|
+
|
12
|
+
Jeweler::Tasks.new do |gem|
|
13
|
+
gem.name = "paginate"
|
14
|
+
gem.version = Paginate::Version::STRING
|
15
|
+
gem.summary = "Paginate collections using SIZE+1 to determine if there is a next page. Includes ActiveRecord and ActionView support."
|
16
|
+
gem.email = "fnando.vieira@gmail.com"
|
17
|
+
gem.homepage = "http://github.com/fnando/paginate"
|
18
|
+
gem.authors = ["Nando Vieira"]
|
19
|
+
gem.files = FileList["lib/**/*", "Rakefile", "README.rdoc"]
|
20
|
+
end
|
21
|
+
|
22
|
+
Jeweler::GemcutterTasks.new
|
data/lib/paginate.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "i18n"
|
2
|
+
require "paginate/base"
|
3
|
+
require "paginate/config"
|
4
|
+
require "paginate/helper"
|
5
|
+
require "paginate/renderer"
|
6
|
+
require "paginate/active_record"
|
7
|
+
require "paginate/action_controller"
|
8
|
+
|
9
|
+
I18n.load_path += Dir[File.dirname(__FILE__) + "/paginate/locales/**/*.yml"]
|
10
|
+
|
11
|
+
module Paginate
|
12
|
+
def self.setup(&block)
|
13
|
+
yield Config
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Paginate.setup do |config|
|
18
|
+
config.param_name = :page
|
19
|
+
config.size = 10
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
ActionController::Base.helper Paginate::Helper if defined?(ActionController::Base)
|
@@ -0,0 +1 @@
|
|
1
|
+
require "paginate/active_record/ext" if defined?(ActiveRecord)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ActiveRecord::Base
|
2
|
+
class << self
|
3
|
+
alias_method :original_inherited, :inherited
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.inherited(child)
|
7
|
+
original_inherited(child)
|
8
|
+
|
9
|
+
child.class_eval do
|
10
|
+
if Rails.version >= "3.0"
|
11
|
+
scope :paginate, proc {|*args| Paginate::Base.new(*args).to_options }
|
12
|
+
else
|
13
|
+
named_scope :paginate, proc {|*args| Paginate::Base.new(*args).to_options }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Paginate
|
2
|
+
class Base
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
if options.kind_of?(Hash)
|
7
|
+
@options = options
|
8
|
+
else
|
9
|
+
@options = {:page => options.to_i}
|
10
|
+
end
|
11
|
+
|
12
|
+
@options.reverse_merge!(Paginate::Config.to_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
def collection_size
|
16
|
+
@collection_size ||= options[:collection].size
|
17
|
+
end
|
18
|
+
|
19
|
+
def next_page?
|
20
|
+
collection_size > options[:size]
|
21
|
+
end
|
22
|
+
|
23
|
+
def previous_page?
|
24
|
+
options[:page] > 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def page
|
28
|
+
[1, options.fetch(:page, 1).to_i].max
|
29
|
+
end
|
30
|
+
|
31
|
+
def offset
|
32
|
+
(page - 1) * limit
|
33
|
+
end
|
34
|
+
|
35
|
+
def limit
|
36
|
+
[options[:size], 10].compact.first.to_i + 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_options
|
40
|
+
{ :limit => limit, :offset => offset }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Paginate
|
2
|
+
module Helper
|
3
|
+
# Display pagination based on the collection and current page.
|
4
|
+
#
|
5
|
+
# <%= paginate @posts %>
|
6
|
+
# <%= paginate @posts, options %>
|
7
|
+
# <%= paginate @posts, posts_path, options %>
|
8
|
+
#
|
9
|
+
# The available options are:
|
10
|
+
#
|
11
|
+
# * <tt>:url</tt>: the URL which page numbers will be appended to. Can be proc or string.
|
12
|
+
# * <tt>:id</tt>: the HTML id that will identify the pagination block.
|
13
|
+
# * <tt>:size</tt>: the page size. When not specified will default to <tt>Paginate::Config.size</tt>.
|
14
|
+
# * <tt>:param_name</tt>: the page param name. When not specified will default to <tt>Paginate::Config.param_name</tt>.
|
15
|
+
#
|
16
|
+
# <%= paginate @posts, proc {|page| posts_path(page) }
|
17
|
+
# <%= paginate @posts, :url => proc {|page| posts_path(page) }
|
18
|
+
#
|
19
|
+
# You don't have to specify the URL; the current requested URI will be used if you don't provide it.
|
20
|
+
#
|
21
|
+
def paginate(collection, *args)
|
22
|
+
options = args.extract_options!
|
23
|
+
param_name = [options[:param_name], Paginate::Config.param_name, :page].compact.first
|
24
|
+
options.merge!({
|
25
|
+
:collection => collection,
|
26
|
+
:page => params[param_name],
|
27
|
+
:param_name => param_name,
|
28
|
+
:fullpath => request.respond_to?(:fullpath) ? request.fullpath : request.request_uri
|
29
|
+
})
|
30
|
+
options.merge!(:url => args.first) if args.any?
|
31
|
+
|
32
|
+
Paginate::Renderer.new(options).render
|
33
|
+
end
|
34
|
+
|
35
|
+
# In order to iterate the correct items you have to skip the latest collection's item.
|
36
|
+
# We added this helper to automatically skip the latest item only if there's a next page,
|
37
|
+
# which means that we have an extra item.
|
38
|
+
#
|
39
|
+
# <% iterate @items do |item| %>
|
40
|
+
# <% end %>
|
41
|
+
#
|
42
|
+
# If you want to grab the iteration index as well just expect it as a block parameter.
|
43
|
+
#
|
44
|
+
# <% iterate @items do |item, i|
|
45
|
+
# <% end %>
|
46
|
+
#
|
47
|
+
#
|
48
|
+
# If you set a custom size while fetching items from database, you need to inform it while iterating.
|
49
|
+
#
|
50
|
+
# @items = Item.paginate(:page => 1, :size => 5)
|
51
|
+
#
|
52
|
+
# Then in your view:
|
53
|
+
#
|
54
|
+
# <% iterate @items, :size => 5 do |item| %>
|
55
|
+
# <% end %>
|
56
|
+
#
|
57
|
+
def iterate(collection, options = {}, &block)
|
58
|
+
options.reverse_merge!(:size => Paginate::Config.size)
|
59
|
+
block_arity = block.arity
|
60
|
+
|
61
|
+
collection[0, options[:size]].each_with_index do |item, i|
|
62
|
+
if block_arity == 1
|
63
|
+
yield item
|
64
|
+
else
|
65
|
+
yield item, i
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Paginate
|
2
|
+
class Renderer
|
3
|
+
attr_accessor :options
|
4
|
+
attr_accessor :current_page
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@current_page = [options[:page].to_i, 1].max
|
8
|
+
options.reverse_merge!(Paginate::Config.to_hash)
|
9
|
+
@options = options.merge(:page => current_page)
|
10
|
+
end
|
11
|
+
|
12
|
+
def processor
|
13
|
+
@processor ||= Paginate::Base.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def url_for(page)
|
17
|
+
url = options[:url] || options[:fullpath]
|
18
|
+
|
19
|
+
if url.kind_of?(Proc)
|
20
|
+
url[page]
|
21
|
+
else
|
22
|
+
re = Regexp.new("([&?])#{Regexp.escape(options[:param_name].to_s)}=[0-9]+")
|
23
|
+
url.gsub!(re, "\\1")
|
24
|
+
url.gsub!(/\?$/, "")
|
25
|
+
url.gsub!(/\?&/, "?")
|
26
|
+
url = URI.parse(url).to_s
|
27
|
+
|
28
|
+
connector = (url =~ /\?/) ? "&" : "?"
|
29
|
+
|
30
|
+
url + connector + page.to_s.to_query(options[:param_name])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def render
|
35
|
+
html = String.new
|
36
|
+
previous_label = I18n.t("paginate.previous")
|
37
|
+
previous_url = url_for(options[:page] - 1)
|
38
|
+
next_label = I18n.t("paginate.next")
|
39
|
+
next_url = url_for(options[:page] + 1)
|
40
|
+
page_label = I18n.t("paginate.page", options)
|
41
|
+
|
42
|
+
css = %w[ paginate ]
|
43
|
+
css << "disabled" unless processor.previous_page? || processor.next_page?
|
44
|
+
|
45
|
+
html << %[<ul class="#{css.join(" ")}">]
|
46
|
+
|
47
|
+
# Previous page
|
48
|
+
if processor.previous_page?
|
49
|
+
html << %[<li><a class="previous-page" href="#{previous_url}" title="#{previous_label}">#{previous_label}</a></li>]
|
50
|
+
else
|
51
|
+
html << %[<li><span class="previous-page disabled" title="#{previous_label}">#{previous_label}</span></li>]
|
52
|
+
end
|
53
|
+
|
54
|
+
# Current page
|
55
|
+
html << %[<li><span class="page">#{page_label}</span></li>]
|
56
|
+
|
57
|
+
# Next page
|
58
|
+
if processor.next_page?
|
59
|
+
html << %[<li><a class="next-page" href="#{next_url}" title="#{next_label}">#{next_label}</a></li>]
|
60
|
+
else
|
61
|
+
html << %[<li><span class="next-page disabled" title="#{next_label}">#{next_label}</span></li>]
|
62
|
+
end
|
63
|
+
|
64
|
+
html << %[</ul>]
|
65
|
+
|
66
|
+
if html.respond_to?(:html_safe)
|
67
|
+
html.html_safe
|
68
|
+
else
|
69
|
+
html
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ActionViewTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@request = OpenStruct.new
|
6
|
+
@params = Hash.new
|
7
|
+
|
8
|
+
@controller = ThingsController.new
|
9
|
+
@controller.params = @params
|
10
|
+
|
11
|
+
@view = ActionView::Base.new
|
12
|
+
@view.controller = @controller
|
13
|
+
@view.extend(Paginate::Helper)
|
14
|
+
@view.stubs(:request).returns(@request)
|
15
|
+
|
16
|
+
@helper = Object.new
|
17
|
+
@helper.extend(Paginate::Helper)
|
18
|
+
|
19
|
+
Paginate.setup do |config|
|
20
|
+
config.param_name = :page
|
21
|
+
config.size = 10
|
22
|
+
end
|
23
|
+
|
24
|
+
I18n.locale = :en
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_display_pagination_list
|
28
|
+
@request.fullpath = "/some/path?page=1"
|
29
|
+
html = render(:default, [])
|
30
|
+
|
31
|
+
assert_equal 1, html.css("ul.paginate").count
|
32
|
+
assert_equal 3, html.css("ul.paginate > li").count
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_disabled
|
36
|
+
@request.fullpath = "/some/path"
|
37
|
+
html = render(:default, [])
|
38
|
+
|
39
|
+
assert_not_nil html.css("ul.paginate.disabled").first
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_display_next_page_link
|
43
|
+
@request.fullpath = "/some/path?page=1"
|
44
|
+
html = render(:default, Array.new(11))
|
45
|
+
link = html.css("li > a.next-page").first
|
46
|
+
|
47
|
+
assert_not_nil link
|
48
|
+
assert_equal "/some/path?page=2", link["href"]
|
49
|
+
assert_equal "Next page", link.text
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_display_next_page_link_from_block
|
53
|
+
@request.fullpath = "/some/path?page=1"
|
54
|
+
html = render(:block_as_url, Array.new(11))
|
55
|
+
link = html.css("li > a.next-page").first
|
56
|
+
|
57
|
+
assert_not_nil link
|
58
|
+
assert_equal "/some/path/2", link["href"]
|
59
|
+
assert_equal "Next page", link.text
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_display_previous_page_link
|
63
|
+
@params[:page] = 2
|
64
|
+
@request.fullpath = "/some/path?page=2"
|
65
|
+
html = render(:default, Array.new(11))
|
66
|
+
link = html.css("li > a.previous-page").first
|
67
|
+
|
68
|
+
assert_not_nil link
|
69
|
+
assert_equal "/some/path?page=1", link["href"]
|
70
|
+
assert_equal "Previous page", link.text
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_display_next_page_as_disabled
|
74
|
+
@request.fullpath = "/some/path?page=1"
|
75
|
+
html = render(:default, Array.new(10))
|
76
|
+
link = html.css("li > a.next-page").first
|
77
|
+
span = html.css("li > span.next-page.disabled").first
|
78
|
+
|
79
|
+
assert_nil link
|
80
|
+
assert_not_nil span
|
81
|
+
assert_equal "Next page", span.text
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_display_previous_page_as_disabled
|
85
|
+
@request.fullpath = "/some/path?page=1"
|
86
|
+
html = render(:default, Array.new(10))
|
87
|
+
link = html.css("li > a.previous-page").first
|
88
|
+
span = html.css("li > span.previous-page.disabled").first
|
89
|
+
|
90
|
+
assert_nil link
|
91
|
+
assert_not_nil span
|
92
|
+
assert_equal "Previous page", span.text
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_display_current_page
|
96
|
+
@params[:page] = 10
|
97
|
+
@request.fullpath = "/some/path?page=10"
|
98
|
+
html = render(:default, [])
|
99
|
+
span = html.css("li > span.page").first
|
100
|
+
|
101
|
+
assert_not_nil span
|
102
|
+
assert_equal "Page 10", span.text
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_translate_strings
|
106
|
+
I18n.locale = :pt
|
107
|
+
|
108
|
+
@params[:page] = 10
|
109
|
+
@request.fullpath = "/some/path?page=10"
|
110
|
+
html = render(:default, Array.new(11))
|
111
|
+
|
112
|
+
assert_equal "Página 10", html.css("span.page").text
|
113
|
+
assert_equal "Próxima página", html.css("a.next-page").text
|
114
|
+
assert_equal "Página anterior", html.css("a.previous-page").text
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_iterate
|
118
|
+
values = []
|
119
|
+
items = Array.new(11) {|i| "User#{i}" }
|
120
|
+
|
121
|
+
@helper.iterate items do |item|
|
122
|
+
values << item
|
123
|
+
end
|
124
|
+
|
125
|
+
assert_equal items[0, 10], values
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_iterate_with_index
|
129
|
+
values = []
|
130
|
+
indices = []
|
131
|
+
items = Array.new(11) {|i| "User#{i}" }
|
132
|
+
|
133
|
+
@helper.iterate items do |item, i|
|
134
|
+
values << item
|
135
|
+
indices << i
|
136
|
+
end
|
137
|
+
|
138
|
+
assert_equal items[0, 10], values
|
139
|
+
assert_equal (0...10).to_a, indices
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
143
|
+
def render(view_name, items)
|
144
|
+
Nokogiri @view.render(:inline => load_view(view_name), :locals => {:items => items})
|
145
|
+
end
|
146
|
+
|
147
|
+
def load_view(name)
|
148
|
+
File.read(File.dirname(__FILE__) + "/../resources/views/#{name}.erb")
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
15.times {|i| Thing.create(:name => "Thing #{i}") }
|
6
|
+
Paginate::Config.size = 10
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_respond_to_paginate_method
|
10
|
+
assert_respond_to Thing, :paginate
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_paginate_with_defaults
|
14
|
+
things = Thing.all(:limit => 11)
|
15
|
+
assert_equal things, Thing.paginate.all
|
16
|
+
|
17
|
+
things = Thing.all(:limit => 11, :offset => 11)
|
18
|
+
assert_equal things, Thing.paginate(:page => 2).all
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_paginate_with_options
|
22
|
+
things = Thing.all(:limit => 6)
|
23
|
+
assert_equal things, Thing.paginate(:size => 5)
|
24
|
+
|
25
|
+
things = Thing.all(:limit => 6, :offset => 6)
|
26
|
+
assert_equal things, Thing.paginate(:size => 5, :page => 2)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class BaseTest < Test::Unit::TestCase
|
4
|
+
def test_page_from_integer
|
5
|
+
@base = Paginate::Base.new(12)
|
6
|
+
assert_equal 12, @base.page
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_page_from_string
|
10
|
+
@base = Paginate::Base.new("12")
|
11
|
+
assert_equal 12, @base.page
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_default_page_value
|
15
|
+
@base = Paginate::Base.new
|
16
|
+
assert_equal 1, @base.page
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_page
|
20
|
+
@base = Paginate::Base.new(:page => 0)
|
21
|
+
assert_equal 1, @base.page
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_limit_from_config
|
25
|
+
Paginate::Config.size = 25
|
26
|
+
@base = Paginate::Base.new
|
27
|
+
|
28
|
+
assert_equal 26, @base.limit
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_limit_from_options
|
32
|
+
Paginate::Config.size = 25
|
33
|
+
@base = Paginate::Base.new(:size => 13)
|
34
|
+
|
35
|
+
assert_equal 14, @base.limit
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_default_limit
|
39
|
+
Paginate::Config.size = nil
|
40
|
+
@base = Paginate::Base.new
|
41
|
+
|
42
|
+
assert_equal 11, @base.limit
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_offset_from_config
|
46
|
+
Paginate::Config.size = 15
|
47
|
+
@base = Paginate::Base.new(:page => 2)
|
48
|
+
|
49
|
+
assert_equal 16, @base.offset
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_offset_from_options
|
53
|
+
@base = Paginate::Base.new(:page => 2, :size => 5)
|
54
|
+
|
55
|
+
assert_equal 6, @base.offset
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_return_finder_options
|
59
|
+
@base = Paginate::Base.new(:page => 3, :size => 5)
|
60
|
+
|
61
|
+
options = {:limit => 6, :offset => 12}
|
62
|
+
assert_equal options, @base.to_options
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_next_page
|
66
|
+
@base = Paginate::Base.new(:page => 1, :size => 5, :collection => Array.new(6))
|
67
|
+
assert @base.next_page?
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_no_next_page
|
71
|
+
@base = Paginate::Base.new(:page => 1, :size => 5, :collection => Array.new(5))
|
72
|
+
assert !@base.next_page?
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_previous_page
|
76
|
+
@base = Paginate::Base.new(:page => 2)
|
77
|
+
assert @base.previous_page?
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_no_previous_page
|
81
|
+
@base = Paginate::Base.new(:page => 1)
|
82
|
+
assert !@base.previous_page?
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ConfigTest < Test::Unit::TestCase
|
4
|
+
def test_yield_configuration_class
|
5
|
+
Paginate.setup do |config|
|
6
|
+
config.param_name = :p
|
7
|
+
config.size = 50
|
8
|
+
end
|
9
|
+
|
10
|
+
assert_equal :p, Paginate::Config.param_name
|
11
|
+
assert_equal 50, Paginate::Config.size
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_return_config_as_hash
|
15
|
+
Paginate.setup do |config|
|
16
|
+
config.param_name = :p
|
17
|
+
config.size = 25
|
18
|
+
end
|
19
|
+
|
20
|
+
options = {:param_name => :p, :size => 25}
|
21
|
+
assert_equal options, Paginate::Config.to_hash
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class RendererTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Paginate.setup do |config|
|
6
|
+
config.param_name = :page
|
7
|
+
config.size = 10
|
8
|
+
end
|
9
|
+
|
10
|
+
I18n.locale = :en
|
11
|
+
|
12
|
+
@renderer = Paginate::Renderer.new({
|
13
|
+
:collection => Array.new(11),
|
14
|
+
:page => 1,
|
15
|
+
:fullpath => "/some/path"
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_safe_html
|
20
|
+
string = String.new
|
21
|
+
string.expects(:respond_to?).with(:html_safe).returns(true)
|
22
|
+
string.expects(:html_safe)
|
23
|
+
String.expects(:new).returns(string)
|
24
|
+
|
25
|
+
@renderer.render
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_no_safe_html
|
29
|
+
string = String.new
|
30
|
+
string.expects(:respond_to?).with(:html_safe).returns(false)
|
31
|
+
string.expects(:html_safe).never
|
32
|
+
String.expects(:new).returns(string)
|
33
|
+
|
34
|
+
@renderer.render
|
35
|
+
end
|
36
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
gem "test-unit"
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
require "mocha"
|
5
|
+
require "nokogiri"
|
6
|
+
require "ostruct"
|
7
|
+
require "active_support/all"
|
8
|
+
require "active_record"
|
9
|
+
require "action_view"
|
10
|
+
require "action_controller"
|
11
|
+
require "sqlite3"
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
14
|
+
|
15
|
+
Rails = OpenStruct.new(:version => ActiveRecord::VERSION::STRING)
|
16
|
+
|
17
|
+
require "paginate"
|
18
|
+
|
19
|
+
load "resources/schema.rb"
|
20
|
+
require "resources/model"
|
21
|
+
require "resources/controller"
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paginate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Nando Vieira
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-04 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: fnando.vieira@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- README.rdoc
|
31
|
+
- Rakefile
|
32
|
+
- lib/paginate.rb
|
33
|
+
- lib/paginate/action_controller.rb
|
34
|
+
- lib/paginate/active_record.rb
|
35
|
+
- lib/paginate/active_record/ext.rb
|
36
|
+
- lib/paginate/base.rb
|
37
|
+
- lib/paginate/config.rb
|
38
|
+
- lib/paginate/helper.rb
|
39
|
+
- lib/paginate/locales/en.yml
|
40
|
+
- lib/paginate/locales/pt.yml
|
41
|
+
- lib/paginate/renderer.rb
|
42
|
+
- lib/paginate/version.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/fnando/paginate
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.6
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Paginate collections using SIZE+1 to determine if there is a next page. Includes ActiveRecord and ActionView support.
|
73
|
+
test_files:
|
74
|
+
- test/paginate/action_view_test.rb
|
75
|
+
- test/paginate/active_record_test.rb
|
76
|
+
- test/paginate/base_test.rb
|
77
|
+
- test/paginate/config_test.rb
|
78
|
+
- test/paginate/renderer_test.rb
|
79
|
+
- test/resources/controller.rb
|
80
|
+
- test/resources/model.rb
|
81
|
+
- test/resources/schema.rb
|
82
|
+
- test/test_helper.rb
|