-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmodel_methods.rb
71 lines (62 loc) · 2.34 KB
/
model_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Hydra::ModelMethods
#
# Adds metadata about the depositor to the asset
# Most important behavior: if the asset has a rightsMetadata datastream, this method will add +depositor_id+ to its individual edit permissions.
# @param [String, #user_key] depositor
#
def apply_depositor_metadata(depositor)
prop_ds = self.datastreams["properties"]
rights_ds = self.datastreams["rightsMetadata"]
depositor_id = depositor.respond_to?(:user_key) ? depositor.user_key : depositor
if prop_ds
prop_ds.depositor = depositor_id unless prop_ds.nil?
end
rights_ds.permissions({:person=>depositor_id}, 'edit') unless rights_ds.nil?
return true
end
# 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
#
# @param [#read] file the IO object that is the blob
# @param [String] file the IO object that is the blob
def add_file(file, dsid, file_name)
mime_types = MIME::Types.of(file_name)
mime_type = mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
options = {:label=>file_name, :mimeType=>mime_type}
options[:dsid] = dsid if dsid
add_file_datastream(file, options)
set_title_and_label( file_name, :only_if_blank=>true )
end
# Set the title and label on the current object
#
# @param [String] new_title
# @param [Hash] opts (optional) hash of configuration options
#
# @example 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)
def set_title_and_label(new_title, opts={})
if opts[:only_if_blank]
if self.label.nil? || self.label.empty?
self.label = new_title
self.set_title( new_title )
end
else
self.label = new_title
set_title( new_title )
end
end
# Set the title and label on the current object
#
# @param [String] new_title
# @param [Hash] opts (optional) hash of configuration options
def set_title(new_title, opts={})
if self.datastreams.has_key?("descMetadata")
desc_metadata_ds = self.datastreams["descMetadata"]
if desc_metadata_ds.respond_to?(:title_values)
desc_metadata_ds.title_values = new_title
else
desc_metadata_ds.title = new_title
end
end
end
end