Merge remote-tracking branch 'origin' into 1862-show-object-links
[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 resource_class_for_uuid(attrvalue, opts={})
26     ArvadosBase::resource_class_for_uuid(attrvalue, opts)
27   end
28
29   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
30     if (resource_class = resource_class_for_uuid(attrvalue, opts))
31       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
32       link_name = opts[:link_text]
33       if !link_name
34         link_name = link_uuid
35         opts[:friendly_name]
36         if opts[:friendly_name] and resource_class.column_names.include? "name"
37           link_name = resource_class.find(link_uuid).name
38         elsif opts[:friendly_name] and resource_class.column_names.include? "first_name"
39           link_name = "#{resource_class.find(link_uuid).first_name} #{resource_class.find(link_uuid).last_name}"
40         else
41           if opts[:with_class_name]
42             link_name = "#{resource_class.to_s} #{link_name}"
43           end
44         end
45       end
46       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
47     else
48       attrvalue
49     end
50   end
51
52   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
53     attrvalue = object.send(attr) if attrvalue.nil?
54     return attrvalue if !object.attribute_editable? attr
55
56     input_type = 'text'
57     case object.class.attribute_info[attr.to_sym].andand[:type]
58     when 'text'
59       input_type = 'textarea'
60     when 'datetime'
61       input_type = 'date'
62     else
63       input_type = 'text'
64     end
65
66     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
67
68     link_to attrvalue.to_s, '#', {
69       "data-emptytext" => "none",
70       "data-placement" => "bottom",
71       "data-type" => input_type,
72       "data-resource" => object.class.to_s.underscore,
73       "data-name" => attr,
74       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
75       "data-original-title" => "Update #{attr.gsub '_', ' '}",
76       :class => "editable"
77     }.merge(htmloptions)
78   end
79 end