Merge remote-tracking branch 'origin' into 1786-replace-jekyll-with-zenweb
[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
17     orders = {
18       1 => "bytes",
19       1024 => "KiB",
20       (1024*1024) => "MiB",
21       (1024*1024*1024) => "GiB",
22       (1024*1024*1024*1024) => "TiB"
23     }
24
25     orders.each do |k, v|
26       sig = (n.to_f/k)
27       if sig >=1 and sig < 1024
28         if v == 'bytes'
29           return "%i #{v}" % sig
30         else
31           return "%0.1f #{v}" % sig
32         end
33       end
34     end
35     
36     return h(n)
37       #raw = n.to_s
38     #cooked = ''
39     #while raw.length > 3
40     #  cooked = ',' + raw[-3..-1] + cooked
41     #  raw = raw[0..-4]
42     #end
43     #cooked = raw + cooked
44   end
45
46   def resource_class_for_uuid(attrvalue, opts={})
47     ArvadosBase::resource_class_for_uuid(attrvalue, opts)
48   end
49
50   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
51     if (resource_class = resource_class_for_uuid(attrvalue, opts))
52       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
53       link_name = opts[:link_text]
54       if !link_name
55         link_name = link_uuid
56
57         if opts[:friendly_name]
58           begin
59             friendly_name = resource_class.find(link_uuid).friendly_link_name
60             if friendly_name and not friendly_name.empty?
61               link_name = friendly_name
62             end
63           rescue RuntimeError
64             # If that lookup failed, the link will too. So don't make one.
65             return attrvalue
66           end
67         end
68         if opts[:with_class_name]
69           link_name = "#{resource_class.to_s}: #{link_name}"
70         end
71       end
72       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
73     else
74       attrvalue
75     end
76   end
77
78   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
79     attrvalue = object.send(attr) if attrvalue.nil?
80     return attrvalue if !object.attribute_editable? attr
81
82     input_type = 'text'
83     case object.class.attribute_info[attr.to_sym].andand[:type]
84     when 'text'
85       input_type = 'textarea'
86     when 'datetime'
87       input_type = 'date'
88     else
89       input_type = 'text'
90     end
91
92     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
93
94     link_to attrvalue.to_s, '#', {
95       "data-emptytext" => "none",
96       "data-placement" => "bottom",
97       "data-type" => input_type,
98       "data-resource" => object.class.to_s.underscore,
99       "data-name" => attr,
100       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
101       "data-original-title" => "Update #{attr.gsub '_', ' '}",
102       :class => "editable"
103     }.merge(htmloptions)
104   end
105 end