Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / app / controllers / trash_items_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class TrashItemsController < ApplicationController
6   def model_class
7     Collection
8   end
9
10   def index_pane_list
11     %w(Recent_trash)
12   end
13
14   def find_objects_for_index
15     # If it's not the index rows partial display, just return
16     # The /index request will again be invoked to display the
17     # partial at which time, we will be using the objects found.
18     return if !params[:partial]
19
20     trashed_items
21
22     if @objects.any?
23       @objects = @objects.sort_by { |obj| obj.trash_at }.reverse
24       @next_page_filters = next_page_filters('<=')
25       @next_page_href = url_for(partial: :trash_rows,
26                                 filters: @next_page_filters.to_json)
27     else
28       @next_page_href = nil
29     end
30   end
31
32   def next_page_href with_params={}
33     @next_page_href
34   end
35
36   def next_page_filters nextpage_operator
37     next_page_filters = @filters.reject do |attr, op, val|
38       (attr == 'trash_at' and op == nextpage_operator) or
39       (attr == 'uuid' and op == 'not in')
40     end
41
42     if @objects.any?
43       last_trash_at = @objects.last.trash_at
44
45       last_uuids = []
46       @objects.each do |obj|
47         last_uuids << obj.uuid if obj.trash_at.eql?(last_trash_at)
48       end
49
50       next_page_filters += [['trash_at', nextpage_operator, last_trash_at]]
51       next_page_filters += [['uuid', 'not in', last_uuids]]
52     end
53
54     next_page_filters
55   end
56
57   def trashed_items
58     # API server index doesn't return manifest_text by default, but our
59     # callers want it unless otherwise specified.
60     @select ||= Collection.columns.map(&:name)
61     limit = if params[:limit] then params[:limit].to_i else 100 end
62     offset = if params[:offset] then params[:offset].to_i else 0 end
63
64     base_search = Collection.select(@select).include_trash(true).where(is_trashed: true)
65     base_search = base_search.filter(params[:filters]) if params[:filters]
66
67     if params[:search].andand.length.andand > 0
68       tags = Link.where(any: ['contains', params[:search]])
69       base_search = base_search.limit(limit).offset(offset)
70       @objects = (base_search.where(uuid: tags.collect(&:head_uuid)) |
71                   base_search.where(any: ['contains', params[:search]])).
72                   uniq { |c| c.uuid }
73     else
74       @objects = base_search.limit(limit).offset(offset)
75     end
76   end
77
78   def untrash_items
79     @untrashed_uuids = []
80
81     updates = {trash_at: nil}
82
83     Collection.include_trash(1).where(uuid: params[:selection]).each do |c|
84       c.untrash
85       @untrashed_uuids << c.uuid
86     end
87
88     respond_to do |format|
89       format.js
90     end
91   end
92 end