Skip to content

Commit 59994c2

Browse files
committed
Implement searching for users
1 parent 35a59fd commit 59994c2

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

app/controllers/users_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class UsersController < ApplicationController
66

77
after_action :remove_x_frame_options_header_when_bare_layout, only: [:new, :create, :show]
88

9+
def index
10+
respond_access_denied unless current_user.administrator?
11+
@users = User.search(params[:search])
12+
end
13+
914
def new
1015
add_breadcrumb 'Sign up', new_user_path
1116

app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ def generate_password_reset_link
253253
url
254254
end
255255

256+
def self.search(query)
257+
return User.none unless query
258+
User.where('lower(email) LIKE ?', "%#{query.strip.downcase}%")
259+
end
260+
256261
private
257262

258263
def course_ids_arel

app/views/layouts/_navigation.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ul class="navbar-nav">
1313
<% if signed_in? %>
1414
<% if current_user.administrator? %>
15+
<li class="nav-item"><%= navigation_link 'Users', users_path %></li>
1516
<li class="nav-item"><%= navigation_link 'Course templates', course_templates_path %></li>
1617
<% end %>
1718
<% end %>

app/views/users/_user.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
<%= link_to user.login, participant_path(user) %>
3+
<%= user.email %>
4+
</div>

app/views/users/index.html.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h1>All users</h1>
2+
3+
<%= form_tag(users_path, method: :get) do %>
4+
<div class="form-group">
5+
<label for="search">Search by email</label>
6+
<%= text_field_tag(:search, params[:search], class: "form-control", id: "search") %>
7+
</div>
8+
<%= submit_tag(:search, class: "btn btn-primary") %>
9+
<% end %>
10+
11+
<br>
12+
13+
<% if @users %>
14+
<%= render @users %>
15+
<% end %>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@
237237
get '/users/:user_id/verify/:id', to: 'users#confirm_email', as: 'confirm_email'
238238
post '/users/:user_id/send_verification_email', to: 'users#send_verification_email', as: 'send_verification_email'
239239

240+
resources :users, only: [:index]
241+
240242
post '/users/:user_id/send_destroy_email', to: 'users#send_destroy_email', as: 'send_destroy_email'
241243
get '/users/:user_id/destroy/:id', to: 'users#verify_destroying_user', as: 'verify_destroying_user'
242244
delete '/users/:user_id/destroy/:id', to: 'users#destroy_user', as: 'destroy_user'

0 commit comments

Comments
 (0)