From 40eeac20216edcd7fd4e31926d5d690c29716bbd Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Mon, 27 Apr 2015 23:14:48 +0200 Subject: [PATCH] Add pagination via kaminari --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/controllers/search_controller.rb | 5 ++--- app/views/kaminari/_first_page.html.slim | 3 +++ app/views/kaminari/_gap.html.slim | 2 ++ app/views/kaminari/_last_page.html.slim | 3 +++ app/views/kaminari/_next_page.html.slim | 4 ++++ app/views/kaminari/_page.html.slim | 4 ++++ app/views/kaminari/_paginator.html.slim | 12 ++++++++++++ app/views/kaminari/_prev_page.html.slim | 4 ++++ app/views/search/index.html.slim | 5 ++++- config/initializers/kaminari_config.rb | 13 +++++++++++++ lib/tasks/import_papers.rake | 2 +- 13 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 app/views/kaminari/_first_page.html.slim create mode 100644 app/views/kaminari/_gap.html.slim create mode 100644 app/views/kaminari/_last_page.html.slim create mode 100644 app/views/kaminari/_next_page.html.slim create mode 100644 app/views/kaminari/_page.html.slim create mode 100644 app/views/kaminari/_paginator.html.slim create mode 100644 app/views/kaminari/_prev_page.html.slim create mode 100644 config/initializers/kaminari_config.rb diff --git a/Gemfile b/Gemfile index 16e0264..62b9861 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ source 'https://rubygems.org' gem 'rails' # Use sqlite3 as the database for Active Record gem 'sqlite3' +gem 'kaminari' # Use SCSS for stylesheets gem 'sass-rails' # Use Uglifier as compressor for JavaScript assets diff --git a/Gemfile.lock b/Gemfile.lock index d16bc3c..7f306e6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,9 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.2) + kaminari (0.16.3) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) launchy (2.4.3) addressable (~> 2.3) loofah (2.0.1) @@ -188,6 +191,7 @@ DEPENDENCIES elasticsearch-model foundation-rails jquery-rails + kaminari launchy omniauth omniauth-browserid diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index a66bc48..a15cd75 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -2,10 +2,9 @@ class SearchController < ApplicationController def index @show_search_result = params[:q].present? @papers = if @show_search_result - # TODO: Add pagination - Paper.search(params[:q]).records + Paper.search(params[:q]).page(params[:page]).results else - Paper.last(30) + Paper.order(published_at: :desc).page(params[:page]) end end end diff --git a/app/views/kaminari/_first_page.html.slim b/app/views/kaminari/_first_page.html.slim new file mode 100644 index 0000000..8a6de84 --- /dev/null +++ b/app/views/kaminari/_first_page.html.slim @@ -0,0 +1,3 @@ +li + = link_to_unless(current_page.first?, + raw(t 'views.pagination.first'), url, :remote => remote) diff --git a/app/views/kaminari/_gap.html.slim b/app/views/kaminari/_gap.html.slim new file mode 100644 index 0000000..cf67eac --- /dev/null +++ b/app/views/kaminari/_gap.html.slim @@ -0,0 +1,2 @@ +li.unavailable + = link_to raw(t 'views.pagination.truncate'), '#' diff --git a/app/views/kaminari/_last_page.html.slim b/app/views/kaminari/_last_page.html.slim new file mode 100644 index 0000000..f8fd3f2 --- /dev/null +++ b/app/views/kaminari/_last_page.html.slim @@ -0,0 +1,3 @@ +li + = link_to_unless(current_page.last?, + raw(t 'views.pagination.last'), url, { :remote => remote }) diff --git a/app/views/kaminari/_next_page.html.slim b/app/views/kaminari/_next_page.html.slim new file mode 100644 index 0000000..cbc6e6e --- /dev/null +++ b/app/views/kaminari/_next_page.html.slim @@ -0,0 +1,4 @@ +li + = link_to_unless(current_page.last?, + raw(t 'views.pagination.next'), + url, :rel => 'next', :remote => remote) diff --git a/app/views/kaminari/_page.html.slim b/app/views/kaminari/_page.html.slim new file mode 100644 index 0000000..254f922 --- /dev/null +++ b/app/views/kaminari/_page.html.slim @@ -0,0 +1,4 @@ +li{class="#{'current' if page.current?}"} + = link_to(page, page.current? ? '#' : url, + { :remote => remote, + :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil }) diff --git a/app/views/kaminari/_paginator.html.slim b/app/views/kaminari/_paginator.html.slim new file mode 100644 index 0000000..95af1fe --- /dev/null +++ b/app/views/kaminari/_paginator.html.slim @@ -0,0 +1,12 @@ += paginator.render do + .pagination-centered + ul.pagination + = first_page_tag unless current_page.first? + = prev_page_tag unless current_page.first? + - each_page do |page| + - if page.left_outer? || page.right_outer? || page.inside_window? + = page_tag page + - elsif !page.was_truncated? + = gap_tag + = next_page_tag unless current_page.last? + = last_page_tag unless current_page.last? diff --git a/app/views/kaminari/_prev_page.html.slim b/app/views/kaminari/_prev_page.html.slim new file mode 100644 index 0000000..c4a518d --- /dev/null +++ b/app/views/kaminari/_prev_page.html.slim @@ -0,0 +1,4 @@ +li + = link_to_unless(current_page.first?, + raw(t 'views.pagination.previous'), + url, :rel => 'prev', :remote => remote) diff --git a/app/views/search/index.html.slim b/app/views/search/index.html.slim index 167ef9e..689649e 100644 --- a/app/views/search/index.html.slim +++ b/app/views/search/index.html.slim @@ -4,7 +4,7 @@ div .clearfix - if @show_search_result .left - | #{@papers.size} Treffer + | #{@papers.total} Treffer .right button.button. TODO: Suche Abbonieren div @@ -18,3 +18,6 @@ div small = doc.published_at = link_to doc.url, target: '_blank' do div = doc.name + +div + = paginate @papers diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 0000000..b1799b0 --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,13 @@ +Kaminari.configure do |config| + # config.default_per_page = 25 + # config.max_per_page = nil + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.page_method_name = :page + # config.param_name = :page +end + +Kaminari::Hooks.init +Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari diff --git a/lib/tasks/import_papers.rake b/lib/tasks/import_papers.rake index 7283452..6ba6f13 100644 --- a/lib/tasks/import_papers.rake +++ b/lib/tasks/import_papers.rake @@ -4,7 +4,7 @@ namespace :import_papers do task :from_morph => :environment do |t, args| require 'open-uri' api_key = ENV['MORPH_API_KEY'] - uri = URI.parse "https://api.morph.io/ahx/city_council_leipzig_recent_papers/data.json?key=#{api_key}&query=select%20*%20from%20%27data%27%20limit%2010" + uri = URI.parse "https://api.morph.io/ahx/city_council_leipzig_recent_papers/data.json?key=#{api_key}&query=select%20*%20from%20%27data%27" puts "Download files from #{uri}…" Paper.import_from_json(uri.read) end