mjfreshyfresh-will_paginate 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/CHANGELOG.rdoc +105 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +125 -0
  4. data/Rakefile +32 -0
  5. data/lib/will_paginate.rb +45 -0
  6. data/lib/will_paginate/array.rb +33 -0
  7. data/lib/will_paginate/collection.rb +145 -0
  8. data/lib/will_paginate/core_ext.rb +69 -0
  9. data/lib/will_paginate/deprecation.rb +50 -0
  10. data/lib/will_paginate/finders.rb +9 -0
  11. data/lib/will_paginate/finders/active_record.rb +192 -0
  12. data/lib/will_paginate/finders/active_record/named_scope.rb +170 -0
  13. data/lib/will_paginate/finders/active_record/named_scope_patch.rb +39 -0
  14. data/lib/will_paginate/finders/active_resource.rb +51 -0
  15. data/lib/will_paginate/finders/base.rb +112 -0
  16. data/lib/will_paginate/finders/data_mapper.rb +30 -0
  17. data/lib/will_paginate/finders/sequel.rb +22 -0
  18. data/lib/will_paginate/version.rb +9 -0
  19. data/lib/will_paginate/view_helpers.rb +42 -0
  20. data/lib/will_paginate/view_helpers/action_view.rb +158 -0
  21. data/lib/will_paginate/view_helpers/base.rb +126 -0
  22. data/lib/will_paginate/view_helpers/link_renderer.rb +130 -0
  23. data/lib/will_paginate/view_helpers/link_renderer_base.rb +83 -0
  24. data/lib/will_paginate/view_helpers/merb.rb +13 -0
  25. data/spec/collection_spec.rb +147 -0
  26. data/spec/console +8 -0
  27. data/spec/console_fixtures.rb +8 -0
  28. data/spec/database.yml +22 -0
  29. data/spec/finders/active_record_spec.rb +461 -0
  30. data/spec/finders/active_resource_spec.rb +52 -0
  31. data/spec/finders/activerecord_test_connector.rb +108 -0
  32. data/spec/finders/data_mapper_spec.rb +62 -0
  33. data/spec/finders/data_mapper_test_connector.rb +20 -0
  34. data/spec/finders/sequel_spec.rb +53 -0
  35. data/spec/finders/sequel_test_connector.rb +9 -0
  36. data/spec/finders_spec.rb +76 -0
  37. data/spec/fixtures/admin.rb +3 -0
  38. data/spec/fixtures/developer.rb +13 -0
  39. data/spec/fixtures/developers_projects.yml +13 -0
  40. data/spec/fixtures/project.rb +15 -0
  41. data/spec/fixtures/projects.yml +6 -0
  42. data/spec/fixtures/replies.yml +29 -0
  43. data/spec/fixtures/reply.rb +7 -0
  44. data/spec/fixtures/schema.rb +38 -0
  45. data/spec/fixtures/topic.rb +6 -0
  46. data/spec/fixtures/topics.yml +30 -0
  47. data/spec/fixtures/user.rb +2 -0
  48. data/spec/fixtures/users.yml +35 -0
  49. data/spec/rcov.opts +2 -0
  50. data/spec/spec.opts +2 -0
  51. data/spec/spec_helper.rb +75 -0
  52. data/spec/tasks.rake +60 -0
  53. data/spec/view_helpers/action_view_spec.rb +344 -0
  54. data/spec/view_helpers/base_spec.rb +64 -0
  55. data/spec/view_helpers/link_renderer_base_spec.rb +84 -0
  56. data/spec/view_helpers/view_example_group.rb +111 -0
  57. metadata +115 -0
