Extended pagination (select page size, per initial, remember selection)

This commit is contained in:
Meutel 2015-07-04 20:10:58 +02:00
parent 3d4d97f66c
commit e5263033dd
10 changed files with 58 additions and 3 deletions

View File

@ -1,4 +1,7 @@
class AuthorsController < ApplicationController class AuthorsController < ApplicationController
include PreferencesHelper
before_action :preferences, only: :index
def show def show
@author = Author.find(params[:id]) @author = Author.find(params[:id])
@ -6,7 +9,8 @@ class AuthorsController < ApplicationController
end end
def index def index
@authors = Author.paginate(page: params[:page]) @authors = Author.where(initial_filter).order(:sort)
.paginate(page: params[:page], per_page: session[:current_per_page])
@title = "Authors" @title = "Authors"
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -1,4 +1,7 @@
class BooksController < ApplicationController class BooksController < ApplicationController
include PreferencesHelper
before_action :preferences, only: :index
def show def show
@book = Book.find(params[:id]) @book = Book.find(params[:id])
@ -6,7 +9,8 @@ class BooksController < ApplicationController
end end
def index def index
@books = Book.paginate(page: params[:page]) @books = Book.where(initial_filter).order(:sort)
.paginate(page: params[:page], per_page: session[:current_per_page])
@title = "Books" @title = "Books"
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -1,4 +1,7 @@
class SeriesController < ApplicationController class SeriesController < ApplicationController
include PreferencesHelper
before_action :preferences, only: :index
def show def show
@serie = Serie.find(params[:id]) @serie = Serie.find(params[:id])
@ -6,7 +9,8 @@ class SeriesController < ApplicationController
end end
def index def index
@series = Serie.paginate(page: params[:page]) @series = Serie.where(initial_filter).order(:sort)
.paginate(page: params[:page], per_page: session[:current_per_page])
@title = "Series" @title = "Series"
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -0,0 +1,16 @@
module PreferencesHelper
# filter data on first letter
def initial_filter
["UPPER(sort) LIKE ?", session[:initial] + "%"] if session[:initial]
end
# update preferences (per_page, initial)
def preferences
session[:current_per_page] = params[:per_page] ? params[:per_page].to_i : (session[:current_per_page] || WillPaginate.per_page)
if params[:initial]
session[:initial] = (session[:initial] == params[:initial] ? nil : params[:initial])
end
end
end

View File

@ -1,3 +1,4 @@
<%= render 'layouts/initials' %>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<table class="table table-striped authors"> <table class="table table-striped authors">
@ -14,3 +15,4 @@
</table> </table>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<%= render 'layouts/perpage' %>

View File

@ -1,3 +1,4 @@
<%= render'layouts/initials' %>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<table class="table table-striped authors"> <table class="table table-striped authors">
@ -25,3 +26,4 @@
</table> </table>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<%= render'layouts/perpage' %>

View File

@ -0,0 +1,10 @@
<% initials = ['0'] + ('A'..'Z').to_a %>
<div class="center">
<ul class="pagination">
<% initials.each do |i| %>
<%= content_tag :li, class: (i == session[:initial] ? "active": nil) do %>
<%= link_to i, url_for(initial: i), remote: true %>
<% end %>
<% end %>
</ul>
</div>

View File

@ -0,0 +1,9 @@
<div class="center">
<ul class="pagination">
<% [10,20,50,100].each do |p| %>
<%= content_tag :li, class: (p == session[:current_per_page] ? "active": nil) do %>
<%= link_to p, url_for(per_page: p), remote: true %>
<% end %>
<% end %>
</ul>
</div>

View File

@ -1,3 +1,4 @@
<%= render'layouts/initials' %>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<table class="table table-striped series"> <table class="table table-striped series">
@ -31,3 +32,4 @@
</table> </table>
<%= will_paginate class: "center" %> <%= will_paginate class: "center" %>
<%= render'layouts/perpage' %>

View File

@ -22,5 +22,7 @@ module BouquinsRor
# Do not swallow errors in after_commit/after_rollback callbacks. # Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true config.active_record.raise_in_transactional_callbacks = true
config.web_console.whitelisted_ips = '10.8.0.0/24'
end end
end end