Add pagination via kaminari

This commit is contained in:
Andreas Haller 2015-04-27 23:14:48 +02:00
parent f71c083722
commit 40eeac2021
13 changed files with 57 additions and 5 deletions

View file

@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem 'rails' gem 'rails'
# Use sqlite3 as the database for Active Record # Use sqlite3 as the database for Active Record
gem 'sqlite3' gem 'sqlite3'
gem 'kaminari'
# Use SCSS for stylesheets # Use SCSS for stylesheets
gem 'sass-rails' gem 'sass-rails'
# Use Uglifier as compressor for JavaScript assets # Use Uglifier as compressor for JavaScript assets

View file

@ -81,6 +81,9 @@ GEM
railties (>= 4.2.0) railties (>= 4.2.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.2) json (1.8.2)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
launchy (2.4.3) launchy (2.4.3)
addressable (~> 2.3) addressable (~> 2.3)
loofah (2.0.1) loofah (2.0.1)
@ -188,6 +191,7 @@ DEPENDENCIES
elasticsearch-model elasticsearch-model
foundation-rails foundation-rails
jquery-rails jquery-rails
kaminari
launchy launchy
omniauth omniauth
omniauth-browserid omniauth-browserid

View file

@ -2,10 +2,9 @@ class SearchController < ApplicationController
def index def index
@show_search_result = params[:q].present? @show_search_result = params[:q].present?
@papers = if @show_search_result @papers = if @show_search_result
# TODO: Add pagination Paper.search(params[:q]).page(params[:page]).results
Paper.search(params[:q]).records
else else
Paper.last(30) Paper.order(published_at: :desc).page(params[:page])
end end
end end
end end

View file

@ -0,0 +1,3 @@
li
= link_to_unless(current_page.first?,
raw(t 'views.pagination.first'), url, :remote => remote)

View file

@ -0,0 +1,2 @@
li.unavailable
= link_to raw(t 'views.pagination.truncate'), '#'

View file

@ -0,0 +1,3 @@
li
= link_to_unless(current_page.last?,
raw(t 'views.pagination.last'), url, { :remote => remote })

View file

@ -0,0 +1,4 @@
li
= link_to_unless(current_page.last?,
raw(t 'views.pagination.next'),
url, :rel => 'next', :remote => remote)

View file

@ -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 })

View file

@ -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?

View file

@ -0,0 +1,4 @@
li
= link_to_unless(current_page.first?,
raw(t 'views.pagination.previous'),
url, :rel => 'prev', :remote => remote)

View file

@ -4,7 +4,7 @@ div
.clearfix .clearfix
- if @show_search_result - if @show_search_result
.left .left
| #{@papers.size} Treffer | #{@papers.total} Treffer
.right .right
button.button. TODO: Suche Abbonieren button.button. TODO: Suche Abbonieren
div div
@ -18,3 +18,6 @@ div
small = doc.published_at small = doc.published_at
= link_to doc.url, target: '_blank' do = link_to doc.url, target: '_blank' do
div = doc.name div = doc.name
div
= paginate @papers

View file

@ -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

View file

@ -4,7 +4,7 @@ namespace :import_papers do
task :from_morph => :environment do |t, args| task :from_morph => :environment do |t, args|
require 'open-uri' require 'open-uri'
api_key = ENV['MORPH_API_KEY'] 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}" puts "Download files from #{uri}"
Paper.import_from_json(uri.read) Paper.import_from_json(uri.read)
end end