/
application_helper.rb
150 lines (132 loc) · 4.54 KB
/
application_helper.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
module ApplicationHelper
def application_name
'Avalon'
end
def image_for(item_id)
#TODO index the thumbnail url to avoid having to hit fedora to get it
media_object = MediaObject.find(item_id)
masterfile = media_object.parts.first
imageurl = thumbnail_master_file_path(masterfile) unless masterfile.nil? or masterfile.thumbnail.new?
imageurl ||= case
when media_object.format == "Moving image"
"video_icon.png"
when media_object.format == "Sound"
"audio_icon.png"
when (media_object.parts.length >= 2)
# TODO
# We need to test if both audio and video are present
# instead of assuming when there is more than one part
"hybrid_icon.png"
else
nil
end
end
# Creates a hot link to the downloadable file if it is available. File names longer
# than 25 characters are truncated although this can be overridden by passing in a
# different value
def file_download_label(masterfile)
# Check to see if the file name is longer than 25 characters
if 20 > masterfile.descMetadata.title[0].length
label_display = masterfile.descMetadata.title[0]
else
label_display = truncate(masterfile.descMetadata.title[0], length: 15)
label_display << "."
label_display << masterfile.descMetadata.title[0].split('.').last
end
end
# Not the best way to do this but it works for the time being
def wrap_text(content)
unless content.nil? or content.empty?
content.gsub(/\n/, '<br />').html_safe
else
"<em>Not provided</em>".html_safe
end
end
def display_metadata(label, value, default=nil)
return if value.blank? and default.nil?
value ||= default
sanitized_values = Array(value).collect { |v| sanitize(v.to_s.strip) }.delete_if(&:empty?)
label = label.pluralize(sanitized_values.size)
result = content_tag(:dt, label) +
content_tag(:dd) {
sanitized_values.join('; ')
}
end
# def display_metadata(label, value, default=nil)
# return if value.blank? and default.nil?
# value ||= default
# sanitized_values = Array(value).collect { |v| sanitize(v.to_s.strip) }.delete_if(&:empty?)
# label = label.pluralize(sanitized_values.size)
# label_value_pair = label + ': ' + sanitized_values.join('; ')
# result = content_tag(:li, label_value_pair)
# end
#FIXME
#This helper should be used by blacklight to display the "Title" field in search results
def search_result_label item
label = item.id
unless item["title_display"].blank?
label = truncate(item["title_display"], length: 35)
end
if ! item['duration_t'].nil? && ! item['duration_t'].empty?
item_duration = item['duration_t'].first
if item_duration.respond_to?(:to_i)
formatted_duration = milliseconds_to_formatted_time(item_duration.to_i)
label += " (#{formatted_duration})"
end
end
label
end
# Retrieve the current status of processing and display a concise version
# for use in the interface
def conversion_status_for(mediaobject)
unless mediaobject.parts.empty?
masterfile = mediaobject.parts.first.pid
masterfile.status
else
"No files have been selected"
end
end
def stream_label_for(resource)
label = ''
unless resource.nil?
if resource.label.blank?
label = File.basename(resource.file_location)
else
label = resource.label
end
end
label
end
#Taken from Hydra::Controller::ControllerBehavior
def user_key
current_user.user_key if current_user
end
# the mediainfo gem returns duration as milliseconds
# see attr_reader.rb line 48 in the mediainfo source
def milliseconds_to_formatted_time( milliseconds )
total_seconds = milliseconds / 1000
hours = total_seconds / (60 * 60)
minutes = (total_seconds / 60) % 60
seconds = total_seconds % 60
output = ''
if hours > 0
output += "#{hours}:"
end
output += "#{minutes}:#{seconds.to_s.rjust(2,'0')}"
output
end
def link_to_add_dynamic_field( name, opts = {} )
opts.merge!( class: 'add-dynamic-field btn btn-mini' )
link_to name, '#', opts
end
def git_commit_info pattern="%s %s"
begin
repo = Grit::Repo.new(Rails.root)
branch = repo.head.name
commit = repo.head.commit.sha[0..5]
pattern % [branch,commit]
rescue
""
end
end
end