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