Browse Source

Update searching for `documents`

pull/40/head
alexdbondoc17 4 years ago
parent
commit
3345aeb669
  1. 1
      app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb
  2. 28
      app/controllers/concerns/document_search.rb
  3. 7
      app/javascript/controllers/annotations_controller.js
  4. 11
      app/javascript/controllers/document_controller.js
  5. 8
      app/reflexes/annotation_reflex.rb
  6. 14
      app/reflexes/document_reflex.rb
  7. 12
      app/views/api/documents/index.json.jbuilder
  8. 24
      app/views/document/doctrines/_document_search_results_table.html.erb
  9. 1
      app/views/shared/_pagination.html.erb

1
app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb

@ -66,6 +66,7 @@
</div>
<div class="row tab">
<div class="col-sm-12default-pagination"> </div>
<table class="table table-striped table-hover mb-0" style="width: 98%; margin: 0 auto;">
<thead>
<th class="bg-light"> Reference No. </th>

28
app/controllers/concerns/document_search.rb

@ -0,0 +1,28 @@
module DocumentSearch
def document_search(search_params)
fulltext_fields = %i[reference_number title short_title].freeze
search = Cdao::Document.search do
fulltext search_params[:q], fields: fulltext_fields, query_phrase_slop: 1 if search_params[:q].present?
fulltext_fields.each do |field|
fulltext search_params[field], fields: [field], query_phrase_slop: 1 if search_params[field].present?
end
with(:year, search_params[:year].to_i) if search_params[:year].present?
without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present?
with(:citation_finders_names).any_of(search_params[:citation_finder]) if search_params[:citation_finder].present?
with(:is_only_in_premium_libraries, false)
order_by :doc_date, :desc
order_by :year, :desc
paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20
end
search
end
end

7
app/javascript/controllers/annotations_controller.js

