will_paginate 3.0.0 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of will_paginate might be problematic. Click here for more details.
- data/README.md +1 -1
- data/lib/will_paginate/active_record.rb +22 -9
- data/lib/will_paginate/collection.rb +36 -21
- data/lib/will_paginate/data_mapper.rb +2 -4
- data/lib/will_paginate/deprecation.rb +55 -0
- data/lib/will_paginate/railtie.rb +32 -1
- data/lib/will_paginate/sequel.rb +3 -0
- data/lib/will_paginate/version.rb +1 -1
- data/lib/will_paginate/view_helpers.rb +9 -4
- data/lib/will_paginate/view_helpers/action_view.rb +16 -16
- data/lib/will_paginate/view_helpers/link_renderer.rb +1 -1
- data/spec/collection_spec.rb +14 -6
- data/spec/finders/active_record_spec.rb +24 -7
- data/spec/finders/data_mapper_spec.rb +4 -0
- data/spec/spec_helper.rb +38 -18
- data/spec/view_helpers/action_view_spec.rb +39 -0
- data/spec/view_helpers/base_spec.rb +14 -0
- data/spec/view_helpers/link_renderer_base_spec.rb +9 -1
- metadata +4 -3
data/README.md
CHANGED
@@ -18,6 +18,8 @@ module WillPaginate
|
|
18
18
|
module ActiveRecord
|
19
19
|
# makes a Relation look like WillPaginate::Collection
|
20
20
|
module RelationMethods
|
21
|
+
include WillPaginate::CollectionMethods
|
22
|
+
|
21
23
|
attr_accessor :current_page
|
22
24
|
attr_writer :total_entries, :wp_count_options
|
23
25
|
|
@@ -81,22 +83,24 @@ module WillPaginate
|
|
81
83
|
# overloaded to be pagination-aware
|
82
84
|
def empty?
|
83
85
|
if !loaded? and offset_value
|
84
|
-
|
86
|
+
rel_count = count
|
87
|
+
if rel_count.respond_to?(:size) and !rel_count.is_a?(Integer)
|
88
|
+
rel_count.size <= offset_value
|
89
|
+
else
|
90
|
+
rel_count <= offset_value
|
91
|
+
end
|
85
92
|
else
|
86
93
|
super
|
87
94
|
end
|
88
95
|
end
|
89
96
|
|
90
|
-
def
|
91
|
-
|
97
|
+
def clone
|
98
|
+
copy_will_paginate_data super
|
92
99
|
end
|
93
100
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
other.total_entries = nil if defined? @total_entries_queried
|
98
|
-
other.wp_count_options = @wp_count_options if defined? @wp_count_options
|
99
|
-
other
|
101
|
+
# workaround for Active Record 3.0
|
102
|
+
def scoped(options = nil)
|
103
|
+
copy_will_paginate_data super
|
100
104
|
end
|
101
105
|
|
102
106
|
def to_a
|
@@ -108,6 +112,15 @@ module WillPaginate
|
|
108
112
|
end
|
109
113
|
end
|
110
114
|
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def copy_will_paginate_data(other)
|
119
|
+
other.current_page = current_page unless other.current_page
|
120
|
+
other.total_entries = nil if defined? @total_entries_queried
|
121
|
+
other.wp_count_options = @wp_count_options if defined? @wp_count_options
|
122
|
+
other
|
123
|
+
end
|
111
124
|
end
|
112
125
|
|
113
126
|
module Pagination
|
@@ -2,6 +2,38 @@ require 'will_paginate/per_page'
|
|
2
2
|
require 'will_paginate/page_number'
|
3
3
|
|
4
4
|
module WillPaginate
|
5
|
+
# Any will_paginate-compatible collection should have these methods:
|
6
|
+
#
|
7
|
+
# current_page, per_page, offset, total_entries, total_pages
|
8
|
+
#
|
9
|
+
# It can also define some of these optional methods:
|
10
|
+
#
|
11
|
+
# out_of_bounds?, previous_page, next_page
|
12
|
+
#
|
13
|
+
# This module provides few of these methods.
|
14
|
+
module CollectionMethods
|
15
|
+
def total_pages
|
16
|
+
total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
|
17
|
+
end
|
18
|
+
|
19
|
+
# current_page - 1 or nil if there is no previous page
|
20
|
+
def previous_page
|
21
|
+
current_page > 1 ? (current_page - 1) : nil
|
22
|
+
end
|
23
|
+
|
24
|
+
# current_page + 1 or nil if there is no next page
|
25
|
+
def next_page
|
26
|
+
current_page < total_pages ? (current_page + 1) : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
# Helper method that is true when someone tries to fetch a page with a
|
30
|
+
# larger number than the last page. Can be used in combination with flashes
|
31
|
+
# and redirecting.
|
32
|
+
def out_of_bounds?
|
33
|
+
current_page > total_pages
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
5
37
|
# = The key to pagination
|
6
38
|
# Arrays returned from paginating finds are, in fact, instances of this little
|
7
39
|
# class. You may think of WillPaginate::Collection as an ordinary array with
|
@@ -18,7 +50,9 @@ module WillPaginate
|
|
18
50
|
# require 'will_paginate/collection'
|
19
51
|
# # WillPaginate::Collection is now available for use
|
20
52
|
class Collection < Array
|
21
|
-
|
53
|
+
include CollectionMethods
|
54
|
+
|
55
|
+
attr_reader :current_page, :per_page, :total_entries
|
22
56
|
|
23
57
|
# Arguments to the constructor are the current page number, per-page limit
|
24
58
|
# and the total number of entries. The last argument is optional because it
|
@@ -63,35 +97,16 @@ module WillPaginate
|
|
63
97
|
pager
|
64
98
|
end
|
65
99
|
|
66
|
-
# Helper method that is true when someone tries to fetch a page with a
|
67
|
-
# larger number than the last page. Can be used in combination with flashes
|
68
|
-
# and redirecting.
|
69
|
-
def out_of_bounds?
|
70
|
-
current_page > total_pages
|
71
|
-
end
|
72
|
-
|
73
100
|
# Current offset of the paginated collection. If we're on the first page,
|
74
101
|
# it is always 0. If we're on the 2nd page and there are 30 entries per page,
|
75
102
|
# the offset is 30. This property is useful if you want to render ordinals
|
76
103
|
# side by side with records in the view: simply start with offset + 1.
|
77
104
|
def offset
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
# current_page - 1 or nil if there is no previous page
|
82
|
-
def previous_page
|
83
|
-
current_page > 1 ? (current_page - 1) : nil
|
105
|
+
current_page.to_offset(per_page).to_i
|
84
106
|
end
|
85
107
|
|
86
|
-
# current_page + 1 or nil if there is no next page
|
87
|
-
def next_page
|
88
|
-
current_page < total_pages ? (current_page + 1) : nil
|
89
|
-
end
|
90
|
-
|
91
|
-
# sets the <tt>total_entries</tt> property and calculates <tt>total_pages</tt>
|
92
108
|
def total_entries=(number)
|
93
109
|
@total_entries = number.to_i
|
94
|
-
@total_pages = (@total_entries / per_page.to_f).ceil
|
95
110
|
end
|
96
111
|
|
97
112
|
# This is a magic wrapper for the original Array#replace method. It serves
|
@@ -30,6 +30,8 @@ module WillPaginate
|
|
30
30
|
end
|
31
31
|
|
32
32
|
module CollectionMethods
|
33
|
+
include WillPaginate::CollectionMethods
|
34
|
+
|
33
35
|
attr_accessor :current_page
|
34
36
|
|
35
37
|
def paginated?
|
@@ -58,10 +60,6 @@ module WillPaginate
|
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
def total_pages
|
62
|
-
(total_entries / per_page.to_f).ceil
|
63
|
-
end
|
64
|
-
|
65
63
|
def to_a
|
66
64
|
::WillPaginate::Collection.create(current_page, per_page) do |col|
|
67
65
|
col.replace super
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module WillPaginate::Deprecation
|
2
|
+
class << self
|
3
|
+
def warn(message, stack = caller)
|
4
|
+
offending_line = origin_of_call(stack)
|
5
|
+
full_message = "DEPRECATION WARNING: #{message} (called from #{offending_line})"
|
6
|
+
logger = rails_logger || Kernel
|
7
|
+
logger.warn full_message
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def rails_logger
|
13
|
+
defined?(Rails) && Rails.logger
|
14
|
+
end
|
15
|
+
|
16
|
+
def origin_of_call(stack)
|
17
|
+
lib_root = File.expand_path('../../..', __FILE__)
|
18
|
+
stack.find { |line| line.index(lib_root) != 0 } || stack.first
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Hash < ::Hash
|
23
|
+
def initialize(values = {})
|
24
|
+
super()
|
25
|
+
update values
|
26
|
+
@deprecated = {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def []=(key, value)
|
30
|
+
check_deprecated(key, value)
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def deprecate_key(*keys)
|
35
|
+
message = block_given? ? Proc.new : keys.pop
|
36
|
+
Array(keys).each { |key| @deprecated[key] = message }
|
37
|
+
end
|
38
|
+
|
39
|
+
def merge(another)
|
40
|
+
to_hash.update(another)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_hash
|
44
|
+
::Hash.new.update(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def check_deprecated(key, value)
|
50
|
+
if msg = @deprecated[key] and (!msg.respond_to?(:call) or (msg = msg.call(key, value)))
|
51
|
+
WillPaginate::Deprecation.warn(msg)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'will_paginate'
|
2
|
+
require 'will_paginate/page_number'
|
2
3
|
require 'will_paginate/collection'
|
3
4
|
require 'will_paginate/i18n'
|
4
5
|
|
@@ -18,14 +19,44 @@ module WillPaginate
|
|
18
19
|
end
|
19
20
|
|
20
21
|
self.class.add_locale_path config
|
22
|
+
|
23
|
+
# early access to ViewHelpers.pagination_options
|
24
|
+
require 'will_paginate/view_helpers'
|
21
25
|
end
|
22
26
|
|
23
27
|
def self.setup_actioncontroller
|
24
|
-
ActionDispatch::ShowExceptions.
|
28
|
+
ActionDispatch::ShowExceptions.send :include, ShowExceptionsPatch
|
29
|
+
ActionController::Base.extend ControllerRescuePatch
|
25
30
|
end
|
26
31
|
|
27
32
|
def self.add_locale_path(config)
|
28
33
|
config.i18n.railties_load_path.unshift(*WillPaginate::I18n.load_path)
|
29
34
|
end
|
35
|
+
|
36
|
+
# Extending the exception handler middleware so it properly detects
|
37
|
+
# WillPaginate::InvalidPage regardless of it being a tag module.
|
38
|
+
module ShowExceptionsPatch
|
39
|
+
extend ActiveSupport::Concern
|
40
|
+
included { alias_method_chain :status_code, :paginate }
|
41
|
+
private
|
42
|
+
def status_code_with_paginate(exception)
|
43
|
+
if exception.is_a?(WillPaginate::InvalidPage) or
|
44
|
+
(exception.respond_to?(:original_exception) &&
|
45
|
+
exception.original_exception.is_a?(WillPaginate::InvalidPage))
|
46
|
+
Rack::Utils.status_code(:not_found)
|
47
|
+
else
|
48
|
+
status_code_without_paginate(exception)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module ControllerRescuePatch
|
54
|
+
def rescue_from(*args, &block)
|
55
|
+
if idx = args.index(WillPaginate::InvalidPage)
|
56
|
+
args[idx] = args[idx].name
|
57
|
+
end
|
58
|
+
super(*args, &block)
|
59
|
+
end
|
60
|
+
end
|
30
61
|
end
|
31
62
|
end
|
data/lib/will_paginate/sequel.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
require 'sequel'
|
2
2
|
require 'sequel/extensions/pagination'
|
3
|
+
require 'will_paginate/collection'
|
3
4
|
|
4
5
|
module WillPaginate
|
5
6
|
# Sequel already supports pagination; we only need to make the
|
6
7
|
# resulting dataset look a bit more like WillPaginate::Collection
|
7
8
|
module SequelMethods
|
9
|
+
include WillPaginate::CollectionMethods
|
10
|
+
|
8
11
|
def total_pages
|
9
12
|
page_count
|
10
13
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'will_paginate/core_ext'
|
3
3
|
require 'will_paginate/i18n'
|
4
|
+
require 'will_paginate/deprecation'
|
4
5
|
|
5
6
|
module WillPaginate
|
6
7
|
# = Will Paginate view helpers
|
@@ -19,7 +20,7 @@ module WillPaginate
|
|
19
20
|
end
|
20
21
|
|
21
22
|
# default view options
|
22
|
-
self.pagination_options =
|
23
|
+
self.pagination_options = Deprecation::Hash.new \
|
23
24
|
:class => 'pagination',
|
24
25
|
:previous_label => nil,
|
25
26
|
:next_label => nil,
|
@@ -28,10 +29,14 @@ module WillPaginate
|
|
28
29
|
:link_separator => ' ', # single space is friendly to spiders and non-graphic browsers
|
29
30
|
:param_name => :page,
|
30
31
|
:params => nil,
|
31
|
-
:renderer => nil,
|
32
32
|
:page_links => true,
|
33
33
|
:container => true
|
34
|
+
|
35
|
+
label_deprecation = Proc.new { |key, value|
|
36
|
+
"set the 'will_paginate.#{key}' key in your i18n locale instead of editing pagination_options" if defined? Rails
|
34
37
|
}
|
38
|
+
pagination_options.deprecate_key(:previous_label, :next_label, &label_deprecation)
|
39
|
+
pagination_options.deprecate_key(:renderer) { |key, _| "pagination_options[#{key.inspect}] shouldn't be set globally" }
|
35
40
|
|
36
41
|
include WillPaginate::I18n
|
37
42
|
|
@@ -49,8 +54,8 @@ module WillPaginate
|
|
49
54
|
# * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
|
50
55
|
# * <tt>:params</tt> -- additional parameters when generating pagination links
|
51
56
|
# (eg. <tt>:controller => "foo", :action => nil</tt>)
|
52
|
-
# * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default:
|
53
|
-
# <tt>WillPaginate::LinkRenderer</tt>)
|
57
|
+
# * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default in Rails:
|
58
|
+
# <tt>WillPaginate::ActionView::LinkRenderer</tt>)
|
54
59
|
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
|
55
60
|
# * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
|
56
61
|
# false only when you are rendering your own pagination markup (default: true)
|
@@ -72,14 +72,18 @@ module WillPaginate
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def will_paginate_translate(keys, options = {})
|
75
|
-
if
|
76
|
-
|
77
|
-
|
75
|
+
if respond_to? :translate
|
76
|
+
if Array === keys
|
77
|
+
defaults = keys.dup
|
78
|
+
key = defaults.shift
|
79
|
+
else
|
80
|
+
defaults = nil
|
81
|
+
key = keys
|
82
|
+
end
|
83
|
+
translate(key, options.merge(:default => defaults, :scope => :will_paginate))
|
78
84
|
else
|
79
|
-
|
80
|
-
key = keys
|
85
|
+
super
|
81
86
|
end
|
82
|
-
translate(key, options.merge(:default => defaults, :scope => :will_paginate))
|
83
87
|
end
|
84
88
|
|
85
89
|
protected
|
@@ -101,9 +105,8 @@ module WillPaginate
|
|
101
105
|
|
102
106
|
def url(page)
|
103
107
|
@base_url_params ||= begin
|
104
|
-
url_params =
|
108
|
+
url_params = merge_get_params(default_url_params)
|
105
109
|
merge_optional_params(url_params)
|
106
|
-
url_params
|
107
110
|
end
|
108
111
|
|
109
112
|
url_params = @base_url_params.dup
|
@@ -112,15 +115,16 @@ module WillPaginate
|
|
112
115
|
@template.url_for(url_params)
|
113
116
|
end
|
114
117
|
|
115
|
-
def
|
116
|
-
|
117
|
-
|
118
|
-
|
118
|
+
def merge_get_params(url_params)
|
119
|
+
if @template.respond_to? :request and @template.request and @template.request.get?
|
120
|
+
symbolized_update(url_params, @template.params)
|
121
|
+
end
|
119
122
|
url_params
|
120
123
|
end
|
121
124
|
|
122
125
|
def merge_optional_params(url_params)
|
123
126
|
symbolized_update(url_params, @options[:params]) if @options[:params]
|
127
|
+
url_params
|
124
128
|
end
|
125
129
|
|
126
130
|
def add_current_page_param(url_params, page)
|
@@ -132,10 +136,6 @@ module WillPaginate
|
|
132
136
|
end
|
133
137
|
end
|
134
138
|
|
135
|
-
def get_request?
|
136
|
-
@template.request.get?
|
137
|
-
end
|
138
|
-
|
139
139
|
private
|
140
140
|
|
141
141
|
def parse_query_parameters(params)
|
@@ -35,7 +35,7 @@ module WillPaginate
|
|
35
35
|
# Returns the subset of +options+ this instance was initialized with that
|
36
36
|
# represent HTML attributes for the container element of pagination links.
|
37
37
|
def container_attributes
|
38
|
-
@container_attributes ||= @options.except(*(ViewHelpers.pagination_options.keys - [:class]))
|
38
|
+
@container_attributes ||= @options.except(*(ViewHelpers.pagination_options.keys + [:renderer] - [:class]))
|
39
39
|
end
|
40
40
|
|
41
41
|
protected
|
data/spec/collection_spec.rb
CHANGED
@@ -71,12 +71,20 @@ describe WillPaginate::Collection do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
describe "out of bounds" do
|
75
|
+
it "is out of bounds when page number is too high" do
|
76
|
+
create(2, 3, 2).should be_out_of_bounds
|
77
|
+
end
|
78
|
+
|
79
|
+
it "isn't out of bounds when inside collection" do
|
80
|
+
create(1, 3, 2).should_not be_out_of_bounds
|
81
|
+
end
|
82
|
+
|
83
|
+
it "isn't out of bounds when the collection is empty" do
|
84
|
+
collection = create(1, 3, 0)
|
85
|
+
collection.should_not be_out_of_bounds
|
86
|
+
collection.total_pages.should == 1
|
87
|
+
end
|
80
88
|
end
|
81
89
|
|
82
90
|
describe "guessing total count" do
|
@@ -91,6 +91,13 @@ describe WillPaginate::ActiveRecord do
|
|
91
91
|
rel = Developer.page(3).limit(3)
|
92
92
|
rel.offset.should == 6
|
93
93
|
end
|
94
|
+
|
95
|
+
it "keeps pagination data after 'scoped'" do
|
96
|
+
rel = Developer.page(2).scoped
|
97
|
+
rel.per_page.should == 10
|
98
|
+
rel.offset.should == 10
|
99
|
+
rel.current_page.should == 2
|
100
|
+
end
|
94
101
|
end
|
95
102
|
|
96
103
|
describe "counting" do
|
@@ -131,6 +138,13 @@ describe WillPaginate::ActiveRecord do
|
|
131
138
|
topics.should_not be_empty
|
132
139
|
}.should run_queries(1)
|
133
140
|
end
|
141
|
+
|
142
|
+
it "support empty? for grouped queries" do
|
143
|
+
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
144
|
+
lambda {
|
145
|
+
topics.should_not be_empty
|
146
|
+
}.should run_queries(1)
|
147
|
+
end
|
134
148
|
|
135
149
|
it "overrides total_entries count with a fixed value" do
|
136
150
|
lambda {
|
@@ -165,17 +179,16 @@ describe WillPaginate::ActiveRecord do
|
|
165
179
|
it "should count with group" do
|
166
180
|
Developer.group(:salary).page(1).total_entries.should == 4
|
167
181
|
end
|
182
|
+
|
183
|
+
it "should not have zero total_pages when the result set is empty" do
|
184
|
+
Developer.where("1 = 2").page(1).total_pages.should == 1
|
185
|
+
end
|
168
186
|
end
|
169
187
|
|
170
188
|
it "should not ignore :select parameter when it says DISTINCT" do
|
171
189
|
users = User.select('DISTINCT salary').paginate :page => 2
|
172
190
|
users.total_entries.should == 5
|
173
191
|
end
|
174
|
-
|
175
|
-
it "should count with scoped select when :select => DISTINCT" do
|
176
|
-
pending
|
177
|
-
Topic.distinct.paginate :page => 2
|
178
|
-
end
|
179
192
|
|
180
193
|
describe "paginate_by_sql" do
|
181
194
|
it "should respond" do
|
@@ -311,7 +324,7 @@ describe WillPaginate::ActiveRecord do
|
|
311
324
|
|
312
325
|
lambda {
|
313
326
|
# with association-specified order
|
314
|
-
result = dhh.projects.paginate(:page => 1)
|
327
|
+
result = ignore_deprecation { dhh.projects.paginate(:page => 1) }
|
315
328
|
result.should == expected_name_ordered
|
316
329
|
result.total_entries.should == 2
|
317
330
|
}.should run_queries(2)
|
@@ -402,7 +415,7 @@ describe WillPaginate::ActiveRecord do
|
|
402
415
|
it "should paginate on habtm association" do
|
403
416
|
project = projects(:active_record)
|
404
417
|
lambda {
|
405
|
-
result = project.developers.poor.paginate :page => 1, :per_page => 1
|
418
|
+
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
406
419
|
result.size.should == 1
|
407
420
|
result.total_entries.should == 1
|
408
421
|
}.should run_queries(2)
|
@@ -452,6 +465,10 @@ describe WillPaginate::ActiveRecord do
|
|
452
465
|
|
453
466
|
protected
|
454
467
|
|
468
|
+
def ignore_deprecation
|
469
|
+
ActiveSupport::Deprecation.silence { yield }
|
470
|
+
end
|
471
|
+
|
455
472
|
def run_queries(num)
|
456
473
|
QueryCountMatcher.new(num)
|
457
474
|
end
|
@@ -76,4 +76,8 @@ describe WillPaginate::DataMapper do
|
|
76
76
|
Animal.all(:limit => 2).page(2).per_page.should == 2
|
77
77
|
end
|
78
78
|
|
79
|
+
it "has total_pages at 1 for empty collections" do
|
80
|
+
Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1
|
81
|
+
end
|
82
|
+
|
79
83
|
end if datamapper_loaded
|
data/spec/spec_helper.rb
CHANGED
@@ -6,26 +6,18 @@ rescue LoadError
|
|
6
6
|
# no debugger available
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def include_phrase(string)
|
13
|
-
PhraseMatcher.new(string)
|
14
|
-
end
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.include Module.new {
|
11
|
+
protected
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
params[:per_page] = 1
|
19
|
-
params[:total_entries] = params[:total_pages]
|
13
|
+
def include_phrase(string)
|
14
|
+
PhraseMatcher.new(string)
|
20
15
|
end
|
21
|
-
WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
|
22
|
-
end
|
23
|
-
end
|
24
16
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
17
|
+
def have_deprecation(msg)
|
18
|
+
DeprecationMatcher.new(msg)
|
19
|
+
end
|
20
|
+
}
|
29
21
|
|
30
22
|
config.mock_with :mocha
|
31
23
|
end
|
@@ -33,7 +25,7 @@ end
|
|
33
25
|
class PhraseMatcher
|
34
26
|
def initialize(string)
|
35
27
|
@string = string
|
36
|
-
@pattern = /\b#{string}\b/
|
28
|
+
@pattern = /\b#{Regexp.escape string}\b/
|
37
29
|
end
|
38
30
|
|
39
31
|
def matches?(actual)
|
@@ -49,3 +41,31 @@ class PhraseMatcher
|
|
49
41
|
"expected #{@actual.inspect} not to contain phrase #{@string.inspect}"
|
50
42
|
end
|
51
43
|
end
|
44
|
+
|
45
|
+
require 'stringio'
|
46
|
+
|
47
|
+
class DeprecationMatcher
|
48
|
+
def initialize(message)
|
49
|
+
@message = message
|
50
|
+
end
|
51
|
+
|
52
|
+
def matches?(block)
|
53
|
+
@actual = hijack_stderr(&block)
|
54
|
+
PhraseMatcher.new("DEPRECATION WARNING: #{@message}").matches?(@actual)
|
55
|
+
end
|
56
|
+
|
57
|
+
def failure_message
|
58
|
+
"expected deprecation warning #{@message.inspect}, got #{@actual.inspect}"
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def hijack_stderr
|
64
|
+
err = $stderr
|
65
|
+
$stderr = StringIO.new
|
66
|
+
yield
|
67
|
+
$stderr.string.rstrip
|
68
|
+
ensure
|
69
|
+
$stderr = err
|
70
|
+
end
|
71
|
+
end
|
@@ -293,6 +293,45 @@ describe WillPaginate::ActionView do
|
|
293
293
|
end
|
294
294
|
end
|
295
295
|
|
296
|
+
it "renders using ActionView helpers on a custom object" do
|
297
|
+
helper = Object.new
|
298
|
+
class << helper
|
299
|
+
attr_reader :controller
|
300
|
+
include ActionView::Helpers::UrlHelper
|
301
|
+
include Routes.url_helpers
|
302
|
+
include WillPaginate::ActionView
|
303
|
+
end
|
304
|
+
helper.default_url_options[:controller] = 'dummy'
|
305
|
+
|
306
|
+
collection = WillPaginate::Collection.new(2, 1, 3)
|
307
|
+
@render_output = helper.will_paginate(collection)
|
308
|
+
|
309
|
+
assert_select 'a[href]', 4 do |links|
|
310
|
+
urls = links.map {|l| l['href'] }.uniq
|
311
|
+
urls.should == ['/dummy/page/1', '/dummy/page/3']
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
it "renders using ActionDispatch helper on a custom object" do
|
316
|
+
helper = Object.new
|
317
|
+
class << helper
|
318
|
+
include ActionDispatch::Routing::UrlFor
|
319
|
+
include Routes.url_helpers
|
320
|
+
include WillPaginate::ActionView
|
321
|
+
end
|
322
|
+
helper.default_url_options[:host] = 'example.com'
|
323
|
+
helper.default_url_options[:controller] = 'dummy'
|
324
|
+
# helper.default_url_options[:only_path] = true
|
325
|
+
|
326
|
+
collection = WillPaginate::Collection.new(2, 1, 3)
|
327
|
+
@render_output = helper.will_paginate(collection)
|
328
|
+
|
329
|
+
assert_select 'a[href]', 4 do |links|
|
330
|
+
urls = links.map {|l| l['href'] }.uniq
|
331
|
+
urls.should == ['http://example.com/dummy/page/1', 'http://example.com/dummy/page/3']
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
296
335
|
private
|
297
336
|
|
298
337
|
def translation(data)
|
@@ -33,6 +33,20 @@ describe WillPaginate::ViewHelpers do
|
|
33
33
|
will_paginate(collection).should be_nil
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
describe "pagination_options" do
|
38
|
+
let(:pagination_options) { WillPaginate::ViewHelpers.pagination_options }
|
39
|
+
|
40
|
+
it "deprecates setting :renderer" do
|
41
|
+
begin
|
42
|
+
lambda {
|
43
|
+
pagination_options[:renderer] = 'test'
|
44
|
+
}.should have_deprecation("pagination_options[:renderer] shouldn't be set")
|
45
|
+
ensure
|
46
|
+
pagination_options.delete :renderer
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
36
50
|
|
37
51
|
describe "page_entries_info" do
|
38
52
|
before :all do
|
@@ -71,7 +71,15 @@ describe WillPaginate::ViewHelpers::LinkRendererBase do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
protected
|
74
|
-
|
74
|
+
|
75
|
+
def collection(params = {})
|
76
|
+
if params[:total_pages]
|
77
|
+
params[:per_page] = 1
|
78
|
+
params[:total_entries] = params[:total_pages]
|
79
|
+
end
|
80
|
+
WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
|
81
|
+
end
|
82
|
+
|
75
83
|
def prepare(collection_options, options = {})
|
76
84
|
@renderer.prepare(collection(collection_options), options)
|
77
85
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: will_paginate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 3.0.
|
5
|
+
version: 3.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Mislav Marohni\xC4\x87"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-09-17 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: will_paginate provides a simple API for performing paginated queries with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination links in Rails, Sinatra and Merb web apps.
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- lib/will_paginate/collection.rb
|
30
30
|
- lib/will_paginate/core_ext.rb
|
31
31
|
- lib/will_paginate/data_mapper.rb
|
32
|
+
- lib/will_paginate/deprecation.rb
|
32
33
|
- lib/will_paginate/i18n.rb
|
33
34
|
- lib/will_paginate/locale/en.yml
|
34
35
|
- lib/will_paginate/page_number.rb
|
@@ -100,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
101
|
requirements: []
|
101
102
|
|
102
103
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.8.
|
104
|
+
rubygems_version: 1.8.8
|
104
105
|
signing_key:
|
105
106
|
specification_version: 3
|
106
107
|
summary: Pagination plugin for web frameworks and other apps
|