hobo_will_paginate 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +18 -0
  3. data/README.md +61 -0
  4. data/Rakefile +25 -0
  5. data/lib/will_paginate.rb +25 -0
  6. data/lib/will_paginate/active_record.rb +216 -0
  7. data/lib/will_paginate/array.rb +57 -0
  8. data/lib/will_paginate/collection.rb +149 -0
  9. data/lib/will_paginate/core_ext.rb +30 -0
  10. data/lib/will_paginate/data_mapper.rb +95 -0
  11. data/lib/will_paginate/deprecation.rb +55 -0
  12. data/lib/will_paginate/i18n.rb +22 -0
  13. data/lib/will_paginate/locale/en.yml +33 -0
  14. data/lib/will_paginate/page_number.rb +57 -0
  15. data/lib/will_paginate/per_page.rb +27 -0
  16. data/lib/will_paginate/railtie.rb +68 -0
  17. data/lib/will_paginate/sequel.rb +39 -0
  18. data/lib/will_paginate/version.rb +9 -0
  19. data/lib/will_paginate/view_helpers.rb +161 -0
  20. data/lib/will_paginate/view_helpers/action_view.rb +148 -0
  21. data/lib/will_paginate/view_helpers/link_renderer.rb +132 -0
  22. data/lib/will_paginate/view_helpers/link_renderer_base.rb +77 -0
  23. data/lib/will_paginate/view_helpers/merb.rb +26 -0
  24. data/lib/will_paginate/view_helpers/sinatra.rb +41 -0
  25. data/spec/ci.rb +29 -0
  26. data/spec/collection_spec.rb +139 -0
  27. data/spec/console +12 -0
  28. data/spec/console_fixtures.rb +28 -0
  29. data/spec/database.yml +22 -0
  30. data/spec/finders/active_record_spec.rb +543 -0
  31. data/spec/finders/activerecord_test_connector.rb +113 -0
  32. data/spec/finders/data_mapper_spec.rb +103 -0
  33. data/spec/finders/data_mapper_test_connector.rb +54 -0
  34. data/spec/finders/sequel_spec.rb +67 -0
  35. data/spec/finders/sequel_test_connector.rb +9 -0
  36. data/spec/fixtures/admin.rb +3 -0
  37. data/spec/fixtures/developer.rb +13 -0
  38. data/spec/fixtures/developers_projects.yml +13 -0
  39. data/spec/fixtures/project.rb +15 -0
  40. data/spec/fixtures/projects.yml +6 -0
  41. data/spec/fixtures/replies.yml +29 -0
  42. data/spec/fixtures/reply.rb +9 -0
  43. data/spec/fixtures/schema.rb +38 -0
  44. data/spec/fixtures/topic.rb +7 -0
  45. data/spec/fixtures/topics.yml +30 -0
  46. data/spec/fixtures/user.rb +2 -0
  47. data/spec/fixtures/users.yml +35 -0
  48. data/spec/page_number_spec.rb +65 -0
  49. data/spec/per_page_spec.rb +41 -0
  50. data/spec/spec_helper.rb +71 -0
  51. data/spec/view_helpers/action_view_spec.rb +423 -0
  52. data/spec/view_helpers/base_spec.rb +130 -0
  53. data/spec/view_helpers/link_renderer_base_spec.rb +87 -0
  54. data/spec/view_helpers/view_example_group.rb +114 -0
  55. metadata +104 -0
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+ require 'will_paginate/view_helpers'
3
+ require 'will_paginate/array'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'active_support/inflections'
7
+
8
+ describe WillPaginate::ViewHelpers do
9
+
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
20
+
21
+ describe "will_paginate" do
22
+ it "should render" do
23
+ collection = WillPaginate::Collection.new(1, 2, 4)
24
+ renderer = mock 'Renderer'
25
+ renderer.expects(:prepare).with(collection, instance_of(Hash), self)
26
+ renderer.expects(:to_html).returns('<PAGES>')
27
+
28
+ will_paginate(collection, :renderer => renderer).should == '<PAGES>'
29
+ end
30
+
31
+ it "should return nil for single-page collections" do
32
+ collection = mock 'Collection', :total_pages => 1
33
+ will_paginate(collection).should be_nil
34
+ end
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
50
+
51
+ describe "page_entries_info" do
52
+ before :all do
53
+ @array = ('a'..'z').to_a
54
+ end
55
+
56
+ def info(params, options = {})
57
+ collection = Hash === params ? @array.paginate(params) : params
58
+ page_entries_info collection, {:html => false}.merge(options)
59
+ end
60
+
61
+ it "should display middle results and total count" do
62
+ info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
63
+ end
64
+
65
+ it "uses translation if available" do
66
+ translation :will_paginate => {
67
+ :page_entries_info => {:multi_page => 'Showing %{from} - %{to}'}
68
+ }
69
+ info(:page => 2, :per_page => 5).should == "Showing 6 - 10"
70
+ end
71
+
72
+ it "uses specific translation if available" do
73
+ translation :will_paginate => {
74
+ :page_entries_info => {:multi_page => 'Showing %{from} - %{to}'},
75
+ :string => { :page_entries_info => {:multi_page => 'Strings %{from} to %{to}'} }
76
+ }
77
+ info(:page => 2, :per_page => 5).should == "Strings 6 to 10"
78
+ end
79
+
80
+ it "should output HTML by default" do
81
+ info({ :page => 2, :per_page => 5 }, :html => true).should ==
82
+ "Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total"
83
+ end
84
+
85
+ it "should display shortened end results" do
86
+ info(:page => 7, :per_page => 4).should include_phrase('strings 25 - 26')
87
+ end
88
+
89
+ it "should handle longer class names" do
90
+ collection = @array.paginate(:page => 2, :per_page => 5)
91
+ model = stub('Class', :name => 'ProjectType', :to_s => 'ProjectType')
92
+ collection.first.stubs(:class).returns(model)
93
+ info(collection).should include_phrase('project types')
94
+ end
95
+
96
+ it "should adjust output for single-page collections" do
97
+ info(('a'..'d').to_a.paginate(:page => 1, :per_page => 5)).should == "Displaying all 4 strings"
98
+ info(['a'].paginate(:page => 1, :per_page => 5)).should == "Displaying 1 string"
99
+ end
100
+
101
+ it "should display 'no entries found' for empty collections" do
102
+ info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
103
+ end
104
+
105
+ it "uses model_name.human when available" do
106
+ name = stub('model name', :i18n_key => :flower_key)
107
+ name.expects(:human).with(:count => 1).returns('flower')
108
+ model = stub('Class', :model_name => name)
109
+ collection = [1].paginate(:page => 1)
110
+
111
+ info(collection, :model => model).should == "Displaying 1 flower"
112
+ end
113
+
114
+ it "uses custom translation instead of model_name.human" do
115
+ name = stub('model name', :i18n_key => :flower_key)
116
+ name.expects(:human).never
117
+ model = stub('Class', :model_name => name)
118
+ translation :will_paginate => {:models => {:flower_key => 'tulip'}}
119
+ collection = [1].paginate(:page => 1)
120
+
121
+ info(collection, :model => model).should == "Displaying 1 tulip"
122
+ end
123
+
124
+ private
125
+
126
+ def translation(data)
127
+ I18n.backend.store_translations(:en, data)
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require 'will_paginate/view_helpers/link_renderer_base'
3
+ require 'will_paginate/collection'
4
+
5
+ describe WillPaginate::ViewHelpers::LinkRendererBase do
6
+
7
+ before do
8
+ @renderer = described_class.new
9
+ end
10
+
11
+ it "should raise error when unprepared" do
12
+ lambda {
13
+ @renderer.pagination
14
+ }.should raise_error
15
+ end
16
+
17
+ it "should prepare with collection and options" do
18
+ prepare({})
19
+ @renderer.send(:current_page).should == 1
20
+ end
21
+
22
+ it "should have total_pages accessor" do
23
+ prepare :total_pages => 42
24
+ @renderer.send(:total_pages).should == 42
25
+ end
26
+
27
+ it "should clear old cached values when prepared" do
28
+ prepare(:total_pages => 1)
29
+ @renderer.send(:total_pages).should == 1
30
+ # prepare with different object:
31
+ prepare(:total_pages => 2)
32
+ @renderer.send(:total_pages).should == 2
33
+ end
34
+
35
+ it "should have pagination definition" do
36
+ prepare({ :total_pages => 1 }, :page_links => true)
37
+ @renderer.pagination.should == [:previous_page, 1, :next_page]
38
+ end
39
+
40
+ describe "visible page numbers" do
41
+ it "should calculate windowed visible links" do
42
+ prepare({ :page => 6, :total_pages => 11 }, :inner_window => 1, :outer_window => 1)
43
+ showing_pages 1, 2, :gap, 5, 6, 7, :gap, 10, 11
44
+ end
45
+
46
+ it "should eliminate small gaps" do
47
+ prepare({ :page => 6, :total_pages => 11 }, :inner_window => 2, :outer_window => 1)
48
+ # pages 4 and 8 appear instead of the gap
49
+ showing_pages 1..11
50
+ end
51
+
52
+ it "should support having no windows at all" do
53
+ prepare({ :page => 4, :total_pages => 7 }, :inner_window => 0, :outer_window => 0)
54
+ showing_pages 1, :gap, 4, :gap, 7
55
+ end
56
+
57
+ it "should adjust upper limit if lower is out of bounds" do
58
+ prepare({ :page => 1, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
59
+ showing_pages 1, 2, 3, 4, 5, :gap, 9, 10
60
+ end
61
+
62
+ it "should adjust lower limit if upper is out of bounds" do
63
+ prepare({ :page => 10, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
64
+ showing_pages 1, 2, :gap, 6, 7, 8, 9, 10
65
+ end
66
+
67
+ def showing_pages(*pages)
68
+ pages = pages.first.to_a if Array === pages.first or Range === pages.first
69
+ @renderer.send(:windowed_page_numbers).should == pages
70
+ end
71
+ end
72
+
73
+ protected
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
+
83
+ def prepare(collection_options, options = {})
84
+ @renderer.prepare(collection(collection_options), options)
85
+ end
86
+
87
+ end
@@ -0,0 +1,114 @@
1
+ require 'active_support'
2
+ require 'action_dispatch/testing/assertions'
3
+ require 'will_paginate/array'
4
+
5
+ module ViewExampleGroup
6
+
7
+ include ActionDispatch::Assertions::SelectorAssertions
8
+
9
+ def assert(value, message)
10
+ raise message unless value
11
+ end
12
+
13
+ def paginate(collection = {}, options = {}, &block)
14
+ if collection.instance_of? Hash
15
+ page_options = { :page => 1, :total_entries => 11, :per_page => 4 }.merge(collection)
16
+ collection = [1].paginate(page_options)
17
+ end
18
+
19
+ locals = { :collection => collection, :options => options }
20
+
21
+ @render_output = render(locals)
22
+ @html_document = nil
23
+
24
+ if block_given?
25
+ classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
26
+ assert_select("div.#{classname}", 1, 'no main DIV', &block)
27
+ end
28
+
29
+ @render_output
30
+ end
31
+
32
+ def html_document
33
+ @html_document ||= HTML::Document.new(@render_output, true, false)
34
+ end
35
+
36
+ def response_from_page_or_rjs
37
+ html_document.root
38
+ end
39
+
40
+ def validate_page_numbers(expected, links, param_name = :page)
41
+ param_pattern = /\W#{Regexp.escape(param_name.to_s)}=([^&]*)/
42
+
43
+ links.map { |el|
44
+ unescape_href(el) =~ param_pattern
45
+ $1 ? $1.to_i : $1
46
+ }.should == expected
47
+ end
48
+
49
+ def assert_links_match(pattern, links = nil, numbers = nil)
50
+ links ||= assert_select 'div.pagination a[href]' do |elements|
51
+ elements
52
+ end
53
+
54
+ pages = [] if numbers
55
+
56
+ links.each do |el|
57
+ href = unescape_href(el)
58
+ href.should =~ pattern
59
+ if numbers
60
+ href =~ pattern
61
+ pages << ($1.nil?? nil : $1.to_i)
62
+ end
63
+ end
64
+
65
+ pages.should == numbers if numbers
66
+ end
67
+
68
+ def assert_no_links_match(pattern)
69
+ assert_select 'div.pagination a[href]' do |elements|
70
+ elements.each do |el|
71
+ unescape_href(el).should_not =~ pattern
72
+ end
73
+ end
74
+ end
75
+
76
+ def unescape_href(el)
77
+ CGI.unescape CGI.unescapeHTML(el['href'])
78
+ end
79
+
80
+ def build_message(message, pattern, *args)
81
+ built_message = pattern.dup
82
+ for value in args
83
+ built_message.sub! '?', value.inspect
84
+ end
85
+ built_message
86
+ end
87
+
88
+ end
89
+
90
+ RSpec.configure do |config|
91
+ config.include ViewExampleGroup, :type => :view, :example_group => {
92
+ :file_path => %r{spec/view_helpers/}
93
+ }
94
+ end
95
+
96
+ module HTML
97
+ Node.class_eval do
98
+ def inner_text
99
+ children.map(&:inner_text).join('')
100
+ end
101
+ end
102
+
103
+ Text.class_eval do
104
+ def inner_text
105
+ self.to_s
106
+ end
107
+ end
108
+
109
+ Tag.class_eval do
110
+ def inner_text
111
+ childless?? '' : super
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobo_will_paginate
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mislav Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: will_paginate provides a simple API for performing paginated queries
14
+ with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination
15
+ links in Rails, Sinatra and Merb web apps.
16
+ email: mislav.marohnic@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ - LICENSE
22
+ files:
23
+ - Rakefile
24
+ - lib/will_paginate/version.rb
25
+ - lib/will_paginate/page_number.rb
26
+ - lib/will_paginate/railtie.rb
27
+ - lib/will_paginate/i18n.rb
28
+ - lib/will_paginate/view_helpers.rb
29
+ - lib/will_paginate/array.rb
30
+ - lib/will_paginate/locale/en.yml
31
+ - lib/will_paginate/deprecation.rb
32
+ - lib/will_paginate/active_record.rb
33
+ - lib/will_paginate/core_ext.rb
34
+ - lib/will_paginate/per_page.rb
35
+ - lib/will_paginate/view_helpers/sinatra.rb
36
+ - lib/will_paginate/view_helpers/link_renderer_base.rb
37
+ - lib/will_paginate/view_helpers/link_renderer.rb
38
+ - lib/will_paginate/view_helpers/merb.rb
39
+ - lib/will_paginate/view_helpers/action_view.rb
40
+ - lib/will_paginate/data_mapper.rb
41
+ - lib/will_paginate/collection.rb
42
+ - lib/will_paginate/sequel.rb
43
+ - lib/will_paginate.rb
44
+ - spec/fixtures/topic.rb
45
+ - spec/fixtures/project.rb
46
+ - spec/fixtures/projects.yml
47
+ - spec/fixtures/developer.rb
48
+ - spec/fixtures/topics.yml
49
+ - spec/fixtures/replies.yml
50
+ - spec/fixtures/user.rb
51
+ - spec/fixtures/reply.rb
52
+ - spec/fixtures/schema.rb
53
+ - spec/fixtures/users.yml
54
+ - spec/fixtures/developers_projects.yml
55
+ - spec/fixtures/admin.rb
56
+ - spec/ci.rb
57
+ - spec/console
58
+ - spec/page_number_spec.rb
59
+ - spec/per_page_spec.rb
60
+ - spec/collection_spec.rb
61
+ - spec/spec_helper.rb
62
+ - spec/view_helpers/view_example_group.rb
63
+ - spec/view_helpers/base_spec.rb
64
+ - spec/view_helpers/action_view_spec.rb
65
+ - spec/view_helpers/link_renderer_base_spec.rb
66
+ - spec/console_fixtures.rb
67
+ - spec/finders/sequel_test_connector.rb
68
+ - spec/finders/activerecord_test_connector.rb
69
+ - spec/finders/sequel_spec.rb
70
+ - spec/finders/data_mapper_test_connector.rb
71
+ - spec/finders/active_record_spec.rb
72
+ - spec/finders/data_mapper_spec.rb
73
+ - spec/database.yml
74
+ - README.md
75
+ - LICENSE
76
+ homepage: https://github.com/mislav/will_paginate/wiki
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --main
82
+ - README.md
83
+ - --charset=UTF-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Pagiation plugin for web frameworks and other apps (customised for the Hobo
102
+ Framework)
103
+ test_files: []
104
+ has_rdoc: