diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index 1c0ec9f..dd919ce 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -1,4 +1,7 @@ class AuthorsController < ApplicationController + include PreferencesHelper + + before_action :preferences, only: :index def show @author = Author.find(params[:id]) @@ -6,7 +9,8 @@ class AuthorsController < ApplicationController end 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" respond_to do |format| format.html diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 5081477..6a4cb72 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,4 +1,7 @@ class BooksController < ApplicationController + include PreferencesHelper + + before_action :preferences, only: :index def show @book = Book.find(params[:id]) @@ -6,7 +9,8 @@ class BooksController < ApplicationController end 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" respond_to do |format| format.html diff --git a/app/controllers/series_controller.rb b/app/controllers/series_controller.rb index a8bdbf7..7dd91ee 100644 --- a/app/controllers/series_controller.rb +++ b/app/controllers/series_controller.rb @@ -1,4 +1,7 @@ class SeriesController < ApplicationController + include PreferencesHelper + + before_action :preferences, only: :index def show @serie = Serie.find(params[:id]) @@ -6,7 +9,8 @@ class SeriesController < ApplicationController end 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" respond_to do |format| format.html diff --git a/app/helpers/preferences_helper.rb b/app/helpers/preferences_helper.rb new file mode 100644 index 0000000..fc7863e --- /dev/null +++ b/app/helpers/preferences_helper.rb @@ -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 diff --git a/app/views/authors/_index.html.erb b/app/views/authors/_index.html.erb index e1da93b..2033cd3 100644 --- a/app/views/authors/_index.html.erb +++ b/app/views/authors/_index.html.erb @@ -1,3 +1,4 @@ +<%= render 'layouts/initials' %> <%= will_paginate class: "center" %>