Merge branch '1977-provenance-report' of git.clinicalfuture.com:arvados into 1977...
[arvados.git] / apps / workbench / app / helpers / application_helper.rb
index f630e764f22432ce7f9c1fc703cc30295140c6ff..a42016b8eca0cc4a70ead39b1dbc4ff07cf0bb23 100644 (file)
@@ -13,26 +13,60 @@ module ApplicationHelper
 
   def human_readable_bytes_html(n)
     return h(n) unless n.is_a? Fixnum
-    raw = n.to_s
-    cooked = ''
-    while raw.length > 3
-      cooked = ',' + raw[-3..-1] + cooked
-      raw = raw[0..-4]
+
+    orders = {
+      1 => "bytes",
+      1024 => "KiB",
+      (1024*1024) => "MiB",
+      (1024*1024*1024) => "GiB",
+      (1024*1024*1024*1024) => "TiB"
+    }
+
+    orders.each do |k, v|
+      sig = (n.to_f/k)
+      if sig >=1 and sig < 1024
+        if v == 'bytes'
+          return "%i #{v}" % sig
+        else
+          return "%0.1f #{v}" % sig
+        end
+      end
     end
-    cooked = raw + cooked
+    
+    return h(n)
+      #raw = n.to_s
+    #cooked = ''
+    #while raw.length > 3
+    #  cooked = ',' + raw[-3..-1] + cooked
+    #  raw = raw[0..-4]
+    #end
+    #cooked = raw + cooked
+  end
+
+  def resource_class_for_uuid(attrvalue, opts={})
+    ArvadosBase::resource_class_for_uuid(attrvalue, opts)
   end
 
   def link_to_if_arvados_object(attrvalue, opts={}, style_opts={})
-    if (resource_class = ArvadosBase::resource_class_for_uuid(attrvalue, opts))
+    if (resource_class = resource_class_for_uuid(attrvalue, opts))
       link_uuid = attrvalue.is_a?(ArvadosBase) ? attrvalue.uuid : attrvalue
       link_name = opts[:link_text]
       if !link_name
         link_name = link_uuid
+
+        if opts[:friendly_name]
+          begin
+            link_name = resource_class.find(link_uuid).friendly_link_name
+          rescue RuntimeError
+            # If that lookup failed, the link will too. So don't make one.
+            return attrvalue
+          end
+        end
         if opts[:with_class_name]
-          link_name = "#{resource_class.to_s} #{link_name}"
+          link_name = "#{resource_class.to_s}: #{link_name}"
         end
-        style_opts = style_opts.merge(style: 'font-family: monospace')
       end
+      style_opts[:class] = (style_opts[:class] || '') + ' nowrap'
       link_to link_name, { controller: resource_class.to_s.underscore.pluralize, action: 'show', id: link_uuid }, style_opts
     else
       attrvalue