public
Rubygem
Description: Most awesome pagination solution for Rails
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/mislav/will_paginate.git
Search Repo:
specs for common view helpers
mislav (author)
Fri May 09 02:48:50 -0700 2008
commit  ad66001d7f816ddfcdaf24e75c0fd1fd1dfc464a
tree    51cd7137d5ad585605c58e0e3e420e5bb62c05c3
parent  42c8e31fde4c554a56a99614dbd834c5c1da57ca
...
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -30,4 +30,28 @@
0
     end
0
   end
0
 end
0
+
0
+unless String.instance_methods.include? 'constantize'
0
+ String.class_eval do
0
+ def constantize
0
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
0
+ raise NameError, "#{self.inspect} is not a valid constant name!"
0
+ end
0
+
0
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
0
+ end
0
+ end
0
+end
0
+
0
+unless String.instance_methods.include? 'underscore'
0
+ String.class_eval do
0
+ def underscore
0
+ self.to_s.gsub(/::/, '/').
0
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
0
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
0
+ tr("-", "_").
0
+ downcase
0
+ end
0
+ end
0
+end
...
80
81
82
 
 
 
 
 
 
 
 
 
83
84
85
 
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
88
89
90
91
 
 
 
92
93
94
 
95
96
97
...
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
0
@@ -80,18 +80,46 @@
0
       #
0
       # <%= page_entries_info @posts, :entry_name => 'item' %>
0
       # #-> Displaying items 6 - 10 of 26 in total
0
+ #
0
+ # Entry name is entered in singular and pluralized with
0
+ # <tt>String#pluralize</tt> method from ActiveSupport. If it isn't
0
+ # loaded, specify plural with <tt>:plural_name</tt> parameter:
0
+ #
0
+ # <%= page_entries_info @posts, :entry_name => 'item', :plural_name => 'items' %>
0
+ #
0
+ # By default, this method produces HTML output. You can trigger plain
0
+ # text output by passing <tt>:html => false</tt> in options.
0
       def page_entries_info(collection, options = {})
