You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

100 lines
3.1 KiB

class Cdao::Jurisprudence < Cdao::Base
self.table_name = "jurisprudences"
has_many :annotations, as: :document, dependent: :restrict_with_error
has_many :doctrines, as: :document, dependent: :restrict_with_error
has_many :annotation_documents, as: :document, dependent: :restrict_with_error
has_many :annotations_as_cited, through: :annotation_documents, source: :annotation
alias_attribute :doc_date, :docdate
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 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(/<!--\d+-->/, "")
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
searchable do
text :reference_number, stored: true
text :title, stored: true
text :short_title, stored: true
string :reference_number
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
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
end
end