Module: Hydra::ModelMethods

Extended by:
ActiveSupport::Concern
Included in:
ModsAsset
Defined in:
app/models/concerns/hydra/model_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_file(file, path, file_name, mime_type = nil) ⇒ Object

Puts the contents of file (posted blob) into a datastream and sets the title and label Sets asset label and title to filename if they’re empty

Parameters:

  • file (#read)

    the IO object that is the blob

  • file (String)

    the IO object that is the blob



16
17
18
19
20
21
22
# File 'app/models/concerns/hydra/model_methods.rb', line 16

def add_file(file, path, file_name, mime_type=nil)
  mime_type ||= best_mime_for_filename(file_name)
  options = { mime_type: mime_type, original_name: file_name }
  options[:path] = path if path
  super(file, options)
  set_title_and_label(file_name, only_if_blank: true)
end

#best_mime_for_filename(file_name) ⇒ Object



24
25
26
27
# File 'app/models/concerns/hydra/model_methods.rb', line 24

def best_mime_for_filename(file_name)
  mime_types = MIME::Types.of(file_name)
  mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
end

#set_title(new_title, opts = {}) ⇒ Object

Set the title and label on the current object

Parameters:

  • new_title (String)
  • opts (Hash) (defaults to: {})

    (optional) hash of configuration options



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/concerns/hydra/model_methods.rb', line 53

def set_title(new_title, opts={})
  if respond_to? :title=
    self.title = self.class.multiple?(:title) ? Array(new_title) : new_title
  elsif attached_files.has_key?("descMetadata")
    Deprecation.warn ModelMethods, 'setting title in descMetadata is deprecated and will be remove in hydra-head 10.0. If you need this behavior declare `has_attribute :title`'
    if .respond_to?(:title_values)
      .title_values = new_title
    else
      .title = new_title
    end
  end
end

#set_title_and_label(new_title, opts = {}) ⇒ Object

Set the title and label on the current object

Examples:

Use :only_if_blank option to only update the values when the label is empty

obj.set_title_and_label("My Title", :only_if_blank=> true)

Parameters:

  • new_title (String)
  • opts (Hash) (defaults to: {})

    (optional) hash of configuration options



37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/concerns/hydra/model_methods.rb', line 37

def set_title_and_label(new_title, opts={})
  if opts[:only_if_blank]
    if respond_to?(:label) && label.blank?
      self.label = new_title
      set_title new_title
    end
  else
    self.label = new_title if respond_to? :label
    set_title new_title
  end
end