1 module ApplicationHelper
3 controller.current_user
6 def self.match_uuid(uuid)
7 /^([0-9a-z]{5})-([0-9a-z]{5})-([0-9a-z]{15})$/.match(uuid.to_s)
11 Rails.configuration.arvados_v1_base.gsub /https?:\/\/|\/arvados\/v1/,''
14 def render_content_from_database(markup)
15 raw RedCloth.new(markup).to_html
18 def human_readable_bytes_html(n)
19 return h(n) unless n.is_a? Fixnum
25 (1024*1024*1024) => "GiB",
26 (1024*1024*1024*1024) => "TiB"
31 if sig >=1 and sig < 1024
33 return "%i #{v}" % sig
35 return "%0.1f #{v}" % sig
44 # cooked = ',' + raw[-3..-1] + cooked
47 #cooked = raw + cooked
50 def resource_class_for_uuid(attrvalue, opts={})
51 ArvadosBase::resource_class_for_uuid(attrvalue, opts)
55 # Returns HTML that links to the Arvados object specified in +attrvalue+
56 # Provides various output control and styling options.
58 # +attrvalue+ an Arvados model object or uuid
60 # +opts+ a set of flags to control output:
62 # [:link_text] the link text to use (may include HTML), overrides everything else
64 # [:friendly_name] whether to use the "friendly" name in the link text (by
65 # calling #friendly_link_name on the object), otherwise use the uuid
67 # [:with_class_name] prefix the link text with the class name of the model
69 # [:no_tags] disable tags in the link text (default is to show tags).
70 # Currently tags are only shown for Collections.
72 # [:thumbnail] if the object is a collection, show an image thumbnail if the
73 # collection consists of a single image file.
75 # [:no_link] don't create a link, just return the link text
77 # +style_opts+ additional HTML properties for the anchor tag, passed to link_to
79 def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
80 if (resource_class = resource_class_for_uuid(attrvalue, opts))
81 link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
82 link_name = opts[:link_text]
86 if opts[:friendly_name]
87 if attrvalue.respond_to? :friendly_link_name
88 link_name = attrvalue.friendly_link_name
91 link_name = resource_class.find(link_uuid).friendly_link_name
93 # If that lookup failed, the link will too. So don't make one.
98 if opts[:with_class_name]
99 link_name = "#{resource_class.to_s}: #{link_name}"
101 if !opts[:no_tags] and resource_class == Collection
102 Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
103 link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
106 if opts[:thumbnail] and resource_class == Collection
107 # add an image thumbnail if the collection consists of a single image file.
108 Collection.where(uuid: link_uuid).each do |c|
109 if c.files.length == 1 and CollectionsHelper::is_image c.files.first[1]
111 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path c.files.first}", style: "height: 4em; width: auto"
116 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
120 link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
123 # just return attrvalue if it is not recognizable as an Arvados object or uuid.
128 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
129 attrvalue = object.send(attr) if attrvalue.nil?
130 return attrvalue if !object.attribute_editable? attr
133 case object.class.attribute_info[attr.to_sym].andand[:type]
135 input_type = 'textarea'
142 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
147 key: object.class.to_s.underscore
151 ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
153 ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
154 ajax_options['data-pk'][:defaults] = object.attributes
155 ajax_options['data-pk'][:_method] = 'post'
157 ajax_options['data-pk'] = ajax_options['data-pk'].to_json
159 link_to attrvalue.to_s, '#', {
160 "data-emptytext" => "none",
161 "data-placement" => "bottom",
162 "data-type" => input_type,
163 "data-title" => "Update #{attr.gsub '_', ' '}",
166 }.merge(htmloptions).merge(ajax_options)
169 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
172 attrvalue = value_info
174 if value_info.is_a? Hash
175 if value_info[:output_of]
176 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
178 if value_info[:dataclass]
179 dataclass = value_info[:dataclass]
181 if value_info[:optional] != nil
182 required = (value_info[:optional] != "true")
184 if value_info[:required] != nil
185 required = value_info[:required]
188 # Pick a suitable attrvalue to show as the current value (i.e.,
189 # the one that would be used if we ran the pipeline right now).
190 if value_info[:value]
191 attrvalue = value_info[:value]
192 elsif value_info[:default]
193 attrvalue = value_info[:default]
199 unless object.andand.attribute_editable? attr
200 return link_to_if_arvados_object attrvalue
205 dataclass = dataclass.constantize
209 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
212 if dataclass.andand.is_a?(Class)
214 elsif dataclass == 'number'
216 elsif attrvalue.is_a? Array
217 # TODO: find a way to edit arrays with x-editable
219 elsif attrvalue.is_a? Fixnum or attrvalue.is_a? Float
221 elsif attrvalue.is_a? String
225 id = "#{object.uuid}-#{subattr.join('-')}"
230 if value_info.is_a? Hash
236 if dataclass and dataclass.is_a? Class
237 if attrvalue and !attrvalue.empty?
238 Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
239 attrtext += " [#{tag.name}]"
241 selectables.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
243 #dataclass.where(uuid: attrvalue).each do |item|
244 # selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
247 dataclass.limit(10).each do |item|
248 itemuuids << item.uuid
249 selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
251 Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
252 selectables.each do |selectable|
253 if selectable['uuid'] == tag.head_uuid
254 selectable['name'] += ' [' + tag.name + ']'
260 lt = link_to attrtext, '#', {
261 "data-emptytext" => "none",
262 "data-placement" => "bottom",
263 "data-type" => datatype,
264 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
265 "data-title" => "Set value for #{subattr[-1].to_s}",
267 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
268 "data-showbuttons" => "false",
269 "data-value" => attrvalue,
270 :class => "editable #{'required' if required}",
274 lt += raw("\n<script>")
277 lt += raw("add_form_selection_sources(#{selectables.to_json});\n")
280 lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
282 lt += raw("</script>")