From be1925645793dc91edd3c7a3017c4283ca52bf73 Mon Sep 17 00:00:00 2001 From: Lars Henrik Mai Date: Mon, 18 May 2015 22:14:46 +0200 Subject: [PATCH] adds filters and breaks pagination --- app/controllers/search_controller.rb | 33 +++++++++++++++++++++++----- app/views/search/_form.slim | 11 ++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 5549ab6..45f2ab0 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,12 +1,35 @@ class SearchController < ApplicationController def index - @show_search_result = params[:q].present? + @paper_type = params[:paper_type] + @originator = params[:originator] + + @show_search_result = @show_filters = params[:q].present? papers_found = if @show_search_result Paper.search(params[:q]).records else - Paper + Paper.all + end + + if @show_filters + + @paper_types_found = papers_found.map(&:paper_type).uniq + @originators_found = papers_found.map(&:originator).uniq + + if @paper_type.present? + papers_found = papers_found.where(paper_type: @paper_type) + end + + if @originator.present? + papers_found = papers_found.where(originator: @originator) + end + end + + @papers_count = papers_found.page(params[:page]).total_count + @papers = papers_found.page(params[:page]) + # Fixme Pagination + #order(published_at: :desc). + # page(params[:page]) + + end - @papers_count = @show_search_result ? papers_found.results.total : papers_found.count - @papers = papers_found.order(published_at: :desc).page(params[:page]) - end end diff --git a/app/views/search/_form.slim b/app/views/search/_form.slim index d46877b..30eadb5 100644 --- a/app/views/search/_form.slim +++ b/app/views/search/_form.slim @@ -1,2 +1,13 @@ = form_tag(search_path, method: :get) = text_field_tag(:q, params[:q], placeholder: 'Sucheā€¦', autofocus: true) + - if @show_filters + fieldset + legend Ergebnisse filtern + .row + .small-4.columns + = label "paper_type", "Typ" + = select_tag "paper_type", options_for_select(@paper_types_found, @paper_type), include_blank: true, :onchange => "this.form.submit();" + .small-4.columns.end + = label "originator", "Einreicher" + = select_tag "originator", options_for_select(@originators_found, @originator), include_blank: true, :onchange => "this.form.submit();" +