@ -8,12 +8,7 @@ export default class extends ApplicationController {
}
searchDocument () {
var $this = this
$.get("/api/documents.json", { q: $this.qTarget.value }, function (data, status) {
if (status === "success") {
$this.stimulate("AnnotationReflex#render_document_search_results", data)
}
});
this.stimulate("AnnotationReflex#render_document_search_results", { q: this.qTarget.value })
}
renderForm () {

11
app/javascript/controllers/document_controller.js

@ -155,13 +155,10 @@ export default class extends ApplicationController {
}
search () {
var $this = this
$.get("/api/documents.json", { q: $this.inputTarget.value }, function (data, status) {
if (status === "success") {
$this.stimulate("DocumentReflex#render_index_table", data, $this.element.dataset["documentId"], $this.element.dataset["doctrineId"])
}
});
this.stimulate("DocumentReflex#render_index_table",
{ q: this.inputTarget.value },
this.element.dataset["documentId"],
this.element.dataset["doctrineId"])
}
loadYears () {

8
app/reflexes/annotation_reflex.rb

@ -1,8 +1,12 @@
# frozen_string_literal: true
class AnnotationReflex < ApplicationReflex
def render_document_search_results(results)
include DocumentSearch
def render_document_search_results(search_params)
search = document_search(search_params)
@documents = search.results
opts = { is_citing_document: true }
morph "tbody#documentSearchResultTable", render(partial: "document_search_results_table", locals: { search_results: results, opts: opts })
morph "tbody#documentSearchResultTable", render(partial: "document_search_results_table", locals: { search_results: @documents, opts: opts })
end
end

14
app/reflexes/document_reflex.rb

@ -1,18 +1,22 @@
# frozen_string_literal: true
class DocumentReflex < ApplicationReflex
include JurisprudenceSearch
include DocumentSearch
def render_index_table(results, document_id, doctrine_id)
def render_index_table(search_params, document_id, doctrine_id)
search = document_search(search_params)
@documents = search.results
opts = { is_case_lists: true, doctrine_id: doctrine_id }
morph "tbody#documentIndexTable", render(partial: "document_search_results_table", locals: { search_results: results, opts: opts })
morph "tbody#documentIndexTable", render(partial: "document_search_results_table", locals: { search_results: @documents, opts: opts })
end
def render_years(results)
morph "tbody#yearsIndex", render(DocumentsYearsComponent.new(current_user: current_user, years: results))
end
def render_modal_document_search_table(results)
morph "tbody#modalDocumentSearchTable", render(partial: "document_search_results_table", locals: { search_results: results, opts: { is_citing_document: true } })
def render_modal_document_search_table(search_params)
search = document_search(search_params)
@documents = search.results
morph "tbody#modalDocumentSearchTable", render(partial: "document_search_results_table", locals: { search_results: @documents, opts: { is_citing_document: true } })
end
end

12
app/views/api/documents/index.json.jbuilder

@ -1,3 +1,11 @@
json.array!(@documents) do |document|
json.extract! document, *%i[id reference_number clean_reference_number title short_title doc_date year phil_rep scra]
json.results do
json.array!(@documents) do |document|
json.extract! document, *%i[id reference_number clean_reference_number title short_title doc_date year phil_rep scra]
end
end
json.total_pages @documents.total_pages
json.current_page @documents.current_page
json.previous_page @documents.previous_page
json.next_page @documents.next_page
json.per_page @documents.per_page

24
app/views/document/doctrines/_document_search_results_table.html.erb

@ -1,17 +1,17 @@
<% search_results.each do |search_result| %>
<tr>
<td> <%= search_result["clean_reference_number"] %> </td>
<td> <%= search_result["short_title"] || search_result["title"] %> </td>
<td> <%= search_result["doc_date"].present? ? search_result["doc_date"].to_date.strftime("%m/%d/%Y") : search_result["year"] %> </td>
<td> <%= search_result.clean_reference_number %> </td>
<td> <%= search_result.short_title || search_result.title %> </td>
<td> <%= search_result.doc_date.present? ? search_result.doc_date : search_result.year %> </td>
<% if opts[:is_case_lists].present? && opts[:doctrine_id].present? %>
<td>
<% date_or_year = search_result["doc_date"].present? ? search_result["doc_date"].to_date.strftime("%B %d, %Y") : search_result["year"] %>
<% title = search_result["short_title"].present? ? search_result["short_title"] : search_result["title"] %>
<% date_or_year = search_result.doc_date || search_result.year %>
<% title = search_result.short_title || search_result.title %>
<a class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#annotationModal" data-controller="annotations"
data-doctrine-id="<%= opts[:doctrine_id] %>" data-document-id="<%= search_result['id'] %>" data-document-title="<%= title %>"
data-document-reference-number="<%= search_result['clean_reference_number'] %>" data-document-date-or-year="<%= date_or_year %>"
data-document-phil-rep="<%= search_result['phil_rep'] %>" data-action="click->annotations#renderForm">
data-doctrine-id="<%= opts[:doctrine_id] %>" data-document-id="<%= search_result.id %>" data-document-title="<%= title %>"
data-document-reference-number="<%= search_result.clean_reference_number %>" data-document-date-or-year="<%= date_or_year %>"
data-document-phil-rep="<%= search_result.phil_rep %>" data-action="click->annotations#renderForm">
Add Annotations
</a>
</td>
@ -19,10 +19,10 @@
<% if opts[:is_citing_document].present? %>
<td>
<a class="btn btn-sm btn-primary" data-controller="annotations" data-document-id="<%= search_result["id"] %>"
data-document-reference-number="<%= search_result['clean_reference_number'] %>"
data-document-date="<%= search_result['doc_date'].present? ? search_result['doc_date'].to_date.strftime('%B %d, %Y') : search_result['year'] %>"
data-document-title="<%= search_result['short_title'] || search_result['title'] %>"
<a class="btn btn-sm btn-primary" data-controller="annotations" data-document-id="<%= search_result.id %>"
data-document-reference-number="<%= search_result.clean_reference_number %>"
data-document-date="<%= search_result.doc_date || search_result.year %>"
data-document-title="<%= search_result.short_title || search_result.title %>"
data-action="click->annotations#addCitingDocument" >
Add
</a>

1
app/views/shared/_pagination.html.erb

@ -0,0 +1 @@
<div class="container m-2"> <%= render PaginationComponent.new(data: search_results) %> </div>
Loading…
Cancel
Save