@@ -0,0 +1,126 @@
1
+ require 'will_paginate/core_ext'
2
+ require 'will_paginate/view_helpers'
3
+
4
+ module WillPaginate
5
+ module ViewHelpers
6
+ # = The main view helpers module
7
+ #
8
+ # This is the base module which provides the +will_paginate+ view helper.
9
+ module Base
10
+ # Renders Digg/Flickr-style pagination for a WillPaginate::Collection object. Nil is
11
+ # returned if there is only one page in total; pagination links aren't needed in that case.
12
+ #
13
+ # ==== Options
14
+ # * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
15
+ # * <tt>:previous_label</tt> -- default: "« Previous"
16
+ # * <tt>:next_label</tt> -- default: "Next »"
17
+ # * <tt>:inner_window</tt> -- how many links are shown around the current page (default: 4)
18
+ # * <tt>:outer_window</tt> -- how many links are around the first and the last page (default: 1)
19
+ # * <tt>:separator</tt> -- string separator for page HTML elements (default: single space)
20
+ # * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
21
+ # * <tt>:params</tt> -- additional parameters when generating pagination links
22
+ # (eg. <tt>:controller => "foo", :action => nil</tt>)
23
+ # * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default:
24
+ # <tt>WillPaginate::LinkRenderer</tt>)
25
+ # * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
26
+ # * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
27
+ # false only when you are rendering your own pagination markup (default: true)
28
+ # * <tt>:id</tt> -- HTML ID for the container (default: nil). Pass +true+ to have the ID
29
+ # automatically generated from the class name of objects in collection: for example, paginating
30
+ # ArticleComment models would yield an ID of "article_comments_pagination".
31
+ #
32
+ # All options beside listed ones are passed as HTML attributes to the container
33
+ # element for pagination links (the DIV). For example:
34
+ #
35
+ # <%= will_paginate @posts, :id => 'wp_posts' %>
36
+ #
37
+ # ... will result in:
38
+ #
39
+ # <div class="pagination" id="wp_posts"> ... </div>
40
+ #
41
+ def will_paginate(collection, options = {})
42
+ # early exit if there is nothing to render
43
+ return nil unless collection.total_pages > 1
44
+
45
+ options = WillPaginate::ViewHelpers.pagination_options.merge(options)
46
+
47
+ if options[:prev_label]
48
+ WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated.")
49
+ options[:previous_label] = options.delete(:prev_label)
50
+ end
51
+
52
+ # get the renderer instance
53
+ renderer = case options[:renderer]
54
+ when String
55
+ options[:renderer].constantize.new
56
+ when Class
57
+ options[:renderer].new
58
+ else
59
+ options[:renderer]
60
+ end
61
+ # render HTML for pagination
62
+ renderer.prepare collection, options, self
63
+ renderer.to_html
64
+ end
65
+
66
+ # Renders a helpful message with numbers of displayed vs. total entries.
67
+ # You can use this as a blueprint for your own, similar helpers.
68
+ #
69
+ # <%= page_entries_info @posts %>
70
+ # #-> Displaying posts 6 - 10 of 26 in total
71
+ #
72
+ # By default, the message will use the humanized class name of objects
73
+ # in collection: for instance, "project types" for ProjectType models.
74
+ # Override this to your liking with the <tt>:entry_name</tt> parameter:
75
+ #
76
+ # <%= page_entries_info @posts, :entry_name => 'item' %>
77
+ # #-> Displaying items 6 - 10 of 26 in total
78
+ #
79
+ # Entry name is entered in singular and pluralized with
80
+ # <tt>String#pluralize</tt> method from ActiveSupport. If it isn't
81
+ # loaded, specify plural with <tt>:plural_name</tt> parameter:
82
+ #
83
+ # <%= page_entries_info @posts, :entry_name => 'item', :plural_name => 'items' %>
84
+ #
85
+ # By default, this method produces HTML output. You can trigger plain
86
+ # text output by passing <tt>:html => false</tt> in options.
87
+ def page_entries_info(collection, options = {})
88
+ entry_name = options[:entry_name] || (collection.empty?? 'entry' :
89
+ collection.first.class.name.underscore.gsub('_', ' '))
90
+
91
+ plural_name = if options[:plural_name]
92
+ options[:plural_name]
93
+ elsif entry_name == 'entry'
94
+ plural_name = 'entries'
95
+ elsif entry_name.respond_to? :pluralize
96
+ plural_name = entry_name.pluralize
97
+ else
98
+ entry_name + 's'
99
+ end
100
+
101
+ unless options[:html] == false
102
+ b = '<b>'
103
+ eb = '</b>'
104
+ sp = '&nbsp;'
105
+ else
106
+ b = eb = ''
107
+ sp = ' '
108
+ end
109
+
110
+ if collection.total_pages < 2
111
+ case collection.size
112
+ when 0; "No #{plural_name} found"
113
+ when 1; "Displaying #{b}1#{eb} #{entry_name}"
114
+ else; "Displaying #{b}all #{collection.size}#{eb} #{plural_name}"
115
+ end
116
+ else
117
+ %{Displaying #{plural_name} #{b}%d#{sp}-#{sp}%d#{eb} of #{b}%d#{eb} in total} % [
118
+ collection.offset + 1,
119
+ collection.offset + collection.length,
120
+ collection.total_entries
121
+ ]
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,130 @@
1
+ require 'cgi'
2
+ require 'will_paginate/core_ext'
3
+ require 'will_paginate/view_helpers/link_renderer_base'
4
+
5
+ module WillPaginate
6
+ module ViewHelpers
7
+ # This class does the heavy lifting of actually building the pagination
8
+ # links. It is used by +will_paginate+ helper internally.
9
+ class LinkRenderer < LinkRendererBase
10
+
11
+ # * +collection+ is a WillPaginate::Collection instance or any other object
12
+ # that conforms to that API
13
+ # * +options+ are forwarded from +will_paginate+ view helper
14
+ # * +template+ is the reference to the template being rendered
15
+ def prepare(collection, options, template)
16
+ super(collection, options)
17
+ @template = template
18
+ @container_attributes = @base_url_params = nil
19
+ end
20
+
21
+ # Process it! This method returns the complete HTML string which contains
22
+ # pagination links. Feel free to subclass LinkRenderer and change this
23
+ # method as you see fit.
24
+ def to_html
25
+ html = pagination.map do |item|
26
+ item.is_a?(Fixnum) ?
27
+ page_number(item) :
28
+ send(item)
29
+ end.join(@options[:separator])
30
+
31
+ @options[:container] ? html_container(html) : html
32
+ end
33
+
34
+ # Returns the subset of +options+ this instance was initialized with that
35
+ # represent HTML attributes for the container element of pagination links.
36
+ def container_attributes
37
+ @container_attributes ||= begin
38
+ attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class])
39
+ # pagination of Post models will have the ID of "posts_pagination"
40
+ if @options[:container] and @options[:id] === true
41
+ attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
42
+ end
43
+ attributes
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def page_number(page)
50
+ unless page == current_page
51
+ link(page, page, :rel => rel_value(page))
52
+ else
53
+ tag(:em, page)
54
+ end
55
+ end
56
+
57
+ def gap
58
+ '<span class="gap">&hellip;</span>'
59
+ end
60
+
61
+ def previous_page
62
+ previous_or_next_page(@collection.previous_page, @options[:previous_label], 'previous_page')
63
+ end
64
+
65
+ def next_page
66
+ previous_or_next_page(@collection.next_page, @options[:next_label], 'next_page')
67
+ end
68
+
69
+ def previous_or_next_page(page, text, classname)
70
+ if page
71
+ link(text, page, :class => classname)
72
+ else
73
+ tag(:span, text, :class => classname + ' disabled')
74
+ end
75
+ end
76
+
77
+ def html_container(html)
78
+ tag(:div, html, container_attributes)
79
+ end
80
+
81
+ # Returns URL params for +page_link_or_span+, taking the current GET params
82
+ # and <tt>:params</tt> option into account.
83
+ def url(page)
84
+ raise NotImplementedError
85
+ end
86
+
87
+ private
88
+
89
+ def link(text, target, attributes = {})
90
+ if target.is_a? Fixnum
91
+ attributes[:rel] = rel_value(target)
92
+ target = url(target)
93
+ end
94
+ attributes[:href] = target
95
+ tag(:a, text, attributes)
96
+ end
97
+
98
+ def tag(name, value, attributes = {})
99
+ string_attributes = attributes.inject('') do |attrs, pair|
100
+ unless pair.last.nil?
101
+ attrs << %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}")
102
+ end
103
+ attrs
104
+ end
105
+ "<#{name}#{string_attributes}>#{value}</#{name}>"
106
+ end
107
+
108
+ def rel_value(page)
109
+ case page
110
+ when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '')
111
+ when @collection.next_page; 'next'
112
+ when 1; 'start'
113
+ end
114
+ end
115
+
116
+ def symbolized_update(target, other)
117
+ other.each do |key, value|
118
+ key = key.to_sym
119
+ existing = target[key]
120
+
121
+ if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?)
122
+ symbolized_update(existing || (target[key] = {}), value)
123
+ else
124
+ target[key] = value
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,83 @@
1
+ require 'will_paginate/view_helpers'
2
+
3
+ module WillPaginate
4
+ module ViewHelpers
5
+ # This class does the heavy lifting of actually building the pagination
6
+ # links. It is used by +will_paginate+ helper internally.
7
+ class LinkRendererBase
8
+
9
+ # * +collection+ is a WillPaginate::Collection instance or any other object
10
+ # that conforms to that API
11
+ # * +options+ are forwarded from +will_paginate+ view helper
12
+ def prepare(collection, options)
13
+ @collection = collection
14
+ @options = options
15
+
16
+ # reset values in case we're re-using this instance
17
+ @total_pages = @param_name = nil
18
+ end
19
+
20
+ def pagination
21
+ items = @options[:page_links] ? windowed_page_numbers : []
22
+ items.unshift :previous_page
23
+ items.push :next_page
24
+ end
25
+
26
+ protected
27
+
28
+ # Calculates visible page numbers using the <tt>:inner_window</tt> and
29
+ # <tt>:outer_window</tt> options.
30
+ def windowed_page_numbers
31
+ inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i
32
+ window_from = current_page - inner_window
33
+ window_to = current_page + inner_window
34
+
35
+ # adjust lower or upper limit if other is out of bounds
36
+ if window_to > total_pages
37
+ window_from -= window_to - total_pages
38
+ window_to = total_pages
39
+ end
40
+ if window_from < 1
41
+ window_to += 1 - window_from
42
+ window_from = 1
43
+ window_to = total_pages if window_to > total_pages
44
+ end
45
+
46
+ # these are always visible
47
+ middle = window_from..window_to
48
+
49
+ # left window
50
+ if outer_window + 3 < middle.first # there's a gap
51
+ left = (1..(outer_window + 1)).to_a
52
+ left << :gap
53
+ else # runs into visible pages
54
+ left = 1...middle.first
55
+ end
56
+
57
+ # right window
58
+ if total_pages - outer_window - 2 > middle.last # again, gap
59
+ right = ((total_pages - outer_window)..total_pages).to_a
60
+ right.unshift :gap
61
+ else # runs into visible pages
62
+ right = (middle.last + 1)..total_pages
63
+ end
64
+
65
+ left.to_a + middle.to_a + right.to_a
66
+ end
67
+
68
+ private
69
+
70
+ def current_page
71
+ @collection.current_page
72
+ end
73
+
74
+ def total_pages
75
+ @collection.total_pages
76
+ end
77
+
78
+ def param_name
79
+ @param_name ||= @options[:param_name].to_s
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,13 @@
1
+ require 'will_paginate/view_helpers/base'
2
+ require 'will_paginate/view_helpers/link_renderer'
3
+
4
+ WillPaginate::ViewHelpers::LinkRenderer.class_eval do
5
+ protected
6
+
7
+ def url(page)
8
+ params = @template.request.params.except(:action, :controller).merge(param_name => page)
9
+ @template.url(:this, params)
10
+ end
11
+ end
12
+
13
+ Merb::AbstractController.send(:include, WillPaginate::ViewHelpers::Base)
@@ -0,0 +1,147 @@
1
+ require 'will_paginate/array'
2
+ require 'spec_helper'
3
+
4
+ describe WillPaginate::Collection do
5
+
6
+ before :all do
7
+ @simple = ('a'..'e').to_a
8
+ end
9
+
10
+ it "should be a subset of original collection" do
11
+ @simple.paginate(:page => 1, :per_page => 3).should == %w( a b c )
12
+ end
13
+
14
+ it "can be shorter than per_page if on last page" do
15
+ @simple.paginate(:page => 2, :per_page => 3).should == %w( d e )
16
+ end
17
+
18
+ it "should include whole collection if per_page permits" do
19
+ @simple.paginate(:page => 1, :per_page => 5).should == @simple
20
+ end
21
+
22
+ it "should be empty if out of bounds" do
23
+ @simple.paginate(:page => 2, :per_page => 5).should be_empty
24
+ end
25
+
26
+ it "should default to 1 as current page and 30 per-page" do
27
+ result = (1..50).to_a.paginate
28
+ result.current_page.should == 1
29
+ result.size.should == 30
30
+ end
31
+
32
+ describe "old API" do
33
+ it "should fail with numeric params" do
34
+ Proc.new { [].paginate(2) }.should raise_error(ArgumentError)
35
+ Proc.new { [].paginate(2, 10) }.should raise_error(ArgumentError)
36
+ end
37
+
38
+ it "should fail with both options and numeric param" do
39
+ Proc.new { [].paginate({}, 5) }.should raise_error(ArgumentError)
40
+ end
41
+ end
42
+
43
+ it "should give total_entries precedence over actual size" do
44
+ %w(a b c).paginate(:total_entries => 5).total_entries.should == 5
45
+ end
46
+
47
+ it "should be an augmented Array" do
48
+ entries = %w(a b c)
49
+ collection = create(2, 3, 10) do |pager|
50
+ pager.replace(entries).should == entries
51
+ end
52
+
53
+ collection.should == entries
54
+ for method in %w(total_pages each offset size current_page per_page total_entries)
55
+ collection.should respond_to(method)
56
+ end
57
+ collection.should be_kind_of(Array)
58
+ collection.entries.should be_instance_of(Array)
59
+ # TODO: move to another expectation:
60
+ collection.offset.should == 3
61
+ collection.total_pages.should == 4
62
+ collection.should_not be_out_of_bounds
63
+ end
64
+
65
+ describe "previous/next pages" do
66
+ it "should have previous_page nil when on first page" do
67
+ collection = create(1, 1, 3)
68
+ collection.previous_page.should be_nil
69
+ collection.next_page.should == 2
70
+ end
71
+
72
+ it "should have both prev/next pages" do
73
+ collection = create(2, 1, 3)
74
+ collection.previous_page.should == 1
75
+ collection.next_page.should == 3
76
+ end
77
+
78
+ it "should have next_page nil when on last page" do
79
+ collection = create(3, 1, 3)
80
+ collection.previous_page.should == 2
81
+ collection.next_page.should be_nil
82
+ end
83
+ end
84
+
85
+ it "should show out of bounds when page number is too high" do
86
+ create(2, 3, 2).should be_out_of_bounds
87
+ end
88
+
89
+ it "should not show out of bounds when inside collection" do
90
+ create(1, 3, 2).should_not be_out_of_bounds
91
+ end
92
+
93
+ describe "guessing total count" do
94
+ it "can guess when collection is shorter than limit" do
95
+ collection = create { |p| p.replace array }
96
+ collection.total_entries.should == 8
97
+ end
98
+
99
+ it "should allow explicit total count to override guessed" do
100
+ collection = create(2, 5, 10) { |p| p.replace array }
101
+ collection.total_entries.should == 10
102
+ end
103
+
104
+ it "should not be able to guess when collection is same as limit" do
105
+ collection = create { |p| p.replace array(5) }
106
+ collection.total_entries.should be_nil
107
+ end
108
+
109
+ it "should not be able to guess when collection is empty" do
110
+ collection = create { |p| p.replace array(0) }
111
+ collection.total_entries.should be_nil
112
+ end
113
+
114
+ it "should be able to guess when collection is empty and this is the first page" do
115
+ collection = create(1) { |p| p.replace array(0) }
116
+ collection.total_entries.should == 0
117
+ end
118
+ end
119
+
120
+ it "should raise WillPaginate::InvalidPage on invalid input" do
121
+ for bad_input in [0, -1, nil, '', 'Schnitzel']
122
+ Proc.new { create bad_input }.should raise_error(WillPaginate::InvalidPage)
123
+ end
124
+ end
125
+
126
+ it "should raise Argument error on invalid per_page setting" do
127
+ Proc.new { create(1, -1) }.should raise_error(ArgumentError)
128
+ end
129
+
130
+ it "should not respond to page_count anymore" do
131
+ Proc.new { create.page_count }.should raise_error(NoMethodError)
132
+ end
133
+
134
+ private
135
+
136
+ def create(page = 2, limit = 5, total = nil, &block)
137
+ if block_given?
138
+ WillPaginate::Collection.create(page, limit, total, &block)
139
+ else
140
+ WillPaginate::Collection.new(page, limit, total)
141
+ end
142
+ end
143
+
144
+ def array(size = 3)
145
+ Array.new(size)
146
+ end
147
+ end