refactoring preferences/search

This commit is contained in:
Meutel 2015-08-04 13:47:26 +02:00
parent e47b68ff0e
commit f50f520672
10 changed files with 70 additions and 25 deletions

View File

@ -16,8 +16,8 @@ class AuthorsController < ApplicationController
end end
def index def index
@authors = Author.where(initial_filter).order(:sort) @authors = Author.where(query_filter).order(:sort)
.paginate(page: params[:page], per_page: session[:current_per_page]) .paginate(page: params[:page], per_page: @preference.per_page)
@title = "Authors" @title = "Authors"
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -10,14 +10,8 @@ class BooksController < ApplicationController
end end
def index def index
filters = initial_filter @books = Book.where(query_filter).order(sort_col)
if params[:s] .paginate(page: params[:page], per_page: @preference.per_page)
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])
@title = "Books" @title = "Books"
respond_to do |format| respond_to do |format|
format.html format.html
@ -32,7 +26,7 @@ class BooksController < ApplicationController
private private
def sort_col def sort_col
if session[:sort] == "latest" if @preference.sort == "latest"
return { last_modified: :desc } return { last_modified: :desc }
end end
:sort :sort

View File

@ -9,8 +9,8 @@ class SeriesController < ApplicationController
end end
def index def index
@series = Serie.where(initial_filter).order(:sort) @series = Serie.where(query_filter).order(:sort)
.paginate(page: params[:page], per_page: session[:current_per_page]) .paginate(page: params[:page], per_page: @preference.per_page)
@title = "Series" @title = "Series"
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -1,17 +1,34 @@
module PreferencesHelper 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) # update preferences (per_page, initial, sort)
def preferences 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] if params[:initial]
session[:initial] = (params[:initial] == "reset" ? nil : params[:initial]) @preference.initial = (params[:initial] == "reset" ? nil : params[:initial])
end 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
end end

16
app/models/preference.rb Normal file
View File

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

View File

@ -8,12 +8,12 @@
<%= render'layouts/initials' %> <%= render'layouts/initials' %>
<%= render'layouts/perpage' %> <%= render'layouts/perpage' %>
<%= yield(:latest_filter) %> <%= yield(:latest_filter) %>
<%= form_tag(method: :get, remote: true) do %> <%= form_tag({}, {method: :get, remote: true}) do %>
<div class="input-group"> <div class="input-group">
<span class="input-group-btn"> <span class="input-group-btn">
<%= button_tag "Seach", class: "btn btn-default" %> <%= button_tag "Seach", class: "btn btn-default" %>
</span> </span>
<%= text_field_tag(:s, nil, placeholder: "Search for...", class: "form-control") %> <%= search_field_tag(:term, @preference.term, placeholder: "Search for...", class: "form-control") %>
</div> </div>
<% end %> <% end %>
</div> </div>

View File

@ -2,7 +2,7 @@
<div class="center"> <div class="center">
<ul class="pagination btn-sm"> <ul class="pagination btn-sm">
<% initials.each do |i| <% initials.each do |i|
is_cur = (i == session[:initial]) is_cur = (i == @preference.initial)
%> %>
<%= content_tag :li, class: (is_cur ? "active": nil) do %> <%= content_tag :li, class: (is_cur ? "active": nil) do %>
<%= link_to i, url_for(initial: ( is_cur ? "reset" : i)), remote: true %> <%= link_to i, url_for(initial: ( is_cur ? "reset" : i)), remote: true %>

View File

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

11
test/fixtures/preferences.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View File

@ -0,0 +1,7 @@
require 'test_helper'
class PreferenceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end