From dffb8d28ed5919128ade70f7aa24dc07b8135cb5 Mon Sep 17 00:00:00 2001 From: Marcin Ciunelis Date: Wed, 19 Oct 2011 14:23:22 +0200 Subject: [PATCH] using kaminari for paging trough notices --- Gemfile | 1 + Gemfile.lock | 3 +++ app/controllers/errs_controller.rb | 2 +- app/views/errs/show.html.haml | 4 +--- app/views/kaminari/notices/_first_page.html.haml | 9 +++++++++ app/views/kaminari/notices/_gap.html.haml | 8 ++++++++ app/views/kaminari/notices/_last_page.html.haml | 9 +++++++++ app/views/kaminari/notices/_next_page.html.haml | 2 ++ app/views/kaminari/notices/_page.html.haml | 10 ++++++++++ app/views/kaminari/notices/_paginator.html.haml | 13 +++++++++++++ app/views/kaminari/notices/_prev_page.html.haml | 2 ++ spec/views/errs/show.html.haml_spec.rb | 2 +- 12 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 app/views/kaminari/notices/_first_page.html.haml create mode 100644 app/views/kaminari/notices/_gap.html.haml create mode 100644 app/views/kaminari/notices/_last_page.html.haml create mode 100644 app/views/kaminari/notices/_next_page.html.haml create mode 100644 app/views/kaminari/notices/_page.html.haml create mode 100644 app/views/kaminari/notices/_paginator.html.haml create mode 100644 app/views/kaminari/notices/_prev_page.html.haml diff --git a/Gemfile b/Gemfile index 6b956d9..df82f81 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem 'hoptoad_notifier', "~> 2.4" gem 'actionmailer_inline_css', "~> 1.3.0" gem 'rpm_contrib' gem 'newrelic_rpm' +gem 'kaminari' platform :ruby do gem 'bson_ext', '~> 1.4.0' diff --git a/Gemfile.lock b/Gemfile.lock index e21ffe2..ab2331c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,8 @@ GEM inherited_resources (1.3.0) has_scope (~> 0.5.0) responders (~> 0.6.0) + kaminari (0.12.4) + rails (>= 3.0.0) libxml-ruby (2.2.2) lighthouse-api (2.0) activeresource (>= 3.0.0) @@ -226,6 +228,7 @@ DEPENDENCIES hoptoad_notifier (~> 2.4) htmlentities (~> 4.3.0) inherited_resources + kaminari lighthouse-api mongoid (= 2.1.2) mongoid_rails_migrations diff --git a/app/controllers/errs_controller.rb b/app/controllers/errs_controller.rb index b941d93..8f02493 100644 --- a/app/controllers/errs_controller.rb +++ b/app/controllers/errs_controller.rb @@ -31,7 +31,7 @@ class ErrsController < ApplicationController def show page = (params[:notice] || @problem.notices_count) page = 1 if page.to_i.zero? - @notices = @problem.notices.paginate(:page => page, :per_page => 1) + @notices = @problem.notices.page(page.to_i).per(1) @notice = @notices.first @comment = Comment.new if request.headers['X-PJAX'] diff --git a/app/views/errs/show.html.haml b/app/views/errs/show.html.haml index 31a319f..0d69934 100644 --- a/app/views/errs/show.html.haml +++ b/app/views/errs/show.html.haml @@ -43,9 +43,7 @@ %h4= @notice.try(:message) -= will_paginate @notices, :param_name => :notice, :page_links => false, :class => 'notice-pagination' -viewing occurrence #{@notices.current_page} of #{@notices.total_pages} -.notice-pagination-loader= image_tag 'loader.gif' += paginate @notices, :param_name => :notice, :theme => :notices .tab-bar %ul diff --git a/app/views/kaminari/notices/_first_page.html.haml b/app/views/kaminari/notices/_first_page.html.haml new file mode 100644 index 0000000..fee8112 --- /dev/null +++ b/app/views/kaminari/notices/_first_page.html.haml @@ -0,0 +1,9 @@ +-# Link to the "First" page +-# available local variables +-# url: url to the first page +-# current_page: a page object for the currently displayed page +-# num_pages: total number of pages +-# per_page: number of items to fetch per page +-# remote: data-remote +%span.first + = link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote diff --git a/app/views/kaminari/notices/_gap.html.haml b/app/views/kaminari/notices/_gap.html.haml new file mode 100644 index 0000000..f82f185 --- /dev/null +++ b/app/views/kaminari/notices/_gap.html.haml @@ -0,0 +1,8 @@ +-# Non-link tag that stands for skipped pages... +-# available local variables +-# current_page: a page object for the currently displayed page +-# num_pages: total number of pages +-# per_page: number of items to fetch per page +-# remote: data-remote +%span.page.gap + = raw(t 'views.pagination.truncate') diff --git a/app/views/kaminari/notices/_last_page.html.haml b/app/views/kaminari/notices/_last_page.html.haml new file mode 100644 index 0000000..6e41d23 --- /dev/null +++ b/app/views/kaminari/notices/_last_page.html.haml @@ -0,0 +1,9 @@ +-# Link to the "Last" page +-# available local variables +-# url: url to the last page +-# current_page: a page object for the currently displayed page +-# num_pages: total number of pages +-# per_page: number of items to fetch per page +-# remote: data-remote +%span.last + = link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} diff --git a/app/views/kaminari/notices/_next_page.html.haml b/app/views/kaminari/notices/_next_page.html.haml new file mode 100644 index 0000000..60c9b69 --- /dev/null +++ b/app/views/kaminari/notices/_next_page.html.haml @@ -0,0 +1,2 @@ +~ link_to_unless current_page.last?, raw('Next →'), url, :class => "next_page", :rel => "next", :remote => remote do |name| + ~ content_tag :span, name, :class => 'next_page disabled' diff --git a/app/views/kaminari/notices/_page.html.haml b/app/views/kaminari/notices/_page.html.haml new file mode 100644 index 0000000..528bba8 --- /dev/null +++ b/app/views/kaminari/notices/_page.html.haml @@ -0,0 +1,10 @@ +-# Link showing page number +-# available local variables +-# page: a page object for "this" page +-# url: url to this page +-# current_page: a page object for the currently displayed page +-# num_pages: total number of pages +-# per_page: number of items to fetch per page +-# remote: data-remote +%span{:class => "page#{' current' if page.current?}"} + = link_to_unless page.current?, page, url, {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil} diff --git a/app/views/kaminari/notices/_paginator.html.haml b/app/views/kaminari/notices/_paginator.html.haml new file mode 100644 index 0000000..96815e1 --- /dev/null +++ b/app/views/kaminari/notices/_paginator.html.haml @@ -0,0 +1,13 @@ +-# The container tag +-# available local variables +-# current_page: a page object for the currently displayed page +-# num_pages: total number of pages +-# per_page: number of items to fetch per page +-# remote: data-remote +-# paginator: the paginator that renders the pagination tags inside += paginator.render do + .notice-pagination< + = prev_page_tag + = next_page_tag +.notice-pagination-loader= image_tag 'loader.gif' +viewing occurrence #{current_page} of #{num_pages} diff --git a/app/views/kaminari/notices/_prev_page.html.haml b/app/views/kaminari/notices/_prev_page.html.haml new file mode 100644 index 0000000..ce4eb4b --- /dev/null +++ b/app/views/kaminari/notices/_prev_page.html.haml @@ -0,0 +1,2 @@ +~ link_to_unless current_page.first?, raw('← Previous'), url, :class => "previous_page", :rel => "prev", :remote => remote do |name| + ~ content_tag :span, name, :class => 'previous_page disabled' diff --git a/spec/views/errs/show.html.haml_spec.rb b/spec/views/errs/show.html.haml_spec.rb index 8f7e54f..23f9bfd 100644 --- a/spec/views/errs/show.html.haml_spec.rb +++ b/spec/views/errs/show.html.haml_spec.rb @@ -8,7 +8,7 @@ describe "errs/show.html.haml" do assign :problem, problem assign :comment, comment assign :app, problem.app - assign :notices, err.notices.paginate(:page => 1, :per_page => 1) + assign :notices, err.notices.page(1).per(1) assign :notice, err.notices.first controller.stub(:current_user) { Factory(:user) } end -- libgit2 0.21.2