Class: DocumentDistribution
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- DocumentDistribution
- Defined in:
- app/models/document_distribution.rb
Overview
DocumentDistribution
This class represents a distribution of a document, which includes a URL and a distribution type. It belongs to a document and a reference type, and it supports CSV import and export.
Associations:
-
belongs_to :document
-
belongs_to :reference_type
Callbacks:
-
after_save :reindex_document
-
after_destroy :reindex_document
Validations:
-
Validates presence of :friendlier_id, :reference_type_id, :url
-
Validates uniqueness of :url scoped to :friendlier_id and :reference_type_id
Scopes:
-
to_aardvark_distributions: Converts distributions to aardvark format
-
to_csv: Converts distributions to CSV format
Class Method Summary collapse
-
.csv_column_names ⇒ Array<String>
CSV Column Names.
-
.destroy_all(file) ⇒ Boolean
Destroy All.
-
.import(file) ⇒ Boolean
Import.
Instance Method Summary collapse
-
#reindex_document ⇒ Object
Reindex Document.
-
#to_aardvark_distribution ⇒ Hash
To Aardvark Reference.
-
#to_csv ⇒ Array<String>
To CSV.
Class Method Details
.csv_column_names ⇒ Array<String>
CSV Column Names
Returns an array of column names for CSV export.
59 60 61 |
# File 'app/models/document_distribution.rb', line 59 def self.csv_column_names ["friendlier_id", "reference_type", "distribution_url", "label"] end |
.destroy_all(file) ⇒ Boolean
Destroy All
Destroys document distributions based on a CSV file.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/document_distribution.rb', line 106 def self.destroy_all(file) logger.debug("CSV Destroy") ::CSV.foreach(file.path, headers: true) do |row| logger.debug("CSV Row: #{row.to_hash}") if DocumentDistribution.destroy_by( friendlier_id: row.to_hash["friendlier_id"], reference_type_id: ReferenceType.find_by(name: row.to_hash["reference_type"]).id, url: row.to_hash["distribution_url"] ) logger.debug("Destroyed: #{row.to_hash}") else logger.debug("Not Destroyed: #{row.to_hash}") end end true end |
.import(file) ⇒ Boolean
Import
Imports document distributions from a CSV file.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/document_distribution.rb', line 69 def self.import(file) @errors = [] logger.debug("CSV Import") ::CSV.foreach(file.path, headers: true) do |row| logger.debug("CSV Row: #{row.to_hash}") unless Document.exists?(friendlier_id: row.to_hash["friendlier_id"]) logger.debug("Document not found: #{row.to_hash["friendlier_id"]}") @errors << "Document not found: #{row.to_hash["friendlier_id"]}" next end document_distribution = DocumentDistribution.find_or_initialize_by( friendlier_id: row.to_hash["friendlier_id"], reference_type_id: ReferenceType.find_by(name: row.to_hash["reference_type"]).id, url: row.to_hash["distribution_url"] ) logger.debug("Document Distribution: #{document_distribution.inspect}") document_distribution.update!( friendlier_id: row.to_hash["friendlier_id"], reference_type_id: ReferenceType.find_by(name: row.to_hash["reference_type"]).id, url: row.to_hash["distribution_url"], label: row.to_hash["label"] ) end [@errors.empty?, @errors] end |
Instance Method Details
#reindex_document ⇒ Object
Reindex Document
Reindexes the associated document.
152 153 154 |
# File 'app/models/document_distribution.rb', line 152 def reindex_document document.save end |
#to_aardvark_distribution ⇒ Hash
To Aardvark Reference
Converts the document distribution to an aardvark distribution format.
142 143 144 145 146 147 |
# File 'app/models/document_distribution.rb', line 142 def to_aardvark_distribution hash = {} hash[reference_type.reference_uri.to_s] = url hash["label"] = label if reference_type.reference_uri.to_s == "http://schema.org/downloadUrl" hash end |
#to_csv ⇒ Array<String>
To CSV
Converts the document distribution to an array suitable for CSV export.
128 129 130 131 132 133 134 135 |
# File 'app/models/document_distribution.rb', line 128 def to_csv [ friendlier_id, reference_type.name, url, label ] end |