Merge branch '1748-workbench-view-files'
[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] and resource_class.column_names.include? "name" and resource_class.find(link_uuid).name != nil and not resource_class.find(link_uuid).name.empty?
58           link_name = "#{resource_class.to_s} #{resource_class.find(link_uuid).name}"
59         elsif opts[:friendly_name] and resource_class.column_names.include? "hostname" and resource_class.find(link_uuid).hostname != nil and not resource_class.find(link_uuid).hostname.empty?
60           link_name = "#{resource_class.to_s} #{resource_class.find(link_uuid).hostname}"
61         elsif opts[:friendly_name] and resource_class.column_names.include? "first_name"
62           link_name = "#{resource_class.to_s} #{resource_class.find(link_uuid).first_name} #{resource_class.find(link_uuid).last_name}"
63         else
64           if opts[:with_class_name]
65             link_name = "#{resource_class.to_s} #{link_name}"
66           end
67         end
68       end
69       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
70     else
71       attrvalue
72     end
73   end
74
75   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
76     attrvalue = object.send(attr) if attrvalue.nil?
77     return attrvalue if !object.attribute_editable? attr
78
79     input_type = 'text'
80     case object.class.attribute_info[attr.to_sym].andand[:type]
81     when 'text'
82       input_type = 'textarea'
83     when 'datetime'
84       input_type = 'date'
85     else
86       input_type = 'text'
87     end
88
89     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
90
91     link_to attrvalue.to_s, '#', {
92       "data-emptytext" => "none",
93       "data-placement" => "bottom",
94       "data-type" => input_type,
95       "data-resource" => object.class.to_s.underscore,
96       "data-name" => attr,
97       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
98       "data-original-title" => "Update #{attr.gsub '_', ' '}",
99       :class => "editable"
100     }.merge(htmloptions)
101   end
102 end