will_paginate 3.0.pre4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of will_paginate might be problematic. Click here for more details.

Files changed (51) hide show
  1. data/README.md +61 -0
  2. data/Rakefile +19 -36
  3. data/lib/will_paginate.rb +16 -14
  4. data/lib/will_paginate/{finders/active_record.rb → active_record.rb} +66 -35
  5. data/lib/will_paginate/array.rb +5 -5
  6. data/lib/will_paginate/collection.rb +12 -36
  7. data/lib/will_paginate/core_ext.rb +0 -28
  8. data/lib/will_paginate/data_mapper.rb +91 -0
  9. data/lib/will_paginate/i18n.rb +22 -0
  10. data/lib/will_paginate/locale/en.yml +33 -0
  11. data/lib/will_paginate/page_number.rb +57 -0
  12. data/lib/will_paginate/per_page.rb +27 -0
  13. data/lib/will_paginate/railtie.rb +13 -4
  14. data/lib/will_paginate/sequel.rb +36 -0
  15. data/lib/will_paginate/version.rb +1 -1
  16. data/lib/will_paginate/view_helpers.rb +136 -22
  17. data/lib/will_paginate/view_helpers/action_view.rb +129 -117
  18. data/lib/will_paginate/view_helpers/link_renderer.rb +10 -11
  19. data/lib/will_paginate/view_helpers/link_renderer_base.rb +2 -8
  20. data/lib/will_paginate/view_helpers/merb.rb +20 -7
  21. data/lib/will_paginate/view_helpers/sinatra.rb +41 -0
  22. data/spec/ci.rb +29 -0
  23. data/spec/collection_spec.rb +7 -23
  24. data/spec/console +9 -1
  25. data/spec/console_fixtures.rb +1 -3
  26. data/spec/database.yml +10 -10
  27. data/spec/finders/active_record_spec.rb +82 -28
  28. data/spec/finders/activerecord_test_connector.rb +9 -1
  29. data/spec/finders/data_mapper_spec.rb +59 -48
  30. data/spec/finders/data_mapper_test_connector.rb +8 -1
  31. data/spec/finders/sequel_spec.rb +9 -3
  32. data/spec/fixtures/project.rb +2 -0
  33. data/spec/page_number_spec.rb +65 -0
  34. data/spec/per_page_spec.rb +41 -0
  35. data/spec/spec_helper.rb +5 -29
  36. data/spec/view_helpers/action_view_spec.rb +48 -32
  37. data/spec/view_helpers/base_spec.rb +59 -7
  38. data/spec/view_helpers/link_renderer_base_spec.rb +7 -12
  39. data/spec/view_helpers/view_example_group.rb +1 -0
  40. metadata +22 -23
  41. data/CHANGELOG.rdoc +0 -105
  42. data/README.rdoc +0 -111
  43. data/lib/will_paginate/deprecation.rb +0 -50
  44. data/lib/will_paginate/finders.rb +0 -9
  45. data/lib/will_paginate/finders/active_resource.rb +0 -51
  46. data/lib/will_paginate/finders/base.rb +0 -112
  47. data/lib/will_paginate/finders/data_mapper.rb +0 -30
  48. data/lib/will_paginate/finders/sequel.rb +0 -23
  49. data/lib/will_paginate/view_helpers/base.rb +0 -126
  50. data/spec/finders/active_resource_spec.rb +0 -52
  51. data/spec/finders_spec.rb +0 -76
@@ -10,4 +10,6 @@ class Project < ActiveRecord::Base
10
10
  scoped.where(['replies.created_at > ?', 15.minutes.ago])
11
11
  end
12
12
  end
13
+
14
+ has_many :unique_replies, :through => :topics, :source => :replies, :uniq => true
13
15
  end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'will_paginate/page_number'
