Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created May 13, 2020 14:53
Show Gist options
  • Save elrayle/53d513897698043fd56367507454853c to your computer and use it in GitHub Desktop.
Save elrayle/53d513897698043fd56367507454853c to your computer and use it in GitHub Desktop.
# Generated via
# `rails generate hyrax:work Publication`
# rubocop:disable Metrics/ClassLength
class Publication < ActiveFedora::Base
# This must come after the WorkBehavior because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::WorkBehavior
self.indexer = PublicationIndexer
# Change this to restrict which works can be added as a child.
self.valid_child_concerns = [Release]
validates :title, presence: { message: 'Your work must have a title.' }
self.human_readable_type = 'Publication'
### START esmis publication metadata properties
has_and_belongs_to_many :related_publication, autosave: false, class_name: "Publication", inverse_of: :related_publication_source, predicate: ::RDF::URI.new('http://id.loc.gov/ontologies/bibframe/relatedTo') do |index|
index.as :symbol
end
has_and_belongs_to_many :related_publication_source, autosave: false, class_name: "Publication", inverse_of: :related_publication, predicate: ::RDF::URI.new('http://NEED_URI_FOR/related_publication')
# title inherited from BasicMetadata
# previous title
property :previous_title, predicate: ::RDF::Vocab::DC.replaces do |index|
index.as :stored_searchable, :facetable
end
# authors
property :authors, predicate: ::RDF::Vocab::MARCRelators.aut do |index|
index.as :stored_searchable, :facetable
end
# description inherited from BasicMetadata
# agency identifier (e.g. slug_id, other string assigned by agency), identifier inherited from CC BasicMetadata
validates_with IdentifierValidator
#resource_type inherited from CC BasicMetadata
# System uniq identifier is the URI for the resource
# External identifier: ISSN
property :issn, predicate: ::RDF::Vocab::BIBO.issn, multiple: false do |index|
index.as :stored_searchable
end
# Publishing Agency (get from agency)
# Location of Publication
# Contact name (a person)
property :contact_name, predicate: ::RDF::Vocab::VCARD.hasFN, multiple: false do |index|
index.as :stored_searchable
end
# Contact office (an organization)
property :contact_organization, predicate: ::RDF::Vocab::VCARD.hasOrganizationName, multiple: false do |index|
index.as :stored_searchable
end
# Contact Email
property :contact_email, predicate: ::RDF::Vocab::VCARD.hasEmail, multiple: false do |index|
index.as :stored_searchable
end
# Contact Phone
property :contact_phone, predicate: ::RDF::Vocab::VCARD.hasTelephone, multiple: false do |index|
index.as :stored_searchable
end
# ESMIS Keywords - Should this be a SKOS.concept?
property :keywords, predicate: ::RDF::Vocab::DC.subject do |index|
index.as :stored_searchable, :facetable
end
# ESMIS Keywords (may not implement this)
# property :esmis_subject, predicate: ::RDF::Vocab::DC11.subject do |index|
# index.as :stored_searchable, :facetable
# end
# Frequency of Release
property :frequency, predicate: ::RDF::URI.new('http://rdaregistry.info/Elements/u#P60538'), multiple: false do |index|
index.as :stored_searchable
end
# Status
property :status, predicate: ::RDF::URI.new('http://rdaregistry.info/Elements/u#P60129'), multiple: false do |index|
index.as :stored_searchable, :facetable
end
property :upcoming_releases, predicate: ::RDF::Vocab::DC.available do |index|
index.as :stored_searchable, :facetable
end
# Local namespace for subscribable field
property :subscribable, predicate: ::RDF::URI.new('http://usda.mannlib.cornell.edu/ns#subscribable'), multiple: false do |index|
index.as :stored_sortable, :facetable
end
# can we do something like this for custom fields. Note the local namespace
# property :commodities, predicate: ::RDF::URI.new('http://www.teeal.org/ns#commodity') do |index|
# index.as :stored_searchable, :facetable
# end
### END esmis publication metadata properties
# This must be included at the end, because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::BasicMetadata
### START esmis publication helper methods
def self.find_by_identifier(identifier)
results = Publication.where(identifier: [identifier])
raise ActiveFedora::ObjectNotFoundError, "Publication with identifier '#{identifier}' not found." unless results.count.positive?
results.first
end
# Get the current agency for the publication
# @returns [Collection] the current agency or nil if not assigned
def agency
cols = member_of_collections
agency = nil
return nil if cols.blank?
cols.each do |col|
agency = col if col.agency?
end
agency
end
# Get the current division for the publication
# @returns [Collection] the current division or nil if not assigned
def division
cols = member_of_collections
division = nil
return nil if cols.blank?
cols.each do |col|
division = col if col.division?
end
division
end
# Change the agency for the publication
# @param [Collection] the new agency
def agency=(new_agency)
return if current_agency?(new_agency)
remove_from_current_agency
add_to_agency(new_agency)
end
# Change the division for the publication
# @param [Collection] the new agency
def division=(new_division)
return if current_division?(new_division)
remove_from_current_division
add_to_division(new_division)
end
# rubocop:disable Metrics/CyclomaticComplexity:
# Check if the publication is already in new_agency
# @return [Boolean] true if already in new_agency OR publication is not in an agency and new_agency is also nil; otherwise, false
def current_agency?(new_agency)
old_agency = agency
return true if old_agency.blank? && new_agency.blank?
return false if old_agency.present? && new_agency.blank?
return false if old_agency.blank? && new_agency.present?
old_agency.id == new_agency.id
end
# Check if the publication is already in new_division
# @return [Boolean] true if already in new_division OR publication is not in a division and new_division is also nil; otherwise, false
def current_division?(new_division)
old_division = division
return true if old_division.blank? && new_division.blank?
return false if old_division.present? && new_division.blank?
return false if old_division.blank? && new_division.present?
old_division.id == new_division.id
end
# rubocop:enable Metrics/CyclomaticComplexity:
# Get all releases for this publication
# @returns [Array<Release>] the releases for this publication
def releases
ActiveFedora::Base.where("publication_id_ssi:#{id}").sort_by(&:release_datetime)
end
# Count the number of releases in this publication
# @returns [Integer] the number of releases for this publication
def release_count
releases.count
end
def releases?
release_count.positive?
end
# Get the agency's acronym that this publication is in
# @returns [String] the agency's acronym (e.g. 'AMS', 'WAOB', etc.)
def agency_acronym
return nil if agency.blank?
agency.acronym
end
# Get the divisions's acronym that this publication is in
# @returns [String] the agency's acronym (e.g. 'AMS-CT', 'AMS-LPS', etc.)
def division_acronym
return nil if division.blank?
division.acronym
end
# private
# Remove the publication from it's current agency
def remove_from_current_agency
old_agency = agency
return if old_agency.blank?
member_of_collections.delete old_agency
save!
end
# Remove the publication from it's current division
def remove_from_current_division
old_division = division
return if old_division.blank?
member_of_collections.delete old_division
save!
end
# Add the publication to an agency. NOTE: You must first remove it from the old agency or this will throw an exception.
# Use method `agency=` to change agencies.
# @params [Collection] the new agency for the publication
def add_to_agency(new_agency)
return if new_agency.blank?
raise "Can't assign agency until the publication has been persisted" unless persisted?
new_agency.add_member_objects [id]
save!
end
def add_to_division(new_division)
return if new_division.blank?
raise "Can't assign division until the publication has been persisted" unless persisted?
new_division.add_member_objects [id]
save!
end
### END esmis publication helper methods
end
# rubocop:enable Metrics/ClassLength
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment