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
144 link_to attrvalue.to_s, '#', {
145 "data-emptytext" => "none",
146 "data-placement" => "bottom",
147 "data-type" => input_type,
148 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
149 "data-title" => "Update #{attr.gsub '_', ' '}",
151 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
156 def render_pipeline_component_attribute(object, attr, subattr, value_info, htmloptions={})
159 attrvalue = value_info
161 if value_info.is_a? Hash
162 if value_info[:output_of]
163 return raw("<span class='label label-default'>#{value_info[:output_of]}</span>")
165 if value_info[:dataclass]
166 dataclass = value_info[:dataclass]
168 if value_info[:optional] != nil
169 required = (value_info[:optional] != "true")
171 if value_info[:required] != nil
172 required = value_info[:required]
175 # Pick a suitable attrvalue to show as the current value (i.e.,
176 # the one that would be used if we ran the pipeline right now).
177 if value_info[:value]
178 attrvalue = value_info[:value]
179 elsif value_info[:default]
180 attrvalue = value_info[:default]
186 unless object.andand.attribute_editable? attr
187 return link_to_if_arvados_object attrvalue
192 dataclass = dataclass.constantize
196 dataclass = ArvadosBase.resource_class_for_uuid(attrvalue)
199 if dataclass.andand.is_a?(Class)
201 elsif dataclass == 'number'
203 elsif attrvalue.is_a? Array
204 # TODO: find a way to edit arrays with x-editable
206 elsif attrvalue.is_a? Fixnum or attrvalue.is_a? Float
208 elsif attrvalue.is_a? String
212 id = "#{object.uuid}-#{subattr.join('-')}"
217 if value_info.is_a? Hash
223 if dataclass and dataclass.is_a? Class
224 if attrvalue and !attrvalue.empty?
225 Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
226 attrtext += " [#{tag.name}]"
228 items.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
230 #dataclass.where(uuid: attrvalue).each do |item|
231 # items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
234 dataclass.limit(10).each do |item|
235 itemuuids << item.uuid
236 items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
238 Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
240 if item.uuid == tag.head_uuid
241 item.name += ' [' + tag.name + ']'
247 lt = link_to attrtext, '#', {
248 "data-emptytext" => "none",
249 "data-placement" => "bottom",
250 "data-type" => datatype,
251 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore, merge: true),
252 "data-title" => "Set value for #{subattr[-1].to_s}",
254 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
255 "data-showbuttons" => "false",
256 "data-value" => attrvalue,
257 :class => "editable #{'required' if required}",
261 lt += raw("\n<script>")
264 lt += raw("add_form_selection_sources(#{items.to_json});\n")
267 lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
269 lt += raw("</script>")