3
+
4
+ describe WillPaginate::PageNumber do
5
+ describe "valid" do
6
+ subject { described_class.new('12', 'page') }
7
+
8
+ it { should eq(12) }
9
+ its(:inspect) { should eq('page 12') }
10
+ it { should be_a(WillPaginate::PageNumber) }
11
+ it { should be_instance_of(WillPaginate::PageNumber) }
12
+ it { should be_a(Numeric) }
13
+ it { should be_a(Fixnum) }
14
+ it { should_not be_instance_of(Fixnum) }
15
+
16
+ it "passes the PageNumber=== type check" do |variable|
17
+ (WillPaginate::PageNumber === subject).should be
18
+ end
19
+
20
+ it "passes the Numeric=== type check" do |variable|
21
+ (Numeric === subject).should be
22
+ (Fixnum === subject).should be
23
+ end
24
+ end
25
+
26
+ describe "invalid" do
27
+ def create(value, name = 'page')
28
+ described_class.new(value, name)
29
+ end
30
+
31
+ it "errors out on non-int values" do
32
+ lambda { create(nil) }.should raise_error(WillPaginate::InvalidPage)
33
+ lambda { create('') }.should raise_error(WillPaginate::InvalidPage)
34
+ lambda { create('Schnitzel') }.should raise_error(WillPaginate::InvalidPage)
35
+ end
36
+
37
+ it "errors out on zero or less" do
38
+ lambda { create(0) }.should raise_error(WillPaginate::InvalidPage)
39
+ lambda { create(-1) }.should raise_error(WillPaginate::InvalidPage)
40
+ end
41
+
42
+ it "doesn't error out on zero for 'offset'" do
43
+ lambda { create(0, 'offset') }.should_not raise_error
44
+ lambda { create(-1, 'offset') }.should raise_error(WillPaginate::InvalidPage)
45
+ end
46
+ end
47
+
48
+ describe "coercion method" do
49
+ it "defaults to 'page' name" do
50
+ num = WillPaginate::PageNumber(12)
51
+ num.inspect.should eq('page 12')
52
+ end
53
+
54
+ it "accepts a custom name" do
55
+ num = WillPaginate::PageNumber(12, 'monkeys')
56
+ num.inspect.should eq('monkeys 12')
57
+ end
58
+
59
+ it "doesn't affect PageNumber instances" do
60
+ num = WillPaginate::PageNumber(12)
61
+ num2 = WillPaginate::PageNumber(num)
62
+ num2.object_id.should eq(num.object_id)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'will_paginate/per_page'
3
+
4
+ describe WillPaginate::PerPage do
5
+
6
+ class MyModel
7
+ extend WillPaginate::PerPage
8
+ end
9
+
10
+ it "has the default value" do
11
+ MyModel.per_page.should == 30
12
+
13
+ WillPaginate.per_page = 10
14
+ begin
15
+ MyModel.per_page.should == 10
16
+ ensure
17
+ WillPaginate.per_page = 30
18
+ end
19
+ end
20
+
21
+ it "casts values to int" do
22
+ WillPaginate.per_page = '10'
23
+ begin
24
+ MyModel.per_page.should == 10
25
+ ensure
26
+ WillPaginate.per_page = 30
27
+ end
28
+ end
29
+
30
+ it "has an explicit value" do
31
+ MyModel.per_page = 12
32
+ begin
33
+ MyModel.per_page.should == 12
34
+ subclass = Class.new(MyModel)
35
+ subclass.per_page.should == 12
36
+ ensure
37
+ MyModel.send(:remove_instance_variable, '@per_page')
38
+ end
39
+ end
40
+
41
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require 'rspec'
2
2
  require File.expand_path('../view_helpers/view_example_group', __FILE__)
3
+ begin
4
+ require 'ruby-debug'
5
+ rescue LoadError
6
+ # no debugger available
7
+ end
3
8
 
4
9
  module MyExtras
5
10
  protected
@@ -15,10 +20,6 @@ module MyExtras
15
20
  end
16
21
  WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
17
22
  end
18
-
19
- def have_deprecation
20
- DeprecationMatcher.new
21
- end
22
23
  end
23
24
 
24
25
  RSpec.configure do |config|
@@ -48,28 +49,3 @@ class PhraseMatcher
48
49
  "expected #{@actual.inspect} not to contain phrase #{@string.inspect}"
49
50
  end
50
51
  end
