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)
54 def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
55 if (resource_class = resource_class_for_uuid(attrvalue, opts))
56 link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
57 link_name = opts[:link_text]
61 if opts[:friendly_name]
62 if attrvalue.respond_to? :friendly_link_name
63 link_name = attrvalue.friendly_link_name
66 link_name = resource_class.find(link_uuid).friendly_link_name
68 # If that lookup failed, the link will too. So don't make one.
73 if opts[:with_class_name]
74 link_name = "#{resource_class.to_s}: #{link_name}"
76 if !opts[:no_tags] and resource_class == Collection
77 Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
78 link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
81 if opts[:thumbnail] and resource_class == Collection
83 Collection.where(uuid: link_uuid).each do |c|
84 c.files.each do |file|
85 if CollectionsHelper::is_image file[1]
87 link_name += image_tag "#{url_for c}/#{CollectionsHelper::file_path file}", style: "height: 4em; width: auto"
95 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
99 link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
106 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
107 attrvalue = object.send(attr) if attrvalue.nil?
108 return attrvalue if !object.attribute_editable? attr
111 case object.class.attribute_info[attr.to_sym].andand[:type]
113 input_type = 'textarea'
120 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
122 link_to attrvalue.to_s, '#', {
123 "data-emptytext" => "none",
124 "data-placement" => "bottom",
125 "data-type" => input_type,
126 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
127 "data-title" => "Update #{attr.gsub '_', ' '}",
129 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
134 def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
136 attrvalue = object.send(attr)
138 if attrvalue and attrvalue.is_a? Hash
139 attrvalue = attrvalue[k]
149 #puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
150 if template.is_a? Hash
151 if template[:output_of]
152 return raw("<span class='label label-default'>#{template[:output_of]}</span>")
154 if template[:dataclass]
155 dataclass = template[:dataclass]
157 if template[:optional] != nil
158 required = (template[:optional] != "true")
160 if template[:required] != nil
161 required = template[:required]
167 if template.is_a? Hash
169 rsc = template[:value]
170 elsif template[:default]
171 rsc = template[:default]
175 return link_to_if_arvados_object(rsc) if !object
176 return link_to_if_arvados_object(attrvalue) if !object.attribute_editable? attr
180 dataclass = dataclass.constantize
184 dataclass = ArvadosBase.resource_class_for_uuid(rsc)
187 if dataclass && dataclass.is_a?(Class)
189 elsif dataclass == 'number'
192 if template.is_a? Array
194 elsif template.is_a? String
195 if /^\d+$/.match(template)
203 id = "#{object.uuid}-#{subattr.join('-')}"
209 if attrvalue.is_a? String
210 attrvalue = attrvalue.strip
214 if dataclass and dataclass.is_a? Class
216 if attrvalue and !attrvalue.empty?
217 Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
218 attrtext += " [#{tag.name}]"
220 items.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
222 #dataclass.where(uuid: attrvalue).each do |item|
223 # items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
226 dataclass.limit(10).each do |item|
227 itemuuids << item.uuid
228 items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
230 Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
232 if item.uuid == tag.head_uuid
233 item.name += ' [' + tag.name + ']'
239 lt = link_to attrtext, '#', {
240 "data-emptytext" => "none",
241 "data-placement" => "bottom",
242 "data-type" => datatype,
243 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
244 "data-title" => "Set value for #{subattr[-1].to_s}",
246 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
247 "data-showbuttons" => "false",
248 "data-value" => attrvalue,
249 :class => "editable #{'required' if required}",
253 lt += raw("\n<script>")
255 if items and items.length > 0
256 lt += raw("add_form_selection_sources(#{items.to_json});\n")
259 lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
261 lt += raw("</script>")