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.
66 lines
2.3 KiB
66 lines
2.3 KiB
class ParseLinkableContentJob < ActiveJob::Base |
|
queue_as :default |
|
|
|
def perform(annotation) |
|
content = "" |
|
document = annotation.document |
|
citing_docs = annotation.documents |
|
|
|
if annotation.content.present? |
|
content = annotation.content.gsub("<div>", "").gsub("</div>", "") |
|
|
|
contents = content.split(" citing ") |
|
contents.each_with_index do |content, i| |
|
linkable_content = nil |
|
if i.eql?(0) |
|
linkable_content = add_linkable_content(document.id, [content[i], clean_phil_rep(annotation)].reject(&:blank?).join(", ")) |
|
else |
|
citing_docs.each do |citing_doc| |
|
old_content = contents[i] |
|
next if !old_content.include?(citing_doc.clean_reference_number) |
|
|
|
linkable_content = add_linkable_content(citing_doc.id, old_content) |
|
end |
|
end |
|
|
|
contents[i] = linkable_content |
|
end |
|
|
|
content = contents.join(" citing ") |
|
else |
|
contents = [] |
|
doc_title = document.short_title || document.title |
|
doc_date_or_year = document.doc_date.present? ? document.doc_date.strftime("%B %d, %Y") : document.year |
|
doc_ref_num = document.clean_reference_number |
|
contents << add_linkable_content(document.id, [doc_title, doc_ref_num, doc_date_or_year, clean_phil_rep(annotation)].reject(&:blank?).join(", ")) |
|
|
|
if citing_docs.present? |
|
citing_docs.each do |citing_doc| |
|
citing_doc_title = citing_doc.short_title || citing_doc.title |
|
citing_doc_date_or_year = citing_doc.doc_date.present? ? citing_doc.doc_date.strftime("%B %d, %Y") : citing_doc.year |
|
citing_doc_ref_num = citing_doc.clean_reference_number |
|
|
|
citing_content = [citing_doc_title, citing_doc_date_or_year, citing_doc_ref_num].join(", ") |
|
contents << ["citing", add_linkable_content(citing_doc, citing_content)].join(" ") |
|
end |
|
end |
|
content = contents.join(", ") |
|
end |
|
|
|
annotation.update_column(:content, content) if content.present? |
|
annotation.index! |
|
end |
|
|
|
private |
|
def add_linkable_content(document_id, content) |
|
document_route = Rails.application.routes.url_helpers.document_path(document_id) |
|
|
|
"<a href='#{document_route}'> #{content} </a>" |
|
end |
|
|
|
def clean_phil_rep(annotation) |
|
return if annotation.phil_rep.blank? |
|
|
|
annotation.phil_rep.gsub(/(PhilRep|Phil)\.?,?/i, "Phil") |
|
end |
|
end
|
|
|