add tutorials and references to home page
[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 render_content_from_database(markup)
7     raw RedCloth.new(markup).to_html
8   end
9
10   def human_readable_bytes_html(n)
11     return h(n) unless n.is_a? Fixnum
12     raw = n.to_s
13     cooked = ''
14     while raw.length > 3
15       cooked = ',' + raw[-3..-1] + cooked
16       raw = raw[0..-4]
17     end
18     cooked = raw + cooked
19   end
20
21   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
22     if (resource_class = ArvadosBase::resource_class_for_uuid(attrvalue, opts))
23       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
24       link_name = opts[:link_text]
25       if !link_name
26         link_name = link_uuid
27         if !opts[:with_prefixes]
28           link_name = link_name.sub /^.{5}-.{5}-/, ''
29         end
30         if opts[:with_class_name]
31           link_name = "#{resource_class.to_s} #{link_name}"
32         end
33         style_opts = style_opts.merge(style: 'font-family: monospace')
34       end
35       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
36     else
37       attrvalue
38     end
39   end
40
41   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
42     attrvalue = object.send(attr) if attrvalue.nil?
43     return attrvalue if !object.attribute_editable? attr
44
45     input_type = 'text'
46     case object.class.attribute_info[attr.to_sym][:type]
47     when 'text'
48       input_type = 'textarea'
49     when 'datetime'
50       input_type = 'date'
51     else
52       input_type = 'text'
53     end
54
55     link_to attrvalue.to_s, '#', {
56       "data-emptytext" => "none",
57       "data-placement" => "bottom",
58       "data-type" => input_type,
59       "data-resource" => object.class.to_s.underscore,
60       "data-name" => attr,
61       "data-value" => object.send(attr),
62       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
63       "data-original-title" => "Update #{attr.gsub '_', ' '}",
64       :class => "editable"
65     }.merge(htmloptions)
66   end
67 end