refactoring preferences/search
This commit is contained in:
parent
e47b68ff0e
commit
f50f520672
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
16
app/models/preference.rb
Normal file
16
app/models/preference.rb
Normal 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
|
@ -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 %>
|
||||
<div class="input-group">
|
||||
<span class="input-group-btn">
|
||||
<%= button_tag "Seach", class: "btn btn-default" %>
|
||||
</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>
|
||||
<% end %>
|
||||
</div>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<div class="center">
|
||||
<ul class="pagination btn-sm">
|
||||
<% initials.each do |i|
|
||||
is_cur = (i == session[:initial])
|
||||
is_cur = (i == @preference.initial)
|
||||
%>
|
||||
<%= content_tag :li, class: (is_cur ? "active": nil) do %>
|
||||
<%= link_to i, url_for(initial: ( is_cur ? "reset" : i)), remote: true %>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<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 %>
|
||||
<%= content_tag :li, class: (p == @preference.per_page ? "active": nil) do %>
|
||||
<%= link_to p, url_for(per_page: p), remote: true %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
11
test/fixtures/preferences.yml
vendored
Normal file
11
test/fixtures/preferences.yml
vendored
Normal 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
|
7
test/models/preference_test.rb
Normal file
7
test/models/preference_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PreferenceTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue
Block a user