Merge branch '2257-inequality-conditions' into 2290-user-activity
[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 self.match_uuid(uuid)
7     /^([0-9a-z]{5})-([0-9a-z]{5})-([0-9a-z]{15})$/.match(uuid.to_s)
8   end
9
10   def current_api_host
11     Rails.configuration.arvados_v1_base.gsub /https?:\/\/|\/arvados\/v1/,''
12   end
13
14   def render_content_from_database(markup)
15     raw RedCloth.new(markup).to_html
16   end
17
18   def human_readable_bytes_html(n)
19     return h(n) unless n.is_a? Fixnum
20
21     orders = {
22       1 => "bytes",
23       1024 => "KiB",
24       (1024*1024) => "MiB",
25       (1024*1024*1024) => "GiB",
26       (1024*1024*1024*1024) => "TiB"
27     }
28
29     orders.each do |k, v|
30       sig = (n.to_f/k)
31       if sig >=1 and sig < 1024
32         if v == 'bytes'
33           return "%i #{v}" % sig
34         else
35           return "%0.1f #{v}" % sig
36         end
37       end
38     end
39     
40     return h(n)
41       #raw = n.to_s
42     #cooked = ''
43     #while raw.length > 3
44     #  cooked = ',' + raw[-3..-1] + cooked
45     #  raw = raw[0..-4]
46     #end
47     #cooked = raw + cooked
48   end
49
50   def resource_class_for_uuid(attrvalue, opts={})
51     ArvadosBase::resource_class_for_uuid(attrvalue, opts)
52   end
53
54   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
55     if (resource_class = resource_class_for_uuid(attrvalue, opts))
56       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
57       link_name = opts[:link_text]
58       if !link_name
59         link_name = link_uuid
60
61         if opts[:friendly_name]
62           if attrvalue.respond_to? :friendly_link_name
63             link_name = attrvalue.friendly_link_name
64           else
65             begin
66               link_name = resource_class.find(link_uuid).friendly_link_name
67             rescue RuntimeError
68               # If that lookup failed, the link will too. So don't make one.
69               return attrvalue
70             end
71           end
72         end
73         if opts[:with_class_name]
74           link_name = "#{resource_class.to_s}: #{link_name}"
75         end
76       end
77       style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
78       link_to link_name, { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
79     else
80       attrvalue
81     end
82   end
83
84   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
85     attrvalue = object.send(attr) if attrvalue.nil?
86     return attrvalue if !object.attribute_editable? attr
87
88     input_type = 'text'
89     case object.class.attribute_info[attr.to_sym].andand[:type]
90     when 'text'
91       input_type = 'textarea'
92     when 'datetime'
93       input_type = 'date'
94     else
95       input_type = 'text'
96     end
97
98     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
99
100     link_to attrvalue.to_s, '#', {
101       "data-emptytext" => "none",
102       "data-placement" => "bottom",
103       "data-type" => input_type,
104       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
105       "data-title" => "Update #{attr.gsub '_', ' '}",
106       "data-name" => attr,
107       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
108       :class => "editable"
109     }.merge(htmloptions)
110   end
111
112   def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
113     if object
114       attrvalue = object.send(attr)
115       subattr.each do |k|
116         if attrvalue and attrvalue.is_a? Hash
117           attrvalue = attrvalue[k]
118         else
119           break
120         end
121       end
122     end
123
124     datatype = nil
125     required = true
126     if template
127       #puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
128       if template.is_a? Hash
129         if template[:output_of]
130           return raw("<span class='label label-default'>#{template[:output_of]}</span>")
131         end
132         if template[:dataclass]
133           dataclass = template[:dataclass]
134         end
135         if template[:optional] != nil
136           required = (template[:optional] != "true")
137         end
138         if template[:required] != nil
139           required = template[:required]
140         end
141       end
142     end
143
144     rsc = template
145     if template.is_a? Hash
146       if template[:value]
147         rsc = template[:value]
148       elsif template[:default]
149         rsc = template[:default]
150       end
151     end
152
153     return link_to_if_arvados_object(rsc) if !object
154     return link_to_if_arvados_object(attrvalue) if !object.attribute_editable? attr
155
156     if dataclass
157       begin
158         dataclass = dataclass.constantize
159       rescue NameError
160       end
161     else
162       dataclass = ArvadosBase.resource_class_for_uuid(rsc)
163     end
164
165     if dataclass && dataclass.is_a?(Class)
166       datatype = 'select'
167     elsif dataclass == 'number'
168       datatype = 'number'
169     else
170       if template.is_a? Array
171         # ?!?
172       elsif template.is_a? String
173         if /^\d+$/.match(template)
174           datatype = 'number'
175         else
176           datatype = 'text'
177         end
178       end
179     end
180
181     id = "#{object.uuid}-#{subattr.join('-')}"
182     dn = "[#{attr}]"
183     subattr.each do |a|
184       dn += "[#{a}]"
185     end
186
187     if attrvalue.is_a? String
188       attrvalue = attrvalue.strip
189     end
190
191     if dataclass and dataclass.is_a? Class
192       items = []
193       if attrvalue and !attrvalue.empty?
194         items.append({name: attrvalue, uuid: attrvalue, type: dataclass.to_s})
195       end
196       #dataclass.where(uuid: attrvalue).each do |item|
197       #  items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
198       #end
199       dataclass.limit(10).each do |item|
200         items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
201       end
202     end
203
204     lt = link_to attrvalue, '#', {
205       "data-emptytext" => "none",
206       "data-placement" => "bottom",
207       "data-type" => datatype,
208       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
209       "data-title" => "Set value for #{subattr[-1].to_s}",
210       "data-name" => dn,
211       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
212       "data-showbuttons" => "false",
213       "data-value" => attrvalue,
214       :class => "editable #{'required' if required}",
215       :id => id
216     }.merge(htmloptions)
217
218     lt += raw("\n<script>")
219     
220     if items and items.length > 0
221       lt += raw("add_form_selection_sources(#{items.to_json});\n")
222     end
223
224     lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
225
226     lt += raw("</script>")
227
228     lt 
229   end
230 end