From c48fe7321dea4652a18723e11b900ffd35d51f10 Mon Sep 17 00:00:00 2001 From: Angel Aviel Domaoan Date: Mon, 7 Feb 2022 09:25:47 +0000 Subject: [PATCH] Add `doctrine#plain_content` --- Gemfile | 2 + Gemfile.lock | 4 ++ app/models/cdao/citation_finder.rb | 8 ++++ app/models/cdao/jurisprudence.rb | 47 +++++++++++++++++++++ app/models/doctrine.rb | 4 ++ app/views/api/doctrines/index.json.jbuilder | 2 +- app/views/api/doctrines/show.json.jbuilder | 2 +- 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/models/cdao/citation_finder.rb diff --git a/Gemfile b/Gemfile index f6dc169..7f3a4a9 100644 --- a/Gemfile +++ b/Gemfile @@ -122,3 +122,5 @@ group :development do end gem "faker", "~> 2.19" + +gem "sanitize", "~> 6.0" diff --git a/Gemfile.lock b/Gemfile.lock index 3c209be..628dfab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -305,6 +305,9 @@ GEM ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) rubyzip (2.3.2) + sanitize (6.0.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) sassc (2.4.0) @@ -457,6 +460,7 @@ DEPENDENCIES rubocop-packaging rubocop-performance rubocop-rails + sanitize (~> 6.0) sass-rails (>= 6) secure_headers (>= 6.3.0) selenium-webdriver diff --git a/app/models/cdao/citation_finder.rb b/app/models/cdao/citation_finder.rb new file mode 100644 index 0000000..9161f36 --- /dev/null +++ b/app/models/cdao/citation_finder.rb @@ -0,0 +1,8 @@ +class Cdao::CitationFinder < Cdao::Base + self.table_name = "citation_finders" + + TYPES = %w[SCRA PhilRep].freeze + + belongs_to :jurisprudence, class_name: "Cdao::Jurisprudence", optional: false + delegate :title, :content, :ponente, :syllabus, :reference_number, :docdate, to: :jurisprudence +end diff --git a/app/models/cdao/jurisprudence.rb b/app/models/cdao/jurisprudence.rb index 0079c12..eb579d7 100644 --- a/app/models/cdao/jurisprudence.rb +++ b/app/models/cdao/jurisprudence.rb @@ -8,6 +8,8 @@ class Cdao::Jurisprudence < Cdao::Base has_many :annotation_documents, as: :document, dependent: :restrict_with_error has_many :annotations_as_cited, through: :annotation_documents, source: :annotation + has_many :citation_finders, dependent: :restrict_with_error + alias_attribute :doc_date, :docdate def cited_documents @@ -99,4 +101,49 @@ class Cdao::Jurisprudence < Cdao::Base join(:phil_rep, :target => Annotation, :type => :string, :join => { :from => :document_id, :to => :id }) end + + def phil_rep + names = citation_finders_names.keep_if { |a| a.match?("Phil") } + end + + def scra + names = citation_finders_names.keep_if { |a| a.match?(Cdao::CitationFinder::TYPES[0]) } + + names + end + + def citation_finders_names + return [] if citation_finders.blank? + + names = [] + + citation_finders.each do |citation_finder| + volumes = citation_finder.volume.gsub(/\s+/, "").gsub(/\A0*/, "") if citation_finder.volume.present? + + pages = if citation_finder.first_page.present? && citation_finder.last_page.present? + (citation_finder.first_page..citation_finder.last_page).to_a + elsif citation_finder.first_page.present? + [citation_finder.first_page] + elsif citation_finder.last_page.present? + [citation_finder.last_page] + else + [] + end + + types = if citation_finder.citation_source_id.present? + citation_finder.citation_source_id == 0 ? ["SCRA"] : ["Phil"] + else + [] + end + + types.each do |type| + names << ("#{volumes.presence} #{type}").strip + pages.each do |page| + names << ("#{volumes.presence} #{type} #{page}").strip + end + end + end + + names + end end diff --git a/app/models/doctrine.rb b/app/models/doctrine.rb index f7a5efe..c19ef57 100644 --- a/app/models/doctrine.rb +++ b/app/models/doctrine.rb @@ -43,6 +43,10 @@ class Doctrine < ApplicationRecord subject_ids end + def plain_content + Sanitize.clean(content).strip + end + searchable do text :content diff --git a/app/views/api/doctrines/index.json.jbuilder b/app/views/api/doctrines/index.json.jbuilder index 7350ca9..5aca122 100644 --- a/app/views/api/doctrines/index.json.jbuilder +++ b/app/views/api/doctrines/index.json.jbuilder @@ -1,3 +1,3 @@ json.array!(@doctrines) do |doctrine| - json.extract! doctrine, *%i[id content subject_ids created_at updated_at] + json.extract! doctrine, *%i[id subject_ids content plain_content created_at updated_at] end diff --git a/app/views/api/doctrines/show.json.jbuilder b/app/views/api/doctrines/show.json.jbuilder index 5eeb838..3164987 100644 --- a/app/views/api/doctrines/show.json.jbuilder +++ b/app/views/api/doctrines/show.json.jbuilder @@ -1 +1 @@ -json.(@doctrine, *%i[id content subject_ids created_at updated_at]) +json.(@doctrine, *%i[id subject_ids content plain_content created_at updated_at])