2872: all tests are updated to reflect the new ui.
[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 Advanced)
12   end
13
14   def remove_item
15     params[:item_uuids] = [params[:item_uuid]]
16     remove_items
17     render template: 'folders/remove_items'
18   end
19
20   def remove_items
21     @removed_uuids = []
22     links = []
23     params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
24       if (item.class == Link and
25           item.link_class == 'name' and
26           item.tail_uuid == @object.uuid)
27         # Given uuid is a name link, linking an object to this
28         # folder. First follow the link to find the item we're removing,
29         # then delete the link.
30         links << item
31         item = ArvadosBase.find item.head_uuid
32       else
33         # Given uuid is an object. Delete all names.
34         links += Link.where(tail_uuid: @object.uuid,
35                             head_uuid: item.uuid,
36                             link_class: 'name')
37       end
38       links.each do |link|
39         @removed_uuids << link.uuid
40         link.destroy
41       end
42       if item.owner_uuid == @object.uuid
43         # Object is owned by this folder. Remove it from the folder by
44         # changing owner to the current user.
45         item.update_attributes owner_uuid: current_user.uuid
46         @removed_uuids << item.uuid
47       end
48     end
49   end
50
51   def find_objects_for_index
52     @objects = Group.where(group_class: 'folder').order('name')
53     super
54     parent_of = {current_user.uuid => 'me'}
55     @objects.each do |ob|
56       parent_of[ob.uuid] = ob.owner_uuid
57     end
58     children_of = {false => [], 'me' => [current_user]}
59     @objects.each do |ob|
60       if ob.owner_uuid != current_user.uuid and
61           not parent_of.has_key? ob.owner_uuid
62         parent_of[ob.uuid] = false
63       end
64       children_of[parent_of[ob.uuid]] ||= []
65       children_of[parent_of[ob.uuid]] << ob
66     end
67     buildtree = lambda do |children_of, root_uuid=false|
68       tree = {}
69       children_of[root_uuid].andand.each do |ob|
70         tree[ob] = buildtree.call(children_of, ob.uuid)
71       end
72       tree
73     end
74     sorted_paths = lambda do |tree, depth=0|
75       paths = []
76       tree.keys.sort_by { |ob|
77         ob.is_a?(String) ? ob : ob.friendly_link_name
78       }.each do |ob|
79         paths << {object: ob, depth: depth}
80         paths += sorted_paths.call tree[ob], depth+1
81       end
82       paths
83     end
84     @my_folder_tree =
85       sorted_paths.call buildtree.call(children_of, 'me')
86     @shared_folder_tree =
87       sorted_paths.call({'Shared with me' =>
88                           buildtree.call(children_of, false)})
89   end
90
91   def show
92     @objects = @object.contents(limit: 50,
93                                 include_linked: true,
94                                 offset: params[:offset] || 0)
95     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
96                                 ['link_class', '=', 'permission']])
97     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
98
99     @objects_and_names = []
100     @objects.each do |object|
101       if !(name_links = @objects.links_for(object, 'name')).empty?
102         name_links.each do |name_link|
103           @objects_and_names << [object, name_link]
104         end
105       elsif object.respond_to? :name
106         @objects_and_names << [object, object]
107       else
108         @objects_and_names << [object,
109                                Link.new(owner_uuid: @object.uuid,
110                                         tail_uuid: @object.uuid,
111                                         head_uuid: object.uuid,
112                                         link_class: "name",
113                                         name: "")]
114       end
115     end
116     if params[:partial]
117       respond_to do |f|
118         f.json {
119           render json: {
120             content: render_to_string(partial: 'show_contents_rows.html',
121                                       formats: [:html],
122                                       locals: {
123                                         objects_and_names: @objects_and_names,
124                                         folder: @object
125                                       }),
126             next_page_href: (next_page_offset and
127                              url_for(offset: next_page_offset, partial: true))
128           }
129         }
130       end
131     else
132       super
133     end
134   end
135
136   def create
137     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
138     @new_resource_attrs[:name] ||= 'New folder'
139     super
140   end
141
142   def update
143     @updates = params['folder']
144     super
145   end
146 end