clean up SDK examples
[arvados.git] / apps / workbench / app / helpers / application_helper.rb
1 module ApplicationHelper
2   def current_user
3     controller.current_user
4   end
5
6   def current_api_host
7     Rails.configuration.arvados_v1_base.gsub /https?:\/\/|\/arvados\/v1/,''
8   end
9
10   def render_content_from_database(markup)
11     raw RedCloth.new(markup).to_html
12   end
13
14   def human_readable_bytes_html(n)
15     return h(n) unless n.is_a? Fixnum
16     raw = n.to_s
17     cooked = ''
18     while raw.length > 3
19       cooked = ',' + raw[-3..-1] + cooked
20       raw = raw[0..-4]
21     end
22     cooked = raw + cooked
23   end
24
25   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
26     if (resource_class = ArvadosBase::resource_class_for_uuid(attrvalue, opts))
27       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
28       link_name = opts[:link_text]
29       if !link_name
30         link_name = link_uuid
31         if opts[:with_class_name]
32           link_name = "#{resource_class.to_s} #{link_name}"
33         end
34         style_opts = style_opts.merge(style: 'font-family: monospace')
35       end
36       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
37     else
38       attrvalue
39     end
40   end
41
42   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
43     attrvalue = object.send(attr) if attrvalue.nil?
44     return attrvalue if !object.attribute_editable? attr
45
46     input_type = 'text'
47     case object.class.attribute_info[attr.to_sym].andand[:type]
48     when 'text'
49       input_type = 'textarea'
50     when 'datetime'
51       input_type = 'date'
52     else
53       input_type = 'text'
54     end
55
56     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
57
58     link_to attrvalue.to_s, '#', {
59       "data-emptytext" => "none",
60       "data-placement" => "bottom",
61       "data-type" => input_type,
62       "data-resource" => object.class.to_s.underscore,
63       "data-name" => attr,
64       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
65       "data-original-title" => "Update #{attr.gsub '_', ' '}",
66       :class => "editable"
67     }.merge(htmloptions)
68   end
69 end