0
- entry_name = options[:entry_name] ||
0
- (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
0
+ entry_name = options[:entry_name] || (collection.empty?? 'entry' :
0
+ collection.first.class.name.underscore.gsub('_', ' '))
0
         
0
+ plural_name = if options[:plural_name]
0
+ options[:plural_name]
0
+ elsif entry_name == 'entry'
0
+ plural_name = 'entries'
0
+ elsif entry_name.respond_to? :pluralize
0
+ plural_name = entry_name.pluralize
0
+ else
0
+ entry_name + 's'
0
+ end
0
+
0
+ unless options[:html] == false
0
+ b = '<b>'
0
+ eb = '</b>'
0
+ sp = '&nbsp;'
0
+ else
0
+ b = eb = ''
0
+ sp = ' '
0
+ end
0
+
0
         if collection.total_pages < 2
0
           case collection.size
0
- when 0; "No #{entry_name.pluralize} found"
0
- when 1; "Displaying <b>1</b> #{entry_name}"
0
- else; "Displaying <b>all #{collection.size}</b> #{entry_name.pluralize}"
0
+ when 0; "No #{plural_name} found"
0
+ when 1; "Displaying #{b}1#{eb} #{entry_name}"
0
+ else; "Displaying #{b}all #{collection.size}#{eb} #{plural_name}"
0
           end
0
         else
0
- %{Displaying #{entry_name.pluralize} <b>%d&nbsp;-&nbsp;%d</b> of <b>%d</b> in total} % [
0
+ %{Displaying #{plural_name} #{b}%d#{sp}-#{sp}%d#{eb} of #{b}%d#{eb} in total} % [
0
             collection.offset + 1,
0
             collection.offset + collection.length,
0
             collection.total_entries
...
2
3
4
 
 
 
 
 
 
5
6
 
7
8
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
...
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
0
@@ -2,10 +2,37 @@
0
 gem 'rspec', '~> 1.1.3'
0
 require 'spec'
0
 
0
+module StringMatchers
0
+ def include_words(string)
0
+ WordsInclusionMatcher.new(string)
0
+ end
0
+end
0
+
0
 Spec::Runner.configure do |config|
0
   # config.include My::Pony, My::Horse, :type => :farm
0
+ config.include StringMatchers
0
   # config.predicate_matchers[:swim] = :can_swim?
0
   
0
   config.mock_with :mocha
0
+end
0
+
0
+class WordsInclusionMatcher
0
+ def initialize(string)
0
+ @string = string
0
+ @pattern = /\b#{string}\b/
0
+ end
0
+
0
+ def matches?(actual)
0
+ @actual = actual.to_s
0
+ @actual =~ @pattern
0
+ end
0
+
0
+ def failure_message
0
+ "expected #{@actual.inspect} to contain words #{@string.inspect}"
0
+ end
0
+
0
+ def negative_failure_message
0
+ "expected #{@actual.inspect} not to contain words #{@string.inspect}"
0
+ end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,61 @@
0
+require 'spec_helper'
0
+require 'will_paginate/view_helpers/common'
0
+require 'will_paginate/array'
0
+
0
+describe WillPaginate::ViewHelpers::Common do
0
+
0
+ include WillPaginate::ViewHelpers::Common
0
+
0
+ describe "will_paginate" do
0
+ it "should render" do
0
+ collection = WillPaginate::Collection.new(1, 2, 4)
0
+ will_paginate(collection).should_not be_nil
0
+ end
0
+
0
+ it "should return nil for single-page collections" do
0
+ collection = mock 'Collection', :total_pages => 1
0
+ will_paginate(collection).should be_nil
0
+ end
0
+ end
0
+
0
+ describe "page_entries_info" do
0
+ before :all do
0
+ @array = ('a'..'z').to_a
0
+ end
0
+
0
+ def info(params, options = {})
0
+ options[:html] ||= false unless options.key?(:html) and options[:html].nil?
0
+ collection = Hash === params ? @array.paginate(params) : params
0
+ page_entries_info collection, options
0
+ end
0
+
0
+ it "should display middle results and total count" do
0
+ info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
0
+ end
0
+
0
+ it "should output HTML by default" do
0
+ info({ :page => 2, :per_page => 5 }, :html => nil).should ==
0
+ "Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total"
0
+ end
0
+
0
+ it "should display shortened end results" do
0
+ info(:page => 7, :per_page => 4).should include_words('strings 25 - 26')
0
+ end
0
+
0
+ it "should handle longer class names" do
0
+ collection = @array.paginate(:page => 2, :per_page => 5)
0
+ collection.first.stubs(:class).returns(mock('Class', :name => 'ProjectType'))
0
+ info(collection).should include_words('project types')
0
+ end
0
+
0
+ it "should adjust output for single-page collections" do
0
+ info(('a'..'d').to_a.paginate(:page => 1, :per_page => 5)).should == "Displaying all 4 strings"
0
+ info(['a'].paginate(:page => 1, :per_page => 5)).should == "Displaying 1 string"
0
+ end
0
+
0
+ it "should display 'no entries found' for empty collections" do
0
+ info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
0
+ end
0
+ end
0
+end
...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
...
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164
165
0
@@ -160,59 +160,6 @@
0
     assert_select 'div.pagination', 2
0
     assert_select 'div.pagination + div#developers', 1
0
   end
0
-
0
- def test_page_entries_info
0
- @template = '<%= page_entries_info collection %>'
0
- array = ('a'..'z').to_a
0
-
0
- paginate array.paginate(:page => 2, :per_page => 5)
0
- assert_equal %{Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total},
0
- @html_result
0
-
0
- paginate array.paginate(:page => 7, :per_page => 4)
0
- assert_equal %{Displaying strings <b>25&nbsp;-&nbsp;26</b> of <b>26</b> in total},
0
- @html_result
0
- end
0
-
0
- def test_page_entries_info_with_longer_class_name
0
- @template = '<%= page_entries_info collection %>'
0
- collection = ('a'..'z').to_a.paginate
0
- collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
0
-
0
- paginate collection
0
- assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
0
- end
0
-
0
- def test_page_entries_info_with_single_page_collection
0
- @template = '<%= page_entries_info collection %>'
0
-
0
- paginate(('a'..'d').to_a.paginate(:page => 1, :per_page => 5))
0
- assert_equal %{Displaying <b>all 4</b> strings}, @html_result
0
-
0
- paginate(['a'].paginate(:page => 1, :per_page => 5))
0
- assert_equal %{Displaying <b>1</b> string}, @html_result
0
-
0
- paginate([].paginate(:page => 1, :per_page => 5))
0
- assert_equal %{No entries found}, @html_result
0
- end
0
-
0
- def test_page_entries_info_with_custom_entry_name
0
- @template = '<%= page_entries_info collection, :entry_name => "author" %>'
0
-
0
- entries = (1..20).to_a
0
-
0
- paginate(entries.paginate(:page => 1, :per_page => 5))
0
- assert_equal %{Displaying authors <b>1&nbsp;-&nbsp;5</b> of <b>20</b> in total}, @html_result
0
-
0
- paginate(entries.paginate(:page => 1, :per_page => 20))
0
- assert_equal %{Displaying <b>all 20</b> authors}, @html_result
0
-
0
- paginate(['a'].paginate(:page => 1, :per_page => 5))
0
- assert_equal %{Displaying <b>1</b> author}, @html_result
0
-
0
- paginate([].paginate(:page => 1, :per_page => 5))
0
- assert_equal %{No authors found}, @html_result
0
- end
0
   
0
   ## parameter handling in page links ##
0
   

Comments

    No one has commented yet.