module Ddr class Resource < Valkyrie::Resource include Describable include Governable include HasAdminMetadata include HasThumbnail FILE_FIELDS = %i( caption content extracted_text fits_file intermediate_file multires_image streamable_media struct_metadata thumbnail ) FILE_FIELDS.each do |field| # Defines "can_have_?" class method define_singleton_method "can_have_#{field}?" do fields.include?(field) end delegate "can_have_#{field}?", to: :class # Defines "has_?" instance method define_method "has_#{field}?" do send("can_have_#{field}?") && has_file?(field) end end def self.attachable_files @attachable_files ||= FILE_FIELDS.select { |f| fields.include?(f) } end def self.governable? fields.include? :admin_policy_id end def self.captionable? can_have_caption? end def self.can_be_streamable? can_have_streamable_media? end def self.common_model_name name.split('::').last end def self.metadata_fields fields - FILE_FIELDS - reserved_attributes end def self.tableized_name name.tableize end delegate :attachable_files, :common_model_name, :governable?, :captionable?, :can_be_streamable?, :tableized_name, to: :class alias_method :new_record?, :new_record alias_method :resource_model, :internal_resource def title_display return title.first if title.present? return identifier.first if identifier.present? return original_filename if respond_to?(:original_filename) && original_filename.present? "[#{id}]" end def attached_files_having_content attachable_files.select { |f| has_file?(f) } end def has_file?(f) send(f)&.file_identifier.present? end def publishable? false end def has_admin_policy? governable? && admin_policy_id.present? end alias_method :streamable?, :has_streamable_media? alias_method :captioned?, :has_caption? # Convenience method for retrieving values of a metadata term def values(term) self.send(term) end end end