-
Notifications
You must be signed in to change notification settings - Fork 482
/
admin_search_controller.rb
73 lines (66 loc) · 2.45 KB
/
admin_search_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'digest/md5'
# The controller for seaching for and surfacing of internal admin data.
class AdminSearchController < ApplicationController
before_action :authenticate_user!
before_action :require_admin
check_authorization
MAX_PAGE_SIZE = 50
def find_students
SeamlessDatabasePool.use_persistent_read_connection do
users = User.with_deleted
# If requested, filter...
if params[:studentNameFilter].present?
users = users.where("name LIKE ?", "%#{params[:studentNameFilter]}%")
end
if params[:studentEmailFilter].present?
hashed_email = User.hash_email params[:studentEmailFilter]
users = users.where(hashed_email: hashed_email)
end
if params[:teacherNameFilter].present? || params[:teacherEmailFilter].present?
teachers = User.
where("name LIKE ?", "%#{params[:teacherNameFilter]}%").
where("email LIKE ?", "%#{params[:teacherEmailFilter]}%").
all
if teachers.count > 1
flash[:alert] = 'Multiple teachers matched the name and email search criteria.'
end
if teachers.first
array_of_student_ids = teachers.first.students.pluck(:id)
users = users.where(id: array_of_student_ids)
end
end
if params[:sectionFilter].present?
section = Section.with_deleted.find_by_code params[:sectionFilter]
if section.nil?
flash[:alert] = 'Section not found.'
elsif section.deleted?
flash[:alert] = 'Section is deleted.'
end
if section
array_of_student_ids = section.students.pluck(:id)
users = users.where(id: array_of_student_ids)
end
end
@users = users.page(params[:page]).per(MAX_PAGE_SIZE)
end
end
def lookup_section
@section = Section.with_deleted.find_by_code params[:section_code]
if params[:section_code] && @section.nil?
flash[:alert] = 'Section code not found'
end
if params[:section_code] && @section.try(:deleted?)
flash[:alert] = 'Section is deleted'
end
end
def undelete_section
section = Section.with_deleted.find_by_code params[:section_code]
if section.try(:deleted?)
section.restore(recursive: true, recovery_window: 5.minutes)
flash[:alert] = "Section (CODE: #{params[:section_code]}) undeleted!"
else
flash[:alert] = "Section (CODE: #{params[:section_code]}) not found or undeleted."
end
redirect_to :lookup_section
end
end