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 link_name = resource_class.find(link_uuid).friendly_link_name
100 # If that lookup failed, the link will too. So don't make one.
105 if opts[:with_class_name]
106 link_name = "#{resource_class.to_s}: #{link_name}"
108 if !opts[:no_tags] and resource_class == Collection
109 Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
110 link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
113 if opts[:thumbnail] and resource_class == Collection
114 # add an image thumbnail if the collection consists of a single image file.
115 Collection.where(uuid: link_uuid).each do |c|
116 if c.files.length == 1 and CollectionsHelper::is_image c.files.first[1]
118 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path c.files.first}", style: "height: 4em; width: auto"
123 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
127 link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
130 # just return attrvalue if it is not recognizable as an Arvados object or uuid.
135 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
136 attrvalue = object.send(attr) if attrvalue.nil?
137 if !object.attribute_editable?(attr, :ever) or
138 (!object.editable? and
139 !object.owner_uuid.in?(my_folders.collect(&:uuid)))
144 case object.class.attribute_info[attr.to_sym].andand[:type]
146 input_type = 'textarea'
153 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
158 key: object.class.to_s.underscore
162 ajax_options['data-url'] = url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore)
164 ajax_options['data-url'] = url_for(action: "create", controller: object.class.to_s.pluralize.underscore)
165 ajax_options['data-pk'][:defaults] = object.attributes
167 ajax_options['data-pk'] = ajax_options['data-pk'].to_json
168 @unique_id ||= (Time.now.to_f*1000000).to_i
169 span_id = object.uuid.to_s + '-' + attr.to_s + '-' + (@unique_id += 1).to_s
171 span_tag = content_tag 'span', attrvalue.to_s, {
172 "data-emptytext" => (object.andand.default_name || 'none'),
173 "data-placement" => "bottom",
174 "data-type" => input_type,
175 "data-title" => "Edit #{attr.gsub '_', ' '}",
177 "data-object-uuid" => object.uuid,
178 "data-toggle" => "manual",
181 }.merge(htmloptions).merge(ajax_options)
182 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>')
183 if htmloptions[:btnplacement] == :left
184 edit_button + ' ' + span_tag
186 span_tag + ' ' + edit_button
190 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
193 attrvalue = value_info
195 if value_info.is_a? Hash
196 if value_info[:output_of]
197 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
199 if value_info[:dataclass]
200 dataclass = value_info[:dataclass]
202 if value_info[:optional] != nil
203 required = (value_info[:optional] != "true")
205 if value_info[:required] != nil
206 required = value_info[:required]
209 # Pick a suitable attrvalue to show as the current value (i.e.,
210 # the one that would be used if we ran the pipeline right now).
211 if value_info[:value]
212 attrvalue = value_info[:value]
213 elsif value_info[:default]
214 attrvalue = value_info[:default]
221 !object.attribute_editable?(attr, :ever) or
222 (!object.editable? and
223 !object.owner_uuid.in?(my_folders.collect(&:uuid)))
224 return link_to_if_arvados_object attrvalue
229 dataclass = dataclass.constantize
233 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
236 id = "#{object.uuid}-#{subattr.join('-')}"
241 if value_info.is_a? Hash
245 if dataclass == Collection
246 selection_param = object.class.to_s.underscore + dn
247 display_value = attrvalue
248 if value_info.is_a?(Hash)
249 if (link = Link.find? value_info[:link_uuid])
250 display_value = link.name
251 elsif value_info[:link_name]
252 display_value = value_info[:link_name]
255 modal_path = choose_collections_path \
256 ({ title: 'Choose a dataset:',
257 filters: [['owner_uuid', '=', object.owner_uuid]].to_json,
259 action_href: pipeline_instance_path(id: object.uuid),
260 action_method: 'patch',
263 selection_param: selection_param,
264 success: 'page-refresh'
267 return content_tag('div', :class => 'input-group') do
268 html = text_field_tag(dn, display_value, :class => 'form-control')
269 html + content_tag('span', :class => 'input-group-btn') do
272 { :class => "btn btn-primary",
280 if dataclass.andand.is_a?(Class)
282 elsif dataclass == 'number'
284 elsif attrvalue.is_a? Array
285 # TODO: find a way to edit arrays with x-editable
287 elsif attrvalue.is_a? Fixnum or attrvalue.is_a? Float
289 elsif attrvalue.is_a? String
295 if dataclass and dataclass.is_a? Class
296 if attrvalue and !attrvalue.empty?
297 Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
298 attrtext += " [#{tag.name}]"
300 selectables.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
302 #dataclass.where(uuid: attrvalue).each do |item|
303 # selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
306 dataclass.limit(10).each do |item|
307 itemuuids << item.uuid
308 selectables.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
310 Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
311 selectables.each do |selectable|
312 if selectable['uuid'] == tag.head_uuid
313 selectable['name'] += ' [' + tag.name + ']'
319 lt = link_to attrtext, '#', {
320 "data-emptytext" => "none",
321 "data-placement" => "bottom",
322 "data-type" => datatype,
323 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
324 "data-title" => "Set value for #{subattr[-1].to_s}",
326 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
327 "data-showbuttons" => "false",
328 "data-value" => attrvalue,
329 :class => "editable #{'required' if required} form-control",
333 lt += raw("\n<script>")
336 lt += raw("add_form_selection_sources(#{selectables.to_json});\n")
339 lt += raw("$('[data-name=\"#{dn}\"]').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
341 lt += raw("</script>")
346 def render_arvados_object_list_start(list, button_text, button_href,
347 params={}, *rest, &block)
348 show_max = params.delete(:show_max) || 3
349 params[:class] ||= 'btn btn-xs btn-default'
350 list[0...show_max].each { |item| yield item }
351 unless list[show_max].nil?
352 link_to(h(button_text) +
353 raw(' <i class="fa fa-fw fa-arrow-circle-right"></i>'),
354 button_href, params, *rest)
358 def render_controller_partial partial, opts
359 cname = opts.delete :controller_name
361 render opts.merge(partial: "#{cname}/#{partial}")
362 rescue ActionView::MissingTemplate
363 render opts.merge(partial: "application/#{partial}")
367 def fa_icon_class_for_object object
368 case object.class.to_s.to_sym
372 object.group_class ? 'fa-folder' : 'fa-users'
373 when :Job, :PipelineInstance, :PipelineTemplate