From 4abffc7faafcc57a943093b252be83cb38b938c2 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 2 Sep 2019 20:41:10 -0500 Subject: [PATCH] saved searches: paginate index page + add search form. --- app/controllers/saved_searches_controller.rb | 6 +----- app/models/saved_search.rb | 11 +++++++++++ app/views/saved_searches/index.html.erb | 18 +++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/controllers/saved_searches_controller.rb b/app/controllers/saved_searches_controller.rb index b291010e5..87f788c26 100644 --- a/app/controllers/saved_searches_controller.rb +++ b/app/controllers/saved_searches_controller.rb @@ -3,11 +3,7 @@ class SavedSearchesController < ApplicationController respond_to :html, :xml, :json, :js def index - @saved_searches = saved_searches.order("id") - - if params[:label] - @saved_searches = saved_searches.labeled(params[:label]) - end + @saved_searches = saved_searches.search(search_params).paginate(params[:page], limit: params[:limit]) respond_with(@saved_searches) do |format| format.xml do diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index 9c9c06140..5d5fa3a8e 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -93,6 +93,17 @@ class SavedSearch < ApplicationRecord concerning :Search do class_methods do + def search(params) + q = super + q = q.search_attributes(params, :query) + + if params[:label] + q = q.labeled(params[:label]) + end + + q.apply_default_order(params) + end + def populate(query) CurrentUser.as_system do redis_key = "search:#{query}" diff --git a/app/views/saved_searches/index.html.erb b/app/views/saved_searches/index.html.erb index 8979f4d6a..31b0c3cb1 100644 --- a/app/views/saved_searches/index.html.erb +++ b/app/views/saved_searches/index.html.erb @@ -7,19 +7,25 @@ <% end %> - + <%= simple_form_for(:search, url: saved_searches_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.input :query_ilike, label: "Query", input_html: { value: params[:search][:query_ilike] } %> + <%= f.input :label, label: "Label", input_html: { value: params[:search][:label] } %> + <%= f.submit "Search" %> + <% end %> + +
- - - + + + <% @saved_searches.each do |ss| %> - +
QueryLabelsQueryLabels
<%= link_to ss.query, posts_path(:tags => ss.query) %><%= link_to ss.query, posts_path(tags: ss.query) %> <% ss.labels.each do |label| %> <%= link_to label, posts_path(:tags => "search:#{label}") %> @@ -33,6 +39,8 @@ <% end %>
+ + <%= numbered_paginator(@saved_searches) %>