diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 541a9c6..181ced2 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -15,6 +15,10 @@ class DocumentsController < ApplicationController def show @doctrines = @document.doctrines + + where = { enabled: true, state: "published" } + @cited_in_documents = @document.class.citing_docs_of(document, where) + # @cross_ref_documents = @document.class.cited_docs_of(document, where) end def search diff --git a/app/models/cdao/citation.rb b/app/models/cdao/citation.rb new file mode 100644 index 0000000..ca1947a --- /dev/null +++ b/app/models/cdao/citation.rb @@ -0,0 +1,3 @@ +class Cdao::Citation < Cdao::Base + self.table_name = "citations" +end diff --git a/app/models/cdao/jurisprudence.rb b/app/models/cdao/jurisprudence.rb index daa960b..b9ebcb9 100644 --- a/app/models/cdao/jurisprudence.rb +++ b/app/models/cdao/jurisprudence.rb @@ -10,6 +10,43 @@ class Cdao::Jurisprudence < Cdao::Base 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 subjects doctrines.map(&:subjects).flatten.uniq end