3235: Fix project dropdown, use it when choosing collections.
[arvados.git] / apps / workbench / app / controllers / projects_controller.rb
1 class ProjectsController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(Projects)
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: 'projects/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         # project. 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 project. Remove it from the project 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 destroy
52     while (objects = Link.filter([['owner_uuid','=',@object.uuid],
53                                   ['tail_uuid','=',@object.uuid]])).any?
54       objects.each do |object|
55         object.destroy
56       end
57     end
58     while (objects = @object.contents(include_linked: false)).any?
59       objects.each do |object|
60         object.update_attributes! owner_uuid: current_user.uuid
61       end
62     end
63     if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
64       params[:return_to] ||= group_path(@object.owner_uuid)
65     else
66       params[:return_to] ||= projects_path
67     end
68     super
69   end
70
71   def find_objects_for_index
72     @objects = all_projects
73     super
74   end
75
76   def show
77     if !@object
78       return render_not_found("object not found")
79     end
80     @objects = @object.contents(limit: 50,
81                                 include_linked: true,
82                                 offset: params[:offset] || 0)
83     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
84                                 ['link_class', '=', 'permission']])
85     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
86
87     @objects_and_names = []
88     @objects.each do |object|
89       if !(name_links = @objects.links_for(object, 'name')).empty?
90         name_links.each do |name_link|
91           @objects_and_names << [object, name_link]
92         end
93       elsif object.respond_to? :name
94         @objects_and_names << [object, object]
95       else
96         @objects_and_names << [object,
97                                Link.new(owner_uuid: @object.uuid,
98                                         tail_uuid: @object.uuid,
99                                         head_uuid: object.uuid,
100                                         link_class: "name",
101                                         name: "")]
102       end
103     end
104     if params[:partial]
105       respond_to do |f|
106         f.json {
107           render json: {
108             content: render_to_string(partial: 'show_contents_rows.html',
109                                       formats: [:html],
110                                       locals: {
111                                         objects_and_names: @objects_and_names,
112                                         project: @object
113                                       }),
114             next_page_href: (next_page_offset and
115                              url_for(offset: next_page_offset, partial: true))
116           }
117         }
118       end
119     else
120       super
121     end
122   end
123
124   def create
125     @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
126     @new_resource_attrs[:name] ||= 'New project'
127     super
128   end
129
130   def update
131     @updates = params['project']
132     super
133   end
134 end