From 5ea09cf3adbd732343c963bd2bf52e5d4834207e Mon Sep 17 00:00:00 2001 From: alexdbondoc17 Date: Fri, 18 Mar 2022 06:58:09 +0000 Subject: [PATCH] Improve `grouping` for `doctrines#search` --- app/components/doctrine_index_component.rb | 33 +++----- .../doctrine_index_component.html.erb | 78 ++++++++++++------- app/controllers/concerns/doctrine_search.rb | 18 +++++ app/controllers/doctrines_controller.rb | 4 +- app/models/doctrine.rb | 5 ++ app/views/doctrines/search.html.erb | 20 +---- 6 files changed, 90 insertions(+), 68 deletions(-) diff --git a/app/components/doctrine_index_component.rb b/app/components/doctrine_index_component.rb index 2da5b0f..bd6aa0e 100644 --- a/app/components/doctrine_index_component.rb +++ b/app/components/doctrine_index_component.rb @@ -1,22 +1,14 @@ class DoctrineIndexComponent < BaseComponent - with_collection_parameter :doctrine - attr_reader :doctrine, :opts + attr_reader :search_results, :opts include AnnotationSearch + include DoctrineSearch - def initialize(doctrine:, current_user:, opts: {}) - @doctrine = doctrine + def initialize(search_results:, current_user:, opts: {}) + @search_results = search_results @opts = opts end - delegate :id, to: :doctrine - delegate :subject_ids, to: :doctrine - delegate :content, to: :doctrine - delegate :subjects, to: :doctrine - delegate :headnote, to: :doctrine - delegate :doctrine_jurisprudences, to: :doctrine - delegate :jurisprudences, to: :doctrine - def annotation_form_url doctrine_annotations_path(doctrine_id: id) end @@ -25,12 +17,6 @@ class DoctrineIndexComponent < BaseComponent "(No Subjects Provided)" end - def jurisprudence - return nil if doctrine_jurisprudences.blank? - - jurisprudences.first - end - def document_title(annotation) return annotation.document.short_title if annotation.document.short_title.present? @@ -55,8 +41,8 @@ class DoctrineIndexComponent < BaseComponent annotation.phil_rep.gsub(/(PhilRep|Phil)\.?,?/i, "Phil") end - def annotations - search = annotation_search(doctrine_id: id) + def search_annotations(search_params) + search = annotation_search(search_params) search.results end @@ -68,7 +54,12 @@ class DoctrineIndexComponent < BaseComponent contents.join(" citing ") end + def search_doctrines(search_params) + search = doctrine_search(search_params) + search + end + def render? - doctrine.present? && doctrine.persisted? + search_results.present? end end diff --git a/app/components/doctrine_index_component/doctrine_index_component.html.erb b/app/components/doctrine_index_component/doctrine_index_component.html.erb index 8039be7..ac420b9 100644 --- a/app/components/doctrine_index_component/doctrine_index_component.html.erb +++ b/app/components/doctrine_index_component/doctrine_index_component.html.erb @@ -1,32 +1,56 @@ -
- <% document_title = jurisprudence.short_title || jurisprudence.title %> - <% date_or_year = jurisprudence.doc_date.present? ? jurisprudence.doc_date.strftime("%B %d, %Y") : jurisprudence.year %> +
+
+ <% search_results.uniq(&:headnote).each do |uniq_by_headnote| %> +
<%= uniq_by_headnote.headnote %>
- - <% annotations.each do |annotation| %> - <% annotated_documents_title = [] %> - <% annotation.documents.each do |annotated_document| %> - <% ad_title = annotated_document.short_title || annotated_document.title %> - <% ad_date_or_year = annotated_document.doc_date.present? ? annotated_document.doc_date.strftime("%B %d, %Y") : annotated_document.year %> - <% annotated_documents_title << [" citing #{ad_title}", annotated_document.clean_reference_number, ad_date_or_year].join(", ") %> - <% end %> - <% citing_document_title = [document_title, annotation.document.clean_reference_number, date_or_year, clean_phil_rep(annotation), annotated_documents_title].reject(&:blank?).join(', ').html_safe %> + <% grouped_by_content_search_params = { subject_ids: opts[:subject_ids], headnote: uniq_by_headnote.headnote, is_grouped_by_content: true } %> + <% search_doctrines(grouped_by_content_search_params).group(:content).groups.each do |group_by_content| %> + <% group_by_content.results.each do |content_result| %> +
<%= raw content_result.content.html_safe %>
+ + <% grouped_by_juris_search_params = { subject_ids: opts[:subject_ids], content: content_result.content, is_grouped_by_juris_id: true } %> + <% search_doctrines(grouped_by_juris_search_params).group(:jurisprudence_id).groups.each do |grouped_by_juris| %> + <% grouped_by_juris.results.each do |juris_result| %> + + + <% search_params = { subject_ids: opts[:subject_ids], headnote: uniq_by_headnote.headnote, content: content_result.content, jurisprudence_id: juris_result.jurisprudence.id } %> + <% search_doctrines(search_params).results.each do |doctrine| %> + <% search_annotations({ doctrine_id: doctrine.id }).each do |annotation| %> + <% annotated_documents_title = [] %> + <% annotation.documents.each do |annotated_document| %> + <% ad_title = annotated_document.short_title || annotated_document.title %> + <% ad_date_or_year = annotated_document.doc_date.present? ? annotated_document.doc_date.strftime("%B %d, %Y") : annotated_document.year %> + <% annotated_documents_title << [" citing #{ad_title}", annotated_document.clean_reference_number, ad_date_or_year].join(", ") %> + <% end %> + <% citing_document_title = [document_title, annotation.document.clean_reference_number, date_or_year, clean_phil_rep(annotation), annotated_documents_title].reject(&:blank?).join(', ').html_safe %> -
-
<%= "#{annotation.annomarks.map { |annomark| "(#{annomark.code})" }.join(" ")}" %>
-
<%= raw annotation.content.present? ? content_diplay_text(annotation) : citing_document_title %>
-
+
+
<%= "#{annotation.annomarks.map { |annomark| "(#{annomark.code})" }.join(" ")}" %>
+
<%= raw annotation.content.present? ? content_diplay_text(annotation) : citing_document_title %>
+
- <% if annotation.editor_notes.present? %> -
-
- Editors Note: -
- -
- <%= raw annotation.editor_notes %> -
-
+ <% if annotation.editor_notes.present? %> +
+
+ Editors Note: +
+ +
+ <%= raw annotation.editor_notes %> +
+
+ <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> <% end %> - <% end %> +
+
diff --git a/app/controllers/concerns/doctrine_search.rb b/app/controllers/concerns/doctrine_search.rb index ea3b9f4..641133f 100644 --- a/app/controllers/concerns/doctrine_search.rb +++ b/app/controllers/concerns/doctrine_search.rb @@ -9,6 +9,12 @@ module DoctrineSearch with(:user_ids).any_of(search_params[:user_ids].map(&:to_i)) if search_params[:user_ids].present? + with(:jurisprudence_id, search_params[:jurisprudence_id]) if search_params[:jurisprudence_id].present? + + %i[headnote content].each do |field| + with(field, search_params[field]) if search_params[field].present? + end + if search_params[:created_at].present? with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date) end @@ -23,6 +29,18 @@ module DoctrineSearch without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present? + if search_params[:is_grouped_by_headnote].to_s.eql?("true") + group :headnote + end + + if search_params[:is_grouped_by_content].to_s.eql?("true") + group :content + end + + if search_params[:is_grouped_by_juris_id].to_s.eql?("true") + group :jurisprudence_id + end + order_by :search_year, :desc order_by :search_doc_date, :desc diff --git a/app/controllers/doctrines_controller.rb b/app/controllers/doctrines_controller.rb index d85366c..75a2968 100644 --- a/app/controllers/doctrines_controller.rb +++ b/app/controllers/doctrines_controller.rb @@ -19,9 +19,7 @@ class DoctrinesController < ApplicationController def search @search = doctrine_search(search_params) - @results = @search.results - - @uniq_by_headnotes = @results.uniq(&:headnote) + @search_results = @search.results respond_to do |format| format.html diff --git a/app/models/doctrine.rb b/app/models/doctrine.rb index e1c9bc2..1cef882 100644 --- a/app/models/doctrine.rb +++ b/app/models/doctrine.rb @@ -181,6 +181,10 @@ class Doctrine < ApplicationRecord integer :id + integer :jurisprudence_id do + jurisprudence.id + end + integer :subject_ids, multiple: true integer :user_ids, multiple: true do @@ -198,5 +202,6 @@ class Doctrine < ApplicationRecord date :created_at string :headnote + string :content end end diff --git a/app/views/doctrines/search.html.erb b/app/views/doctrines/search.html.erb index 5e3c5da..2cbe9ac 100644 --- a/app/views/doctrines/search.html.erb +++ b/app/views/doctrines/search.html.erb @@ -1,20 +1,6 @@
-
<%= render PaginationComponent.new(data: @results, opts: { is_subject_breadcrums: true, subject_ids: params[:subject_ids], is_index_table: params[:is_index_table] }) %>
+
<%= render PaginationComponent.new(data: @search_results, opts: { is_subject_breadcrums: true, subject_ids: params[:subject_ids], is_index_table: params[:is_index_table] }) %>

- <% @uniq_by_headnotes.each do |uniq_by_headnote| %> -
-
-
<%= uniq_by_headnote.headnote %>
- - <% uniq_by_contents = @results.map { |doctrine| doctrine if doctrine.headnote.eql?(uniq_by_headnote.headnote) }.uniq(&:content) %> - <% uniq_by_contents.each do |uniq_by_content| %> -
<%= raw uniq_by_content.content.html_safe %>
- <% doctrines = @results.map { |doctrine| doctrine if doctrine.headnote.eql?(uniq_by_headnote.headnote) && doctrine.content.eql?(uniq_by_content.content) } %> - <%= render(DoctrineIndexComponent.with_collection(doctrines, current_user: current_user, opts: { is_index_table: false, subject_ids: params[:subject_ids].map(&:to_i) })) %> - <% end %> -
- -
-
- <% end %> + + <%= render(DoctrineIndexComponent.new(search_results: @search_results, current_user: current_user, opts: { is_index_table: false, subject_ids: params[:subject_ids].map(&:to_i) })) %>