2872: Start doing everything from folder perspective.
[arvados.git] / apps / workbench / app / controllers / folders_controller.rb
1 class FoldersController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(Folders)
8   end
9
10   def show_pane_list
11     %w(Contents Permissions)
12   end
13
14   def remove_item
15     @removed_uuids = []
16     links = []
17     item = ArvadosBase.find params[:item_uuid]
18     if (item.class == Link and
19         item.link_class == 'name' and
20         item.tail_uuid = @object.uuid)
21       # Given uuid is a name link, linking an object to this
22       # folder. First follow the link to find the item we're removing,
23       # then delete the link.
24       links << item
25       item = ArvadosBase.find item.head_uuid
26     else
27       # Given uuid is an object. Delete all names.
28       links += Link.where(tail_uuid: @object.uuid,
29                           head_uuid: item.uuid,
30                           link_class: 'name')
31     end
32     links.each do |link|
33       @removed_uuids << link.uuid
34       link.destroy
35     end
36     if item.owner_uuid == @object.uuid
37       # Object is owned by this folder. Remove it from the folder by
38       # changing owner to the current user.
39       item.update_attributes owner_uuid: current_user
40       @removed_uuids << item.uuid
41     end
42   end
43
44   def index
45     @objects = Group.where(group_class: 'folder').order('name')
46     parent_of = {current_user.uuid => 'me'}
47     @objects.each do |ob|
48       parent_of[ob.uuid] = ob.owner_uuid
49     end
50     children_of = {false => [], 'me' => [current_user]}
51     @objects.each do |ob|
52       if ob.owner_uuid != current_user.uuid and
53           not parent_of.has_key? ob.owner_uuid
54         parent_of[ob.uuid] = false
55       end
56       children_of[parent_of[ob.uuid]] ||= []
57       children_of[parent_of[ob.uuid]] << ob
58     end
59     buildtree = lambda do |children_of, root_uuid=false|
60       tree = {}
61       children_of[root_uuid].andand.each do |ob|
62         tree[ob] = buildtree.call(children_of, ob.uuid)
63       end
64       tree
65     end
66     sorted_paths = lambda do |tree, depth=0|
67       paths = []
68       tree.keys.sort_by { |ob|
69         ob.is_a?(String) ? ob : ob.friendly_link_name
70       }.each do |ob|
71         paths << {object: ob, depth: depth}
72         paths += sorted_paths.call tree[ob], depth+1
73       end
74       paths
75     end
76     @my_folder_tree =
77       sorted_paths.call buildtree.call(children_of, 'me')
78     @shared_folder_tree =
79       sorted_paths.call({'Shared with me' =>
80                           buildtree.call(children_of, false)})
81   end
82
83   def show
84     @objects = @object.contents include_linked: true
85     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
86                                 ['link_class', '=', 'permission']])
87     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
88
89     @objects_and_names = []
90     @objects.each do |object|
91       if !(name_links = @objects.links_for(object, 'name')).empty?
92         name_links.each do |name_link|
93           @objects_and_names << [object, name_link]
94         end
95       else
96         @objects_and_names << [object,
97                                Link.new(tail_uuid: @object.uuid,
98                                         head_uuid: object.uuid,
99                                         link_class: "name",
100                                         name: "")]
101       end
102     end
103
104     super
105   end
106
107   def create
108     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
109     @new_resource_attrs[:name] ||= 'New folder'
110     super
111   end
112
113   def update
114     @updates = params['folder']
115     super
116   end
117 end