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
20 return "0 bytes" if (n == 0)
26 (1024*1024*1024) => "GiB",
27 (1024*1024*1024*1024) => "TiB"
32 if sig >=1 and sig < 1024
34 return "%i #{v}" % sig
36 return "%0.1f #{v}" % sig
45 # cooked = ',' + raw[-3..-1] + cooked
48 #cooked = raw + cooked
51 def resource_class_for_uuid(attrvalue, opts={})
52 ArvadosBase::resource_class_for_uuid(attrvalue, opts)
56 # Returns HTML that links to the Arvados object specified in +attrvalue+
57 # Provides various output control and styling options.
59 # +attrvalue+ an Arvados model object or uuid
61 # +opts+ a set of flags to control output:
63 # [:link_text] the link text to use (may include HTML), overrides everything else
65 # [:friendly_name] whether to use the "friendly" name in the link text (by
66 # calling #friendly_link_name on the object), otherwise use the uuid
68 # [:with_class_name] prefix the link text with the class name of the model
70 # [:no_tags] disable tags in the link text (default is to show tags).
71 # Currently tags are only shown for Collections.
73 # [:thumbnail] if the object is a collection, show an image thumbnail if the
74 # collection consists of a single image file.
76 # [:no_link] don't create a link, just return the link text
78 # +style_opts+ additional HTML properties for the anchor tag, passed to link_to
80 def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
81 if (resource_class = resource_class_for_uuid(attrvalue, opts))
82 if attrvalue.is_a? ArvadosBase
84 link_uuid = attrvalue.uuid
89 link_name = opts[:link_text]
91 link_name = object.andand.default_name || resource_class.default_name
93 if opts[:friendly_name]
94 if attrvalue.respond_to? :friendly_link_name
95 link_name = attrvalue.friendly_link_name
98 if resource_class.name == 'Collection'
99 link_name = collections_for_object(link_uuid).andand.first.andand.friendly_link_name
101 link_name = object_for_dataclass(resource_class, link_uuid).andand.friendly_link_name
103 rescue ArvadosApiClient::NotFoundException
104 # If that lookup failed, the link will too. So don't make one.
109 if opts[:with_class_name]
110 link_name = "#{resource_class.to_s}: #{link_name}"
112 if !opts[:no_tags] and resource_class == Collection
113 links_for_object(link_uuid).each do |tag|
114 if tag.link_class.in? ["tag", "identifier"]
115 link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
119 if opts[:thumbnail] and resource_class == Collection
120 # add an image thumbnail if the collection consists of a single image file.
121 collections_for_object(link_uuid).each do |c|
122 if c.files.length == 1 and CollectionsHelper::is_image c.files.first[1]
124 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path c.files.first}", style: "height: 4em; width: auto"
129 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
133 link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: ((opts[:name_link].andand.uuid) || link_uuid) }, style_opts
136 # just return attrvalue if it is not recognizable as an Arvados object or uuid.
141 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
142 attrvalue = object.send(attr) if attrvalue.nil?
143 if !object.attribute_editable?(attr, :ever) or
144 (!object.editable? and
145 !object.owner_uuid.in?(my_projects.collect(&:uuid)))
146 return ((attrvalue && attrvalue.length > 0 && attrvalue) ||
147 (attr == 'name' and object.andand.default_name) ||
152 case object.class.attribute_info[attr.to_sym].andand[:type]
154 input_type = 'textarea'
161 is_textile = object.textile_attributes.andand.include?(attr)
162 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
163 rendervalue = is_textile ? raw( RedCloth.new(attrvalue.to_s).to_html ) : attrvalue
168 key: object.class.to_s.underscore
172 ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
174 ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
175 ajax_options['data-pk'][:defaults] = object.attributes
177 ajax_options['data-pk'] = ajax_options['data-pk'].to_json
178 @unique_id ||= (Time.now.to_f*1000000).to_i
179 span_id = object.uuid.to_s + '-' + attr.to_s + '-' + (@unique_id += 1).to_s
181 span_tag = content_tag 'span', rendervalue, {
182 "data-emptytext" => (object.andand.default_name || 'none'),
183 "data-placement" => "bottom",
184 "data-type" => input_type,
185 "data-title" => "Edit #{attr.gsub '_', ' '}",
187 "data-object-uuid" => object.uuid,
188 "data-toggle" => "manual",
189 "data-value" => attrvalue,
191 :class => "editable #{is_textile ? 'editable-textile' : ''}"
192 }.merge(htmloptions).merge(ajax_options)
193 edit_button = raw('<a href="#" class="btn btn-xs btn-default btn-nodecorate" data-toggle="x-editable tooltip" data-toggle-selector="#' + span_id + '" data-placement="top" title="' + (htmloptions[:tiptitle] || 'edit') + '"><i class="fa fa-fw fa-pencil"></i></a>')
194 if htmloptions[:btnplacement] == :left
195 edit_button + ' ' + span_tag
197 span_tag + ' ' + edit_button
201 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
204 attrvalue = value_info
206 if value_info.is_a? Hash
207 if value_info[:output_of]
208 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
210 if value_info[:dataclass]
211 dataclass = value_info[:dataclass]
213 if value_info[:optional] != nil
214 required = (value_info[:optional] != "true")
216 if value_info[:required] != nil
217 required = value_info[:required]
220 # Pick a suitable attrvalue to show as the current value (i.e.,
221 # the one that would be used if we ran the pipeline right now).
222 if value_info[:value]
223 attrvalue = value_info[:value]
224 elsif value_info[:default]
225 attrvalue = value_info[:default]
232 !object.attribute_editable?(attr, :ever) or
233 (!object.editable? and
234 !object.owner_uuid.in?(my_projects.collect(&:uuid)))
235 return link_to_if_arvados_object attrvalue
240 dataclass = dataclass.constantize
244 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
247 id = "#{object.uuid}-#{subattr.join('-')}"
252 if value_info.is_a? Hash
256 if dataclass == Collection
257 selection_param = object.class.to_s.underscore + dn
258 display_value = attrvalue
259 if value_info.is_a?(Hash)
260 if (link = Link.find? value_info[:link_uuid])
261 display_value = link.name
262 elsif value_info[:link_name]
263 display_value = value_info[:link_name]
266 if (attr == :components) and (subattr.size > 2)
267 chooser_title = "Choose a dataset for #{object.component_input_title(subattr[0], subattr[2])}:"
269 chooser_title = "Choose a dataset:"
271 modal_path = choose_collections_path \
272 ({ title: chooser_title,
273 filters: [['tail_uuid', '=', object.owner_uuid]].to_json,
275 action_href: pipeline_instance_path(id: object.uuid),
276 action_method: 'patch',
277 preconfigured_search_str: "#{value_info[:search_for]}",
280 selection_param: selection_param,
281 success: 'page-refresh'
284 return content_tag('div', :class => 'input-group') do
285 html = text_field_tag(dn, display_value,
287 "form-control #{'required' if required}")
288 html + content_tag('span', :class => 'input-group-btn') do
291 { :class => "btn btn-primary",
299 if dataclass.andand.is_a?(Class)
301 elsif dataclass == 'number'
303 elsif attrvalue.is_a? Array
304 # TODO: find a way to edit arrays with x-editable
306 elsif attrvalue.is_a? Fixnum or attrvalue.is_a? Float
308 elsif attrvalue.is_a? String
318 if dataclass.is_a? Class and dataclass < ArvadosBase
319 objects = get_n_objects_of_class dataclass, 10
320 objects.each do |item|
322 preload_uuids << item.uuid
324 if attrvalue and !attrvalue.empty?
325 preload_uuids << attrvalue
327 preload_links_for_objects preload_uuids
329 if attrvalue and !attrvalue.empty?
330 links_for_object(attrvalue).each do |link|
331 if link.link_class.in? ["tag", "identifier"]
332 attrtext += " [#{link.name}]"
335 selectables.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
339 itemuuids << item.uuid
340 selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
343 itemuuids.each do |itemuuid|
344 links_for_object(itemuuid).each do |link|
345 if link.link_class.in? ["tag", "identifier"]
346 selectables.each do |selectable|
347 if selectable['uuid'] == link.head_uuid
348 selectable['name'] += ' [' + link.name + ']'
356 lt = link_to attrtext, '#', {
357 "data-emptytext" => "none",
358 "data-placement" => "bottom",
359 "data-type" => datatype,
360 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
361 "data-title" => "Set value for #{subattr[-1].to_s}",
363 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
364 "data-value" => attrvalue,
365 # "clear" button interferes with form-control's up/down arrows
366 "data-clear" => false,
367 :class => "editable #{'required' if required} form-control",
371 lt += raw("\n<script>")
374 lt += raw("add_form_selection_sources(#{selectables.to_json});\n")
377 lt += raw("$('[data-name=\"#{dn}\"]').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
379 lt += raw("</script>")
384 def render_arvados_object_list_start(list, button_text, button_href,
385 params={}, *rest, &block)
386 show_max = params.delete(:show_max) || 3
387 params[:class] ||= 'btn btn-xs btn-default'
388 list[0...show_max].each { |item| yield item }
389 unless list[show_max].nil?
390 link_to(h(button_text) +
391 raw(' <i class="fa fa-fw fa-arrow-circle-right"></i>'),
392 button_href, params, *rest)
396 def render_controller_partial partial, opts
397 cname = opts.delete :controller_name
399 render opts.merge(partial: "#{cname}/#{partial}")
400 rescue ActionView::MissingTemplate
401 render opts.merge(partial: "application/#{partial}")
405 RESOURCE_CLASS_ICONS = {
406 "Collection" => "fa-archive",
407 "Group" => "fa-users",
408 "Human" => "fa-male", # FIXME: Use a more inclusive icon.
410 "KeepDisk" => "fa-hdd-o",
411 "KeepService" => "fa-exchange",
412 "Link" => "fa-arrows-h",
413 "Node" => "fa-cloud",
414 "PipelineInstance" => "fa-gears",
415 "PipelineTemplate" => "fa-gears",
416 "Repository" => "fa-code-fork",
417 "Specimen" => "fa-flask",
418 "Trait" => "fa-clipboard",
420 "VirtualMachine" => "fa-terminal",
422 DEFAULT_ICON_CLASS = "fa-cube"
424 def fa_icon_class_for_class(resource_class, default=DEFAULT_ICON_CLASS)
425 RESOURCE_CLASS_ICONS.fetch(resource_class.to_s, default)
428 def fa_icon_class_for_uuid(uuid, default=DEFAULT_ICON_CLASS)
429 fa_icon_class_for_class(resource_class_for_uuid(uuid), default)
432 def fa_icon_class_for_object(object, default=DEFAULT_ICON_CLASS)
433 case class_name = object.class.to_s
435 object.group_class ? 'fa-folder' : 'fa-users'
437 RESOURCE_CLASS_ICONS.fetch(class_name, default)
441 def chooser_preview_url_for object
442 case object.class.to_s
444 polymorphic_path(object, tab_pane: 'chooser_preview')