From f50f520672f3c25dede3cc634c37252cb56cd7db Mon Sep 17 00:00:00 2001 From: Meutel Date: Tue, 4 Aug 2015 13:47:26 +0200 Subject: [PATCH] refactoring preferences/search --- app/controllers/authors_controller.rb | 4 ++-- app/controllers/books_controller.rb | 12 +++------- app/controllers/series_controller.rb | 4 ++-- app/helpers/preferences_helper.rb | 33 ++++++++++++++++++++------- app/models/preference.rb | 16 +++++++++++++ app/views/layouts/_filter.html.erb | 4 ++-- app/views/layouts/_initials.html.erb | 2 +- app/views/layouts/_perpage.html.erb | 2 +- test/fixtures/preferences.yml | 11 +++++++++ test/models/preference_test.rb | 7 ++++++ 10 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 app/models/preference.rb create mode 100644 test/fixtures/preferences.yml create mode 100644 test/models/preference_test.rb diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index 098e283..e2c6ce2 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -16,8 +16,8 @@ class AuthorsController < ApplicationController end def index - @authors = Author.where(initial_filter).order(:sort) - .paginate(page: params[:page], per_page: session[:current_per_page]) + @authors = Author.where(query_filter).order(:sort) + .paginate(page: params[:page], per_page: @preference.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 f9fba27..67b076a 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -10,14 +10,8 @@ class BooksController < ApplicationController end def index - filters = initial_filter - if params[:s] - filters[0] << " AND sort like ?" - filters.push("%#{params[:s]}%") - end - puts filters - @books = Book.where(filters).order(sort_col) - .paginate(page: params[:page], per_page: session[:current_per_page]) + @books = Book.where(query_filter).order(sort_col) + .paginate(page: params[:page], per_page: @preference.per_page) @title = "Books" respond_to do |format| format.html @@ -32,7 +26,7 @@ class BooksController < ApplicationController private def sort_col - if session[:sort] == "latest" + if @preference.sort == "latest" return { last_modified: :desc } end :sort diff --git a/app/controllers/series_controller.rb b/app/controllers/series_controller.rb index 7dd91ee..b180f27 100644 --- a/app/controllers/series_controller.rb +++ b/app/controllers/series_controller.rb @@ -9,8 +9,8 @@ class SeriesController < ApplicationController end def index - @series = Serie.where(initial_filter).order(:sort) - .paginate(page: params[:page], per_page: session[:current_per_page]) + @series = Serie.where(query_filter).order(:sort) + .paginate(page: params[:page], per_page: @preference.per_page) @title = "Series" respond_to do |format| format.html diff --git a/app/helpers/preferences_helper.rb b/app/helpers/preferences_helper.rb index 921a8f1..6a00b72 100644 --- a/app/helpers/preferences_helper.rb +++ b/app/helpers/preferences_helper.rb @@ -1,17 +1,34 @@ module PreferencesHelper - # filter data on first letter - def initial_filter - ["UPPER(sort) LIKE ?", session[:initial] + "%"] if session[:initial] - end - # 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) + @preference = Preference.new + puts session[:preference] + @preference.from_json(session[:preference].to_s) if session[:preference] + p @preference if params[:initial] - session[:initial] = (params[:initial] == "reset" ? nil : params[:initial]) + @preference.initial = (params[:initial] == "reset" ? nil : params[:initial]) end - session[:sort] = params[:sort] + @preference.per_page = params[:per_page] ? params[:per_page].to_i : (@preference.per_page || WillPaginate.per_page) + @preference.sort = params[:sort] if params[:sort] + @preference.term = params[:term] if params[:term] + puts @preference.as_json + puts @preference.to_json + session[:preference] = @preference.to_json + end + + def query_filter + conditions = Array.new + params = Array.new + if @preference.initial + conditions.push "UPPER(sort) LIKE ?" + params.push "#{@preference.initial}%" + end + if @preference.term + conditions.push "sort LIKE ?" + params.push "%#{@preference.term}%" + end + [ conditions.join(" AND ") ] + params end end diff --git a/app/models/preference.rb b/app/models/preference.rb new file mode 100644 index 0000000..ced84d4 --- /dev/null +++ b/app/models/preference.rb @@ -0,0 +1,16 @@ +class Preference + include ActiveModel::Serializers::JSON + extend ActiveModel::Naming + + attr_accessor :sort, :term, :per_page, :initial + + def attributes=(hash) + hash.each do |key, value| + send("#{key}=", value) + end + end + + def attributes + instance_values + end +end diff --git a/app/views/layouts/_filter.html.erb b/app/views/layouts/_filter.html.erb index 33f301e..4cd18d8 100644 --- a/app/views/layouts/_filter.html.erb +++ b/app/views/layouts/_filter.html.erb @@ -8,12 +8,12 @@ <%= render'layouts/initials' %> <%= render'layouts/perpage' %> <%= yield(:latest_filter) %> - <%= form_tag(method: :get, remote: true) do %> + <%= form_tag({}, {method: :get, remote: true}) do %>
<%= button_tag "Seach", class: "btn btn-default" %> - <%= text_field_tag(:s, nil, placeholder: "Search for...", class: "form-control") %> + <%= search_field_tag(:term, @preference.term, placeholder: "Search for...", class: "form-control") %>
<% end %> diff --git a/app/views/layouts/_initials.html.erb b/app/views/layouts/_initials.html.erb index cfb6ff6..f5810d6 100644 --- a/app/views/layouts/_initials.html.erb +++ b/app/views/layouts/_initials.html.erb @@ -2,7 +2,7 @@