class Cdao::Jurisprudence < Cdao::Base self.table_name = "jurisprudences" has_one :document, as: :doc, class_name: "Jurisprudence", dependent: :destroy has_many :annotations, as: :document, dependent: :restrict_with_error has_many :doctrine_jurisprudences, dependent: :destroy has_many :doctrines, through: :doctrine_jurisprudences 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 libraries Cdao::DocumentLibrary.where(library_product_type: self.class.name.gsub("Cdao::", ""), library_product_id: id).map(&:library) end def is_only_in_premium_libraries? libraries.all?(&:premium?) end def cited_documents Cdao::Citation.where(citing_doc_id: self.id, citing_doc_type: self.class.name.gsub("Cdao::", "")) end def citing_documents Cdao::Citation.where(cited_doc_id: self.id, cited_doc_type: self.class.name.gsub("Cdao::", "")) end def document Cdao::Document.where(doc_id: self.id, doc_type: self.class.name.gsub("Cdao::", "")).first end def self.citing_docs_of(doc, where = {}) citing_docs = [] c_arel = Cdao::Citation.arel_table default_where = c_arel[:citing_doc_type].not_eq(nil).and(c_arel[:citing_doc_id].not_eq(nil)) doc.citing_documents.where(where).where(default_where).group_by(&:citing_doc_type).each do |klass_type, records| next unless klass_type.match?(/Jurisprudence/) klass = "Cdao::#{klass_type}".safe_constantize attr = klass.column_names.map(&:to_sym) - [:content] citing_docs.concat(klass.select(*attr).where(id: records.map(&:citing_doc_id))) end citing_docs end def self.cited_docs_of(doc, where = {}) cited_docs = [] c_arel = Cdao::Citation.arel_table default_where = c_arel[:cited_doc_type].not_eq(nil).and(c_arel[:cited_doc_id].not_eq(nil)) doc.cited_documents.where(where).where(default_where).group_by(&:cited_doc_type).each do |klass_type, records| next unless klass_type.match?(/Jurisprudence/) klass = "Cdao::#{klass_type}".safe_constantize attr = klass.column_names.map(&:to_sym) - [:content] docs = klass.select(*attr).where(id: records.filter_map(&:cited_doc_id)) cited_docs.concat(docs) end cited_docs end def clean_reference_number (reference_number.presence || "").gsub(//, "") end def to_builder(ability = nil) Jbuilder.new do |doc| doc.(self, *%i[id title short_title]) doc.docdate docdate.presence || "0000-00-00" doc.year year.presence || 0 doc.reference_number clean_reference_number doc.display_reference_number clean_reference_number.presence || short_title.presence || title doc.url Rails.application.routes.url_helpers.jurisprudence_path(self) end end def subjects doctrines.map(&:subjects).flatten.uniq end def subject_ids subjects.map(&:id) 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 def phil_rep return "" if citation_finders.blank? citation_finders.where(citation_source_id: 1).map(&:display_name).join(' | ') end def scra return "" if citation_finders.blank? citation_finders.where(citation_source_id: 0).map(&:display_name).join(' | ') end searchable include: [:citation_finders] do text :reference_number, stored: true text :title, stored: true text :short_title, stored: true text :ponente, stored: true string :reference_number string :ponente string :title do (title.present? ? title.first(32760).strip : short_title || "").titleize end string :short_title do (short_title.presence || title.first(32760).strip || "").titleize end string :citation_finders_names, multiple: true do citation_finders_names end date :doc_date date :search_doc_date do doc_date.presence || Date.new(year.presence || 0) end integer :id integer :year integer :search_year do year.present? && year > 0 ? year : (doc_date.try :year) end integer :subject_ids, multiple: true boolean :edited boolean :is_only_in_premium_libraries do is_only_in_premium_libraries? end end end