51
-
52
- class DeprecationMatcher
53
- def initialize
54
- @old_behavior = WillPaginate::Deprecation.behavior
55
- @messages = []
56
- WillPaginate::Deprecation.behavior = lambda { |message, callstack|
57
- @messages << message
58
- }
59
- end
60
-
61
- def matches?(block)
62
- block.call
63
- !@messages.empty?
64
- ensure
65
- WillPaginate::Deprecation.behavior = @old_behavior
66
- end
67
-
68
- def failure_message
69
- "expected block to raise a deprecation warning"
70
- end
71
-
72
- def negative_failure_message
73
- "expected block not to raise deprecation warnings, #{@messages.size} raised"
74
- end
75
- end
@@ -4,8 +4,6 @@ require 'action_controller'
4
4
  require 'will_paginate/view_helpers/action_view'
5
5
  require 'will_paginate/collection'
6
6
 
7
- ActionView::Base.send(:include, WillPaginate::ViewHelpers::ActionView)
8
-
9
7
  Routes = ActionDispatch::Routing::RouteSet.new
10
8
 
11
9
  Routes.draw do
@@ -17,7 +15,16 @@ Routes.draw do
17
15
  match ':controller(/:action(/:id(.:format)))'
18
16
  end
19
17
 
20
- describe WillPaginate::ViewHelpers::ActionView do
18
+ describe WillPaginate::ActionView do
19
+
20
+ before(:all) do
21
+ I18n.load_path.concat WillPaginate::I18n.load_path
22
+ end
23
+
24
+ before(:each) do
25
+ I18n.reload!
26
+ end
27
+
21
28
  before(:each) do
22
29
  @assigns = {}
23
30
  @controller = DummyController.new
@@ -44,7 +51,7 @@ describe WillPaginate::ViewHelpers::ActionView do
44
51
  end
45
52
  assert_select 'span', 1
46
53
  assert_select 'span.disabled:first-child', '&#8592; Previous'
47
- assert_select 'em', '1'
54
+ assert_select 'em.current', '1'
48
55
  pagination.first.inner_text.should == '&#8592; Previous 1 2 3 Next &#8594;'
49
56
  end
50
57
  end
@@ -68,7 +75,7 @@ describe WillPaginate::ViewHelpers::ActionView do
68
75
  link.first['rel'].should == 'next'
69
76
  end
70
77
  end
71
- assert_select 'em', '2'
78
+ assert_select '.current', '2'
72
79
  end
73
80
  end
74
81
 
@@ -79,7 +86,7 @@ describe WillPaginate::ViewHelpers::ActionView do
79
86
  end
80
87
 
81
88
  it "should paginate using a custom renderer instance" do
82
- renderer = WillPaginate::ViewHelpers::LinkRenderer.new
89
+ renderer = WillPaginate::ActionView::LinkRenderer.new
83
90
  def renderer.gap() '<span class="my-gap">~~</span>' end
84
91
 
85
92
  paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
@@ -98,20 +105,12 @@ describe WillPaginate::ViewHelpers::ActionView do
98
105
  assert_select 'a.next_page[href]:last-child'
99
106
  end
100
107
  end
101
-
102
- it "should warn about :prev_label being deprecated" do
103
- lambda {
104
- paginate({ :page => 2 }, :prev_label => 'Deprecated') do
105
- assert_select 'a[href]:first-child', 'Deprecated'
106
- end
107
- }.should have_deprecation
108
- end
109
108
 
110
109
  it "should match expected markup" do
111
110
  paginate
112
111
  expected = <<-HTML
113
112
  <div class="pagination"><span class="previous_page disabled">&#8592; Previous</span>
114
- <em>1</em>
113
+ <em class="current">1</em>
115
114
  <a href="/foo/bar?page=2" rel="next">2</a>
116
115
  <a href="/foo/bar?page=3">3</a>
117
116
  <a href="/foo/bar?page=2" class="next_page" rel="next">Next &#8594;</a></div>
@@ -148,22 +147,6 @@ describe WillPaginate::ViewHelpers::ActionView do
148
147
  end
149
148
  end
150
149
 
