module Ddr module Describable extend ActiveSupport::Concern def self.term_names term_names = [] vocabularies.each do |vocab| Ddr::Vocab::Vocabulary.property_terms(vocab).each do |term| term_name = Ddr::Vocab::Vocabulary.term_name(vocab, term) unless term_name == :license term_names << term_name end end end term_names end # Dublin Core vocabulary comes from the rdf-vocab gem (https://github.com/ruby-rdf/rdf-vocab) # DDR vocabulary is defined locally def self.vocabularies [RDF::Vocab::DC, Ddr::Vocab::DukeTerms].freeze end def self.indexers # Add term_name => [indexers] mapping to customize indexing {} end def self.default_indexers [:stored_searchable] end def self.indexers_for(term_name) indexers.fetch(term_name, default_indexers) end included do Ddr::Describable.term_names.each do |term_name| if term_name == :available # Intercept dcterms:available and set it to a single date so it can be used for embargoes attribute term_name, Valkyrie::Types::DateTime.optional else attribute term_name, Valkyrie::Types::Set end end end # Used in dul-hydra view def has_desc_metadata? desc_metadata_terms(:present).present? end # Used in dul-hydra ###################### # Commenting out the :required argument since validation is not a model concern in the Valkyrie paradigm. # This will break the `desc_metadata_form_fields` helper in dul-hydra and we'll have to fix it there eventually. ###################### def desc_metadata_terms *args return Ddr::Describable.term_names if args.empty? arg = args.pop terms = case arg.to_sym when :empty desc_metadata_terms.select { |t| values(t).nil? || values(t).empty? } when :present desc_metadata_terms.select { |t| values(t).present? } when :defined_attributes desc_metadata_terms # when :required # desc_metadata_terms(:defined_attributes).select {|t| required? t} when :dcterms Ddr::Vocab::Vocabulary.term_names(RDF::Vocab::DC11) + (Ddr::Vocab::Vocabulary.term_names(RDF::Vocab::DC) - Ddr::Vocab::Vocabulary.term_names(RDF::Vocab::DC11)) when :dcterms_elements11 Ddr::Vocab::Vocabulary.term_names(RDF::Vocab::DC11) when :duke Ddr::Vocab::Vocabulary.term_names(Ddr::Vocab::DukeTerms) else raise ArgumentError, "Invalid argument: #{arg.inspect}" end if args.empty? terms else terms | desc_metadata_terms(*args) end end # Used in dul-hydra helper -- replace there with Ddr::Describable.vocabularies? def desc_metadata_vocabs Ddr::Describable.vocabularies end # Used here and in indexing spec test but can probably be replaced with just .set_value(term, values) def set_desc_metadata_values term, values set_value term, values end # Used in dul-hydra # Update all descMetadata terms with values in hash # Note that term not having key in hash will be set to nil! def set_desc_metadata term_values_hash desc_metadata_terms.each { |t| set_desc_metadata_values(t, term_values_hash[t]) } end end end