diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 888f9c9..02910d2 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -10,7 +10,7 @@ class BooksController < ApplicationController end def index - @books = Book.where(initial_filter).order(:sort) + @books = Book.where(initial_filter).order(sort_col) .paginate(page: params[:page], per_page: session[:current_per_page]) @title = "Books" respond_to do |format| @@ -24,4 +24,12 @@ class BooksController < ApplicationController redirect_to "/calibre/#{url_encode(@book.path)}/cover.jpg" end + private + def sort_col + if session[:sort] == "latest" + return { last_modified: :desc } + end + :sort + end + end diff --git a/app/helpers/preferences_helper.rb b/app/helpers/preferences_helper.rb index 4558b3b..921a8f1 100644 --- a/app/helpers/preferences_helper.rb +++ b/app/helpers/preferences_helper.rb @@ -5,12 +5,13 @@ module PreferencesHelper ["UPPER(sort) LIKE ?", session[:initial] + "%"] if session[:initial] end - # update preferences (per_page, initial) + # update preferences (per_page, initial, sort) 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] = (params[:initial] == "reset" ? nil : params[:initial]) end + session[:sort] = params[:sort] end end diff --git a/app/views/books/_index.html.erb b/app/views/books/_index.html.erb index 7f3a25b..634492d 100644 --- a/app/views/books/_index.html.erb +++ b/app/views/books/_index.html.erb @@ -1,4 +1,13 @@ +
+
+

Filter

+
+
+<% isLatest = session[:sort] == "latest" %> +<%= link_to "Latest", url_for(sort: (isLatest ? "alpha" : "latest")), class: "btn btn-default btn-sm " + (isLatest ? " active" : ""), remote: true %> <%= render'layouts/initials' %> +
+
<%= will_paginate class: "center" %>