Browse Source

Improve `grouping` for `doctrines#search`

pull/131/head
alexdbondoc17 4 years ago
parent
commit
5ea09cf3ad
  1. 33
      app/components/doctrine_index_component.rb
  2. 78
      app/components/doctrine_index_component/doctrine_index_component.html.erb
  3. 18
      app/controllers/concerns/doctrine_search.rb
  4. 4
      app/controllers/doctrines_controller.rb
  5. 5
      app/models/doctrine.rb
  6. 20
      app/views/doctrines/search.html.erb

33
app/components/doctrine_index_component.rb

@ -1,22 +1,14 @@
class DoctrineIndexComponent < BaseComponent class DoctrineIndexComponent < BaseComponent
with_collection_parameter :doctrine attr_reader :search_results, :opts
attr_reader :doctrine, :opts
include AnnotationSearch include AnnotationSearch
include DoctrineSearch
def initialize(doctrine:, current_user:, opts: {}) def initialize(search_results:, current_user:, opts: {})
@doctrine = doctrine @search_results = search_results
@opts = opts @opts = opts
end 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 def annotation_form_url
doctrine_annotations_path(doctrine_id: id) doctrine_annotations_path(doctrine_id: id)
end end
@ -25,12 +17,6 @@ class DoctrineIndexComponent < BaseComponent
"(No Subjects Provided)" "(No Subjects Provided)"
end end
def jurisprudence
return nil if doctrine_jurisprudences.blank?
jurisprudences.first
end
def document_title(annotation) def document_title(annotation)
return annotation.document.short_title if annotation.document.short_title.present? 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") annotation.phil_rep.gsub(/(PhilRep|Phil)\.?,?/i, "Phil")
end end
def annotations def search_annotations(search_params)
search = annotation_search(doctrine_id: id) search = annotation_search(search_params)
search.results search.results
end end
@ -68,7 +54,12 @@ class DoctrineIndexComponent < BaseComponent
contents.join(" citing ") contents.join(" citing ")
end end
def search_doctrines(search_params)
search = doctrine_search(search_params)
search
end
def render? def render?
doctrine.present? && doctrine.persisted? search_results.present?
end end
end end

78
app/components/doctrine_index_component/doctrine_index_component.html.erb

@ -1,32 +1,56 @@
<div class="container-fluid p-0"> <div class="row-flex m-3 mt-0 doctrine-content-body">
<% document_title = jurisprudence.short_title || jurisprudence.title %> <div class="container-sm <%= params[:is_subjects_index].present? ? 'm-0 ps-0' : '' %>">
<% 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| %>
<h5> <%= uniq_by_headnote.headnote %> </h5>
<h5 class="clickable-link" style="color: darkred;" href="<%= document_path(jurisprudence.document.id, is_index_table: false, subject_ids: params[:subject_ids]) %>"> <%= [document_title, jurisprudence.clean_reference_number, date_or_year].join(", ") %> </h5> <% grouped_by_content_search_params = { subject_ids: opts[:subject_ids], headnote: uniq_by_headnote.headnote, is_grouped_by_content: true } %>
<% annotations.each do |annotation| %> <% search_doctrines(grouped_by_content_search_params).group(:content).groups.each do |group_by_content| %>
<% annotated_documents_title = [] %> <% group_by_content.results.each do |content_result| %>
<% annotation.documents.each do |annotated_document| %> <div class="mb-0"> <%= raw content_result.content.html_safe %> </div>
<% 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 %> <% grouped_by_juris_search_params = { subject_ids: opts[:subject_ids], content: content_result.content, is_grouped_by_juris_id: true } %>
<% annotated_documents_title << [" citing #{ad_title}", annotated_document.clean_reference_number, ad_date_or_year].join(", ") %> <% search_doctrines(grouped_by_juris_search_params).group(:jurisprudence_id).groups.each do |grouped_by_juris| %>
<% end %> <% grouped_by_juris.results.each do |juris_result| %>
<% 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 %> <h5 class="clickable-link" style="color: darkred;" href="<%= document_path(juris_result.jurisprudence.document.id, is_index_table: false, subject_ids: params[:subject_ids]) %>">
<% document_title = juris_result.jurisprudence.short_title || juris_result.jurisprudence.title %>
<% date_or_year = juris_result.jurisprudence.doc_date.present? ? juris_result.jurisprudence.doc_date.strftime("%B, %d, %Y") : doctrine.jurisprudence.year %>
<%= [document_title, juris_result.jurisprudence.clean_reference_number, date_or_year].join(", ") %>
</h5>
<% 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 %>
<div class="row mb-2 pb-0 ms-3"> <div class="row mb-2 pb-0 ms-3">
<div class="col-sm-1 p-0 me-1" style="width: auto;"> <strong> <%= "#{annotation.annomarks.map { |annomark| "(#{annomark.code})" }.join(" ")}" %> </strong> </div> <div class="col-sm-1 p-0 me-1" style="width: auto;"> <strong> <%= "#{annotation.annomarks.map { |annomark| "(#{annomark.code})" }.join(" ")}" %> </strong> </div>
<div class="col-sm-9 p-0 ms-1"> <%= raw annotation.content.present? ? content_diplay_text(annotation) : citing_document_title %> </div> <div class="col-sm-9 p-0 ms-1"> <%= raw annotation.content.present? ? content_diplay_text(annotation) : citing_document_title %> </div>
</div> </div>
<% if annotation.editor_notes.present? %> <% if annotation.editor_notes.present? %>
<div class="row ms-5 mb-2"> <div class="row ms-5 mb-2">
<div class="col-sm-2 p-0" style="width: 105px;"> <div class="col-sm-2 p-0" style="width: 105px;">
<span> Editors Note: </span> <span> Editors Note: </span>
</div> </div>
<div class="col-sm-10 ps-0"> <div class="col-sm-10 ps-0">
<%= raw annotation.editor_notes %> <%= raw annotation.editor_notes %>
</div> </div>
</div> </div>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %> <% end %>
<% end %> </div>
<hr/>
</div> </div>

18
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(: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? if search_params[:created_at].present?
with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date) with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date)
end end
@ -23,6 +29,18 @@ module DoctrineSearch
without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present? 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_year, :desc
order_by :search_doc_date, :desc order_by :search_doc_date, :desc

