Make PaperSearch persistent

This commit is contained in:
Lars Henrik Mai 2015-10-10 09:48:18 +02:00
parent 82c2e7e6b8
commit fce65636f7
3 changed files with 23 additions and 12 deletions

View file

@ -7,16 +7,30 @@ end
class SearchController < ApplicationController class SearchController < ApplicationController
def index def index
@search_definition = PaperSearch.new(params[:paper_search]) @search_definition = params[:paper_search].present? ? PaperSearch.new(search_params) : PaperSearch.new
@search_definition.sort_by ||= "score" @search_definition.sort_by ||= "score"
@response = Paper.search(@search_definition) execute_search
end
def show
@search_definition = PaperSearch.find params[:id]
execute_search
render action: "index"
end
private
def execute_search
@response = Paper.search(@search_definition.to_definition)
@papers = @response.page(params[:page]).results @papers = @response.page(params[:page]).results
@paper_type_facets = extract_facets('paper_types') @paper_type_facets = extract_facets('paper_types')
@originator_facets = extract_facets('originators') @originator_facets = extract_facets('originators')
end end
private def search_params
params.require(:paper_search).permit(:query, :paper_type, :originator, :sort_by)
end
def extract_facets(name) def extract_facets(name)
@response. @response.

View file

@ -1,12 +1,8 @@
class PaperSearch class PaperSearch < ActiveRecord::Base
include ActiveModel::Model def to_definition
options = {paper_type: paper_type, originator: originator, sort_by: sort_by}
attr_accessor :query, :paper_type, :originator, :sort_by PaperSearch.definition(query, options)
def to_hash
options = {paper_type: @paper_type, originator: @originator, sort_by: @sort_by}
PaperSearch.definition(@query, options)
end end
def self.definition(q, options={}) def self.definition(q, options={})

View file

@ -3,6 +3,7 @@ Rails.application.routes.draw do
scope ':body' do scope ':body' do
get '/' => 'search#index', as: :search get '/' => 'search#index', as: :search
get '/:id' => 'search#show', as: :saved_search
end end
post '/auth/:provider/callback', to: 'sessions#create' post '/auth/:provider/callback', to: 'sessions#create'