9587: trash page
[arvados.git] / apps / workbench / app / controllers / trash_items_controller.rb
1 class TrashItemsController < ApplicationController
2   def model_class
3     Collection
4   end
5
6   def find_objects_for_index
7     # If it's not the index rows partial display, just return
8     # The /index request will again be invoked to display the
9     # partial at which time, we will be using the objects found.
10     return if !params[:partial]
11
12     trashed_items
13
14     if @objects.any?
15       @next_page_filters = next_page_filters('<=')
16       @next_page_href = url_for(partial: :trash_rows,
17                                 filters: @next_page_filters.to_json)
18       preload_links_for_objects(@objects.to_a)
19     else
20       @next_page_href = nil
21     end
22   end
23
24   def next_page_href with_params={}
25     @next_page_href
26   end
27
28   def trashed_items
29     # API server index doesn't return manifest_text by default, but our
30     # callers want it unless otherwise specified.
31     @select ||= Collection.columns.map(&:name)
32     limit = if params[:limit] then params[:limit].to_i else 100 end
33     offset = if params[:offset] then params[:offset].to_i else 0 end
34
35     base_search = Collection.select(@select).include_trash(true).where(is_trashed: true)
36     base_search = base_search.filter(params[:filters]) if params[:filters]
37
38     if params[:search].andand.length.andand > 0
39       tags = Link.where(any: ['contains', params[:search]])
40       @objects = (base_search.limit(limit).offset(offset).where(uuid: tags.collect(&:head_uuid)) |
41                       base_search.where(any: ['contains', params[:search]])).
42         uniq { |c| c.uuid }
43     else
44       @objects = base_search.limit(limit).offset(offset)
45     end
46
47     @links = Link.where(head_uuid: @objects.collect(&:uuid))
48     @collection_info = {}
49     @objects.each do |c|
50       @collection_info[c.uuid] = {
51         tag_links: [],
52         wanted: false,
53         wanted_by_me: false,
54         provenance: [],
55         links: []
56       }
57     end
58     @links.each do |link|
59       @collection_info[link.head_uuid] ||= {}
60       info = @collection_info[link.head_uuid]
61       case link.link_class
62       when 'tag'
63         info[:tag_links] << link
64       when 'resources'
65         info[:wanted] = true
66         info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
67       when 'provenance'
68         info[:provenance] << link.name
69       end
70       info[:links] << link
71     end
72     @request_url = request.url
73   end
74
75   def untrash_item
76     params[:item_uuids] = [params[:item_uuid]]
77     untrash_items
78     render template: 'untrash_items'
79   end
80
81   def untrash_items
82     @untrashed_uuids = []
83
84     updates = {trash_at: nil}
85     #updates[:trash_at] = nil
86
87     params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
88       item.update_attributes updates
89       @untrashed_uuids << item.uuid
90     end
91
92     render_template :untrashed_items
93   end
94 end