151
- it "should have magic HTML ID for the container" do
152
- paginate do |div|
153
- div.first['id'].should be_nil
154
- end
155
-
156
- # magic ID
157
- paginate({}, :id => true) do |div|
158
- div.first['id'].should == 'fixnums_pagination'
159
- end
160
-
161
- # explicit ID
162
- paginate({}, :id => 'custom_id') do |div|
163
- div.first['id'].should == 'custom_id'
164
- end
165
- end
166
-
167
150
  ## other helpers ##
168
151
 
169
152
  it "should render a paginated section" do
@@ -177,6 +160,18 @@ describe WillPaginate::ViewHelpers::ActionView do
177
160
  assert_select 'div.pagination', 2
178
161
  assert_select 'div.pagination + div#developers', 1
179
162
  end
163
+
164
+ it "should not render a paginated section with a single page" do
165
+ @template = <<-ERB
166
+ <%= paginated_section collection, options do %>
167
+ <%= content_tag :div, '', :id => "developers" %>
168
+ <% end %>
169
+ ERB
170
+
171
+ paginate(:total_entries => 1)
172
+ assert_select 'div.pagination', 0
173
+ assert_select 'div#developers', 1
174
+ end
180
175
 
181
176
  ## parameter handling in page links ##
182
177
 
@@ -283,9 +278,30 @@ describe WillPaginate::ViewHelpers::ActionView do
283
278
  paginate(nil)
284
279
  }.should raise_error(ActionView::TemplateError, /@developers/)
285
280
  end
281
+
282
+ ## i18n
283
+
284
+ it "is able to translate previous/next labels" do
285
+ translation :will_paginate => {
286
+ :previous_label => 'Go back',
287
+ :next_label => 'Load more'
288
+ }
289
+
290
+ paginate do |pagination|
291
+ assert_select 'span.disabled:first-child', 'Go back'
292
+ assert_select 'a[rel=next]', 'Load more'
293
+ end
294
+ end
295
+
296
+ private
297
+
298
+ def translation(data)
299
+ I18n.available_locales # triggers loading existing translations
300
+ I18n.backend.store_translations(:en, data)
301
+ end
286
302
  end
287
303
 
288
- class AdditionalLinkAttributesRenderer < WillPaginate::ViewHelpers::LinkRenderer
304
+ class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
289
305
  def initialize(link_attributes = nil)
290
306
  super()
291
307
  @additional_link_attributes = link_attributes || { :default => 'true' }
@@ -1,10 +1,22 @@
1
1
  require 'spec_helper'
2
- require 'will_paginate/view_helpers/base'
2
+ require 'will_paginate/view_helpers'
3
3
  require 'will_paginate/array'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'active_support/inflections'
4
7
 
5
- describe WillPaginate::ViewHelpers::Base do
8
+ describe WillPaginate::ViewHelpers do
6
9
 
7
- include WillPaginate::ViewHelpers::Base
10
+ before(:all) do
11
+ # make sure default translations aren't loaded
12
+ I18n.load_path.clear
13
+ end
14
+
15
+ before(:each) do
16
+ I18n.reload!
17
+ end
18
+
19
+ include WillPaginate::ViewHelpers
8
20
 
9
21
  describe "will_paginate" do
10
22
  it "should render" do
@@ -28,17 +40,31 @@ describe WillPaginate::ViewHelpers::Base do
28
40
  end
29
41
 
30
42
  def info(params, options = {})
31
- options[:html] ||= false unless options.key?(:html) and options[:html].nil?
32
43
  collection = Hash === params ? @array.paginate(params) : params
33
- page_entries_info collection, options
44
+ page_entries_info collection, {:html => false}.merge(options)
34
45
  end
35
46
 
36
47
  it "should display middle results and total count" do
37
48
  info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
38
49
  end
39
50
 
51
+ it "uses translation if available" do
52
+ translation :will_paginate => {
53
+ :page_entries_info => {:multi_page => 'Showing %{from} - %{to}'}
54
+ }
55
+ info(:page => 2, :per_page => 5).should == "Showing 6 - 10"
56
+ end
57
+
58
+ it "uses specific translation if available" do
59
+ translation :will_paginate => {
60
+ :page_entries_info => {:multi_page => 'Showing %{from} - %{to}'},
61
+ :string => { :page_entries_info => {:multi_page => 'Strings %{from} to %{to}'} }
62
+ }
63
+ info(:page => 2, :per_page => 5).should == "Strings 6 to 10"
64
+ end
65
+
40
66
  it "should output HTML by default" do
