From 3345aeb6690e960d697e13538b8990161b2efc87 Mon Sep 17 00:00:00 2001 From: alexdbondoc17 Date: Fri, 18 Feb 2022 00:58:24 +0000 Subject: [PATCH] Update searching for `documents` --- .../document_doctrine_show_component.html.erb | 1 + app/controllers/concerns/document_search.rb | 28 +++++++++++++++++++ .../controllers/annotations_controller.js | 7 +---- .../controllers/document_controller.js | 11 +++----- app/reflexes/annotation_reflex.rb | 8 ++++-- app/reflexes/document_reflex.rb | 14 ++++++---- app/views/api/documents/index.json.jbuilder | 12 ++++++-- .../_document_search_results_table.html.erb | 24 ++++++++-------- app/views/shared/_pagination.html.erb | 1 + 9 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 app/controllers/concerns/document_search.rb create mode 100644 app/views/shared/_pagination.html.erb diff --git a/app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb b/app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb index 5c9d420..dfc2acf 100644 --- a/app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb +++ b/app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb @@ -66,6 +66,7 @@
+
diff --git a/app/controllers/concerns/document_search.rb b/app/controllers/concerns/document_search.rb new file mode 100644 index 0000000..230d1a1 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/app/javascript/controllers/annotations_controller.js b/app/javascript/controllers/annotations_controller.js index 7b53731..825023d 100644 --- a/app/javascript/controllers/annotations_controller.js +++ b/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 () { diff --git a/app/javascript/controllers/document_controller.js b/app/javascript/controllers/document_controller.js index 4bf046f..fdab576 100644 --- a/app/javascript/controllers/document_controller.js +++ b/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 () { diff --git a/app/reflexes/annotation_reflex.rb b/app/reflexes/annotation_reflex.rb index 32ff2ae..21ace61 100644 --- a/app/reflexes/annotation_reflex.rb +++ b/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 diff --git a/app/reflexes/document_reflex.rb b/app/reflexes/document_reflex.rb index fe61e68..3d26a11 100644 --- a/app/reflexes/document_reflex.rb +++ b/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 diff --git a/app/views/api/documents/index.json.jbuilder b/app/views/api/documents/index.json.jbuilder index 049d952..7da1397 100644 --- a/app/views/api/documents/index.json.jbuilder +++ b/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 diff --git a/app/views/document/doctrines/_document_search_results_table.html.erb b/app/views/document/doctrines/_document_search_results_table.html.erb index f09ff6d..07a55c3 100644 --- a/app/views/document/doctrines/_document_search_results_table.html.erb +++ b/app/views/document/doctrines/_document_search_results_table.html.erb @@ -1,17 +1,17 @@ <% search_results.each do |search_result| %> - - - + + + <% if opts[:is_case_lists].present? && opts[:doctrine_id].present? %> @@ -19,10 +19,10 @@ <% if opts[:is_citing_document].present? %>
Reference No.
<%= search_result["clean_reference_number"] %> <%= search_result["short_title"] || search_result["title"] %> <%= search_result["doc_date"].present? ? search_result["doc_date"].to_date.strftime("%m/%d/%Y") : search_result["year"] %> <%= search_result.clean_reference_number %> <%= search_result.short_title || search_result.title %> <%= search_result.doc_date.present? ? search_result.doc_date : search_result.year %> - <% 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 %> + 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 - " - 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'] %>" + Add diff --git a/app/views/shared/_pagination.html.erb b/app/views/shared/_pagination.html.erb new file mode 100644 index 0000000..e188fba --- /dev/null +++ b/app/views/shared/_pagination.html.erb @@ -0,0 +1 @@ +
<%= render PaginationComponent.new(data: search_results) %>