4
app/controllers/doctrines_controller.rb

@ -19,9 +19,7 @@ class DoctrinesController < ApplicationController
def search def search
@search = doctrine_search(search_params) @search = doctrine_search(search_params)
@results = @search.results @search_results = @search.results
@uniq_by_headnotes = @results.uniq(&:headnote)
respond_to do |format| respond_to do |format|
format.html format.html

5
app/models/doctrine.rb

@ -181,6 +181,10 @@ class Doctrine < ApplicationRecord
integer :id integer :id
integer :jurisprudence_id do
jurisprudence.id
end
integer :subject_ids, multiple: true integer :subject_ids, multiple: true
integer :user_ids, multiple: true do integer :user_ids, multiple: true do
@ -198,5 +202,6 @@ class Doctrine < ApplicationRecord
date :created_at date :created_at
string :headnote string :headnote
string :content
end end
end end

20
app/views/doctrines/search.html.erb

@ -1,20 +1,6 @@
<div class="container-fluid mt-1 p-0 doctrine-index-body"> <div class="container-fluid mt-1 p-0 doctrine-index-body">
<div class="container-fluid m-2 p-0"> <%= render PaginationComponent.new(data: @results, opts: { is_subject_breadcrums: true, subject_ids: params[:subject_ids], is_index_table: params[:is_index_table] }) %> </div> <div class="container-fluid m-2 p-0"> <%= render PaginationComponent.new(data: @search_results, opts: { is_subject_breadcrums: true, subject_ids: params[:subject_ids], is_index_table: params[:is_index_table] }) %> </div>
<hr class="mt-0"/> <hr class="mt-0"/>
<% @uniq_by_headnotes.each do |uniq_by_headnote| %>
<div class="row-flex m-3 mt-0 doctrine-content-body"> <%= render(DoctrineIndexComponent.new(search_results: @search_results, current_user: current_user, opts: { is_index_table: false, subject_ids: params[:subject_ids].map(&:to_i) })) %>
<div class="container-sm <%= params[:is_subjects_index].present? ? 'm-0 ps-0' : '' %>">
<h5> <%= uniq_by_headnote.headnote %> </h5>
<% 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| %>
<div class="mb-0"> <%= raw uniq_by_content.content.html_safe %> </div>
<% 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 %>
</div>
<hr/>
</div>
<% end %>
</div> </div>

Loading…
Cancel
Save