41
- info({ :page => 2, :per_page => 5 }, :html => nil).should ==
67
+ info({ :page => 2, :per_page => 5 }, :html => true).should ==
42
68
  "Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total"
43
69
  end
44
70
 
@@ -48,7 +74,8 @@ describe WillPaginate::ViewHelpers::Base do
48
74
 
49
75
  it "should handle longer class names" do
50
76
  collection = @array.paginate(:page => 2, :per_page => 5)
51
- collection.first.stubs(:class).returns(mock('Class', :name => 'ProjectType'))
77
+ model = stub('Class', :name => 'ProjectType', :to_s => 'ProjectType')
78
+ collection.first.stubs(:class).returns(model)
52
79
  info(collection).should include_phrase('project types')
53
80
  end
54
81
 
@@ -60,5 +87,30 @@ describe WillPaginate::ViewHelpers::Base do
60
87
  it "should display 'no entries found' for empty collections" do
61
88
  info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
62
89
  end
90
+
91
+ it "uses model_name.human when available" do
92
+ name = stub('model name', :i18n_key => :flower_key)
93
+ name.expects(:human).with(:count => 1).returns('flower')
94
+ model = stub('Class', :model_name => name)
95
+ collection = [1].paginate(:page => 1)
96
+
97
+ info(collection, :model => model).should == "Displaying 1 flower"
98
+ end
99
+
100
+ it "uses custom translation instead of model_name.human" do
101
+ name = stub('model name', :i18n_key => :flower_key)
102
+ name.expects(:human).never
103
+ model = stub('Class', :model_name => name)
104
+ translation :will_paginate => {:models => {:flower_key => 'tulip'}}
105
+ collection = [1].paginate(:page => 1)
106
+
107
+ info(collection, :model => model).should == "Displaying 1 tulip"
108
+ end
109
+
110
+ private
111
+
112
+ def translation(data)
113
+ I18n.backend.store_translations(:en, data)
114
+ end
63
115
  end
64
116
  end
@@ -5,36 +5,31 @@ require 'will_paginate/collection'
5
5
  describe WillPaginate::ViewHelpers::LinkRendererBase do
6
6
 
7
7
  before do
8
- @renderer = WillPaginate::ViewHelpers::LinkRendererBase.new
8
+ @renderer = described_class.new
9
9
  end
10
10
 
11
11
  it "should raise error when unprepared" do
12
12
  lambda {
13
- @renderer.send :param_name
13
+ @renderer.pagination
14
14
  }.should raise_error
15
15
  end
16
16
 
17
17
  it "should prepare with collection and options" do
18
- prepare({}, :param_name => 'mypage')
18
+ prepare({})
19
19
  @renderer.send(:current_page).should == 1
20
- @renderer.send(:param_name).should == 'mypage'
21
20
  end
22
21
 
23
22
  it "should have total_pages accessor" do
24
23
  prepare :total_pages => 42
25
- lambda {
26
- @renderer.send(:total_pages).should == 42
27
- }.should_not have_deprecation
24
+ @renderer.send(:total_pages).should == 42
28
25
  end
29
26
 
30
27
  it "should clear old cached values when prepared" do
31
- prepare({ :total_pages => 1 }, :param_name => 'foo')
28
+ prepare(:total_pages => 1)
32
29
  @renderer.send(:total_pages).should == 1
33
- @renderer.send(:param_name).should == 'foo'
34
- # prepare with different object and options:
35
- prepare({ :total_pages => 2 }, :param_name => 'bar')
30
+ # prepare with different object:
31
+ prepare(:total_pages => 2)
36
32
  @renderer.send(:total_pages).should == 2
37
- @renderer.send(:param_name).should == 'bar'
38
33
  end
39
34
 
40
35
  it "should have pagination definition" do