Added a per-page parameter to the pagination system and created a UI widget to select different page sizes, including an 'all' option.
Signed-off-by: Jesse Wolfe <[email protected]> --- Local-branch: ticket/next/5142 app/helpers/application_helper.rb | 31 +++++++++++++++++++++++--- app/helpers/paginate_scope_helper.rb | 9 ++++++- app/views/node_classes/index.html.haml | 7 ++--- app/views/node_groups/index.html.haml | 7 ++--- spec/helpers/application_helper_spec.rb | 32 +++++++++++++++++++++++++++- spec/helpers/paginate_scope_helper_spec.rb | 10 ++++++++ 6 files changed, 81 insertions(+), 15 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 41414f5..f24d290 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -101,12 +101,35 @@ module ApplicationHelper # Return HTML with pagination controls for displaying an ActiveRecord +scope+. def pagination_for(scope, more_link=nil) - if scope.respond_to?(:total_pages) && scope.total_pages > 1 - content_tag(:div, :class => 'actionbar') do + content_tag(:div, :class => 'actionbar') do + if scope.respond_to?(:total_pages) && scope.total_pages > 1 [ - more_link ? content_tag(:span, :class => 'pagination') { link_to('More »', more_link) } : will_paginate(scope), - tag(:br, :class=> 'clear') + more_link ? content_tag(:span, :class => 'pagination') { link_to('More »', more_link) } : will_paginate(scope), + content_tag(:div, :class => 'pagination') do + ' | ' + end ] + else + [] + end + + [ + pagination_sizer_for(scope), + tag(:br, :class=> 'clear') + ] + end + end + + def pagination_sizer_for(scope) + return nil if ! scope.first + return nil if ! scope.first.class.respond_to? :per_page + content_tag(:div, :class => 'pagination') do + [content_tag(:span){ "Per page: " }] + + [scope.first.class.per_page, 100, :all].map do |n| + if (params[:per_page] || scope.per_page.to_s) == n.to_s + content_tag(:span, :class => "current"){ n } + else + link_to(n, {:per_page => n}) + end end end end diff --git a/app/helpers/paginate_scope_helper.rb b/app/helpers/paginate_scope_helper.rb index 5760e46..93fcc01 100644 --- a/app/helpers/paginate_scope_helper.rb +++ b/app/helpers/paginate_scope_helper.rb @@ -1,7 +1,12 @@ module PaginateScopeHelper # Return a paginated +scope+. def paginate_scope(scope, opts={}) - opts.reverse_merge!(:page => params[:page]) - return scope.paginate(opts) + if ! params[:per_page] + scope.paginate( opts.reverse_merge(:page => params[:page]) ) + elsif params[:per_page] != "all" + scope.paginate( opts.reverse_merge(:page => params[:page], :per_page => params[:per_page]) ) + else + scope + end end end diff --git a/app/views/node_classes/index.html.haml b/app/views/node_classes/index.html.haml index 1b4b965..2951c85 100644 --- a/app/views/node_classes/index.html.haml +++ b/app/views/node_classes/index.html.haml @@ -22,10 +22,9 @@ -# = check_box_tag "check_all" %td.name = link_to h(node_class.name), node_class - - if @node_classes.total_pages > 1 - %tfoot - %tr - %td= pagination_for @node_classes + %tfoot + %tr + %td= pagination_for @node_classes - else %tr %td diff --git a/app/views/node_groups/index.html.haml b/app/views/node_groups/index.html.haml index 5ed7a87..3d8f134 100644 --- a/app/views/node_groups/index.html.haml +++ b/app/views/node_groups/index.html.haml @@ -22,10 +22,9 @@ -# = check_box_tag "check_all" %td.name = link_to h(node_group.name), node_group - - if @node_groups.total_pages > 1 - %tfoot - %tr - %td= pagination_for @node_groups + %tfoot + %tr + %td= pagination_for @node_groups - else %tr %td diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 25317c8..2d021f9 100755 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -61,7 +61,37 @@ describe ApplicationHelper do context "when not given paginated records" do subject { helper.pagination_for([]) } - it { should be_nil } + it { should have_tag('div.actionbar') } + it { should_not have_tag('a', /Next/) } + end + + describe "when rendering the page size control" do + it "should use the default per_page setting" do + foo = Class.new do + def self.per_page; 51 ; end + end + paginated_results = [foo.new].paginate + helper.pagination_for(paginated_results).should have_tag('a', /51/) + end + + it "should render spans instead of links for the current pagination size" do + foo = Class.new do + def self.per_page; 51 ; end + end + paginated_results = [foo.new].paginate(:per_page => 100) + helper.pagination_for(paginated_results).should have_tag('span', /100/) + end + + it "should render spans instead of links for the current pagination size supplied in the url" do + foo = Class.new do + def self.per_page; 51 ; end + end + params[:per_page] = "all" + paginated_results = [foo.new] + helper.pagination_for(paginated_results).should have_tag('span', /all/) + end + + end end diff --git a/spec/helpers/paginate_scope_helper_spec.rb b/spec/helpers/paginate_scope_helper_spec.rb index c364f45..de2cbc1 100644 --- a/spec/helpers/paginate_scope_helper_spec.rb +++ b/spec/helpers/paginate_scope_helper_spec.rb @@ -5,5 +5,15 @@ describe PaginateScopeHelper do it "should paginate the scope" do helper.paginate_scope([1,2,3]).should be_a_kind_of(WillPaginate::Collection) end + + it "should pass in the per_page parameter" do + params[:per_page] = 2 + helper.paginate_scope([1,2,3]).per_page.should == 2 + end + + it "should not paginate if the per_page parameter is 'all'" do + params[:per_page] = 'all' + helper.paginate_scope([1,2,3]).should_not be_a_kind_of(WillPaginate::Collection) + end end end -- 1.7.0.4 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
