Advertise filters param in discovery doc.
[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             link_name = resource_class.find(link_uuid).friendly_link_name
60           rescue RuntimeError
61             # If that lookup failed, the link will too. So don't make one.
62             return attrvalue
63           end
64         end
65         if opts[:with_class_name]
66           link_name = "#{resource_class.to_s}: #{link_name}"
67         end
68       end
69       style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
70       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
71     else
72       attrvalue
73     end
74   end
75
76   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
77     attrvalue = object.send(attr) if attrvalue.nil?
78     return attrvalue if !object.attribute_editable? attr
79
80     input_type = 'text'
81     case object.class.attribute_info[attr.to_sym].andand[:type]
82     when 'text'
83       input_type = 'textarea'
84     when 'datetime'
85       input_type = 'date'
86     else
87       input_type = 'text'
88     end
89
90     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
91
92     link_to attrvalue.to_s, '#', {
93       "data-emptytext" => "none",
94       "data-placement" => "bottom",
95       "data-type" => input_type,
96       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
97       "data-title" => "Update #{attr.gsub '_', ' '}",
98       "data-name" => attr,
99       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
100       :class => "editable"
101     }.merge(htmloptions)
102   end
103 end