Fixed typo in application_helper.rb
[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         if !opts[:no_tags] and resource_class == Collection
77           Link.where(head_uuid: link_uuid, link_class: ["tag", "identifier"]).each do |tag|
78             link_name += ' <span class="label label-info">' + html_escape(tag.name) + '</span>'
79           end
80         end
81       end
82       style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
83       if opts[:no_link]
84         raw(link_name)
85       else
86         link_to raw(link_name), { controller: resource_class.to_s.tableize, action: 'show', id: link_uuid }, style_opts
87       end
88     else
89       attrvalue
90     end
91   end
92
93   def render_editable_attribute(object, attr, attrvalue=nil, htmloptions={})
94     attrvalue = object.send(attr) if attrvalue.nil?
95     return attrvalue if !object.attribute_editable? attr
96
97     input_type = 'text'
98     case object.class.attribute_info[attr.to_sym].andand[:type]
99     when 'text'
100       input_type = 'textarea'
101     when 'datetime'
102       input_type = 'date'
103     else
104       input_type = 'text'
105     end
106
107     attrvalue = attrvalue.to_json if attrvalue.is_a? Hash or attrvalue.is_a? Array
108
109     link_to attrvalue.to_s, '#', {
110       "data-emptytext" => "none",
111       "data-placement" => "bottom",
112       "data-type" => input_type,
113       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
114       "data-title" => "Update #{attr.gsub '_', ' '}",
115       "data-name" => attr,
116       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
117       :class => "editable"
118     }.merge(htmloptions)
119   end
120
121   def render_editable_subattribute(object, attr, subattr, template, htmloptions={})
122     if object
123       attrvalue = object.send(attr)
124       subattr.each do |k|
125         if attrvalue and attrvalue.is_a? Hash
126           attrvalue = attrvalue[k]
127         else
128           break
129         end
130       end
131     end
132
133     datatype = nil
134     required = true
135     if template
136       #puts "Template is #{template.class} #{template.is_a? Hash} #{template}"
137       if template.is_a? Hash
138         if template[:output_of]
139           return raw("<span class='label label-default'>#{template[:output_of]}</span>")
140         end
141         if template[:dataclass]
142           dataclass = template[:dataclass]
143         end
144         if template[:optional] != nil
145           required = (template[:optional] != "true")
146         end
147         if template[:required] != nil
148           required = template[:required]
149         end
150       end
151     end
152
153     rsc = template
154     if template.is_a? Hash
155       if template[:value]
156         rsc = template[:value]
157       elsif template[:default]
158         rsc = template[:default]
159       end
160     end
161
162     return link_to_if_arvados_object(rsc) if !object
163     return link_to_if_arvados_object(attrvalue) if !object.attribute_editable? attr
164
165     if dataclass
166       begin
167         dataclass = dataclass.constantize
168       rescue NameError
169       end
170     else
171       dataclass = ArvadosBase.resource_class_for_uuid(rsc)
172     end
173
174     if dataclass && dataclass.is_a?(Class)
175       datatype = 'select'
176     elsif dataclass == 'number'
177       datatype = 'number'
178     else
179       if template.is_a? Array
180         # ?!?
181       elsif template.is_a? String
182         if /^\d+$/.match(template)
183           datatype = 'number'
184         else
185           datatype = 'text'
186         end
187       end
188     end
189
190     id = "#{object.uuid}-#{subattr.join('-')}"
191     dn = "[#{attr}]"
192     subattr.each do |a|
193       dn += "[#{a}]"
194     end
195
196     if attrvalue.is_a? String
197       attrvalue = attrvalue.strip
198     end
199
200     attrtext = attrvalue
201     if dataclass and dataclass.is_a? Class
202       items = []
203       if attrvalue and !attrvalue.empty?
204         Link.where(head_uuid: attrvalue, link_class: ["tag", "identifier"]).each do |tag|
205           attrtext += " [#{tag.name}]"
206         end
207         items.append({name: attrtext, uuid: attrvalue, type: dataclass.to_s})
208       end
209       #dataclass.where(uuid: attrvalue).each do |item|
210       #  items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
211       #end
212       itemuuids = []
213       dataclass.limit(10).each do |item|
214         itemuuids << item.uuid
215         items.append({name: item.uuid, uuid: item.uuid, type: dataclass.to_s})
216       end
217       Link.where(head_uuid: itemuuids, link_class: ["tag", "identifier"]).each do |tag|
218         items.each do |item|
219           if item.uuid == tag.head_uuid
220             item.name += ' [' + tag.name + ']'
221           end
222         end
223       end
224     end
225
226     lt = link_to attrtext, '#', {
227       "data-emptytext" => "none",
228       "data-placement" => "bottom",
229       "data-type" => datatype,
230       "data-url" => url_for(action: "update", id: object.uuid, controller: object.class.to_s.pluralize.underscore),
231       "data-title" => "Set value for #{subattr[-1].to_s}",
232       "data-name" => dn,
233       "data-pk" => "{id: \"#{object.uuid}\", key: \"#{object.class.to_s.underscore}\"}",
234       "data-showbuttons" => "false",
235       "data-value" => attrvalue,
236       :class => "editable #{'required' if required}",
237       :id => id
238     }.merge(htmloptions)
239
240     lt += raw("\n<script>")
241
242     if items and items.length > 0
243       lt += raw("add_form_selection_sources(#{items.to_json});\n")
244     end
245
246     lt += raw("$('##{id}').editable({source: function() { return select_form_sources('#{dataclass}'); } });\n")
247
248     lt += raw("</script>")
249
250     lt
251   end
252 end