2872: Fix some tests and bugs
[arvados.git] / apps / workbench / app / models / collection.rb
1 class Collection < ArvadosBase
2   MD5_EMPTY = 'd41d8cd98f00b204e9800998ecf8427e'
3
4   # Return true if the given string is the locator of a zero-length blob
5   def self.is_empty_blob_locator? locator
6     !!locator.to_s.match("^#{MD5_EMPTY}(\\+.*)?\$")
7   end
8
9   def self.goes_in_folders?
10     true
11   end
12
13   def content_summary
14     ApplicationController.helpers.human_readable_bytes_html(total_bytes) + " " + super
15   end
16
17   def total_bytes
18     if files
19       tot = 0
20       files.each do |file|
21         tot += file[2]
22       end
23       tot
24     end
25   end
26
27   def files_tree
28     return [] if files.empty?
29     tree = files.group_by { |file_spec| File.split(file_spec.first) }
30     # Fill in entries for empty directories.
31     tree.keys.map { |basedir, _| File.split(basedir) }.each do |splitdir|
32       until tree.include?(splitdir)
33         tree[splitdir] = []
34         splitdir = File.split(splitdir.first)
35       end
36     end
37     dir_to_tree = lambda do |dirname|
38       # First list subdirectories, with their files inside.
39       subnodes = tree.keys.select { |bd, td| (bd == dirname) and (td != '.') }
40         .sort.flat_map do |parts|
41         [parts + [nil]] + dir_to_tree.call(File.join(parts))
42       end
43       # Then extend that list with files in this directory.
44       subnodes + tree[File.split(dirname)]
45     end
46     dir_to_tree.call('.')
47   end
48
49   def attribute_editable? attr, *args
50     false
51   end
52
53   def self.creatable?
54     false
55   end
56
57   def provenance
58     arvados_api_client.api "collections/#{self.uuid}/", "provenance"
59   end
60
61   def used_by
62     arvados_api_client.api "collections/#{self.uuid}/", "used_by"
63   end
64
65 end