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]
63 link_name = resource_class.find(link_uuid).friendly_link_name
65 # If that lookup failed, the link will too. So don't make one.
69 if opts[:with_class_name]
70 link_name = "#{resource_class.to_s}: #{link_name}"
73 style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
74 link_to link_name, { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
80 def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
81 attrvalue = object.send(attr) if attrvalue.nil?
82 return attrvalue if !object.attribute_editable? attr
85 case object.class.attribute_info[attr.to_sym].andand[:type]
87 input_type = 'textarea'
94 attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
96 link_to attrvalue.to_s, '#', {
97 "data-emptytext" => "none",
98 "data-placement" => "bottom",
99 "data-type" => input_type,
100 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
101 "data-title" => "Update #{attr.gsub '_', ' '}",
103 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
108 def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
109 attrvalue = object.send(attr)
111 if attrvalue and attrvalue.is_a? Hash
112 attrvalue = attrvalue[k]
121 puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
122 if template.is_a? Hash
123 if template[:output_of]
124 return raw("<span class='label label-default'>#{template[:output_of]}</span>")
126 if template[:datatype]
127 datatype = template[:datatype]
129 if template[:optional] != nil
130 required = (template[:optional] != "true")
132 if template[:required] != nil
133 required = template[:required]
138 return attrvalue if !object.attribute_editable? attr
141 dataclass = ArvadosBase.resource_class_for_uuid(template)
145 if template.is_a? Array
147 elsif template.is_a? String
148 if /^\d+$/.match(template)
157 id = "#{object.uuid}-#{subattr.join('-')}"
163 if attrvalue.is_a? String
164 attrvalue = attrvalue.strip
169 dataclass.where(uuid: attrvalue).each do |item|
170 items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
172 dataclass.limit(19).each do |item|
173 items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
177 lt = link_to attrvalue, '#', {
178 "data-emptytext" => "none",
179 "data-placement" => "bottom",
180 "data-type" => datatype,
181 "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
182 "data-title" => "Update #{subattr[-1].to_s.titleize}",
184 "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
185 "data-showbuttons" => "false",
186 :class => "editable #{'required' if required}",
190 lt += raw('<script>')
192 if items and items.length > 0
193 lt += raw("add_form_selection_sources(#{items.to_json});\n")
196 lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
198 lt += raw("</script>")