Enable search by name/email for Administrators
This commit is contained in:
@@ -6,16 +6,10 @@ class Admin::AdministratorsController < Admin::BaseController
|
||||
end
|
||||
|
||||
def search
|
||||
@user = User.find_by(email: params[:email])
|
||||
|
||||
respond_to do |format|
|
||||
if @user
|
||||
@administrator = Administrator.find_or_initialize_by(user: @user)
|
||||
format.js
|
||||
else
|
||||
format.js { render "user_not_found" }
|
||||
end
|
||||
end
|
||||
@users = User.search(params[:name_or_email])
|
||||
.includes(:administrator)
|
||||
.page(params[:page])
|
||||
.for_render
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<div class="small-12 column">
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<%= administrator.name %>
|
||||
</td>
|
||||
<td>
|
||||
<%= administrator.email %>
|
||||
</td>
|
||||
<td class="text-right">
|
||||
<% if administrator.persisted? %>
|
||||
<%= link_to t('admin.administrators.administrator.delete'),
|
||||
admin_administrator_path(administrator),
|
||||
method: :delete,
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t('admin.administrators.administrator.add'),{ controller: "admin/administrators", action: :create, user_id: administrator.user_id },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -1,39 +1,24 @@
|
||||
<h2 class="inline-block"><%= t("admin.administrators.index.title") %></h2>
|
||||
|
||||
<div class="row">
|
||||
<%= form_tag search_admin_administrators_path, method: :get, remote: true do %>
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= text_field_tag :email, '', placeholder: t('admin.administrators.search.email_placeholder') %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= submit_tag t('admin.administrators.search.search'), class: 'button' %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div id="search-result" class="row"></div>
|
||||
<%= render 'admin/shared/user_search', url: search_admin_administrators_path %>
|
||||
|
||||
<h3><%= page_entries_info @administrators %></h3>
|
||||
|
||||
<table id="administrators">
|
||||
<% @administrators.each do |administrator| %>
|
||||
<tr>
|
||||
<td>
|
||||
<%= administrator.name %>
|
||||
</td>
|
||||
<td>
|
||||
<%= administrator.email %>
|
||||
</td>
|
||||
<td><%= administrator.name %></td>
|
||||
<td><%= administrator.email %></td>
|
||||
<td class="text-right">
|
||||
<% if administrator.persisted? %>
|
||||
<%= link_to t('admin.administrators.administrator.delete'),
|
||||
admin_administrator_path(administrator),
|
||||
method: :delete,
|
||||
class: "button hollow alert"
|
||||
%>
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t('admin.administrators.administrator.add'),
|
||||
{ controller: "admin/administrators", action: :create,
|
||||
{ controller: "admin/administrators",
|
||||
action: :create,
|
||||
user_id: administrator.user_id },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
|
||||
29
app/views/admin/administrators/search.html.erb
Normal file
29
app/views/admin/administrators/search.html.erb
Normal file
@@ -0,0 +1,29 @@
|
||||
<h2><%= t("admin.administrators.search.title") %></h2>
|
||||
|
||||
<%= render 'admin/shared/user_search', url: search_admin_administrators_path %>
|
||||
|
||||
<h3><%= page_entries_info @users %></h3>
|
||||
|
||||
<table id="administrators">
|
||||
<% @users.each do |user| %>
|
||||
<tr>
|
||||
<td><%= user.name %></td>
|
||||
<td><%= user.email %></td>
|
||||
<td class="text-right">
|
||||
<% if user.administrator? && user.administrator.persisted? %>
|
||||
<%= link_to t('admin.administrators.administrator.delete'),
|
||||
admin_administrator_path(user),
|
||||
method: :delete,
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t('admin.administrators.administrator.add'),
|
||||
{ controller: "admin/administrators",
|
||||
action: :create,
|
||||
user_id: user },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
@@ -1 +0,0 @@
|
||||
$("#search-result").html("<%= j render 'administrator', administrator: @administrator %>");
|
||||
@@ -1 +0,0 @@
|
||||
$("#search-result").html("<div class=\"small-12 column\"><div class=\"callout alert\"><%= j t('admin.administrators.search.user_not_found') %></div></div>");
|
||||
@@ -438,9 +438,7 @@ en:
|
||||
delete: Delete
|
||||
restricted_removal: "Sorry, you can't remove yourself from the administrators"
|
||||
search:
|
||||
email_placeholder: Search user by email
|
||||
search: Search
|
||||
user_not_found: User not found
|
||||
title: 'Administrators: User search'
|
||||
moderators:
|
||||
index:
|
||||
title: Moderators
|
||||
|
||||
@@ -387,9 +387,7 @@ es:
|
||||
delete: Borrar
|
||||
restricted_removal: "Lo sentimos, no puedes eliminarte a ti mismo de la lista"
|
||||
search:
|
||||
email_placeholder: Buscar usuario por email
|
||||
search: Buscar
|
||||
user_not_found: Usuario no encontrado
|
||||
title: 'Administradores: Búsqueda de usuarios'
|
||||
managers:
|
||||
index:
|
||||
title: Gestores
|
||||
|
||||
@@ -16,7 +16,7 @@ feature 'Admin administrators' do
|
||||
end
|
||||
|
||||
scenario 'Create Administrator', :js do
|
||||
fill_in 'email', with: @user.email
|
||||
fill_in 'name_or_email', with: @user.email
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content @user.name
|
||||
@@ -41,5 +41,53 @@ feature 'Admin administrators' do
|
||||
expect(page).to have_content I18n.t("admin.administrators.administrator.restricted_removal")
|
||||
end
|
||||
end
|
||||
|
||||
context 'Search' do
|
||||
|
||||
background do
|
||||
user = create(:user, username: 'Bernard Sumner', email: 'bernard@sumner.com')
|
||||
user2 = create(:user, username: 'Tony Soprano', email: 'tony@soprano.com')
|
||||
@administrator1 = create(:administrator, user: user)
|
||||
@administrator2 = create(:administrator, user: user2)
|
||||
visit admin_administrators_path
|
||||
end
|
||||
|
||||
scenario 'returns no results if search term is empty' do
|
||||
expect(page).to have_content(@administrator1.name)
|
||||
expect(page).to have_content(@administrator2.name)
|
||||
|
||||
fill_in 'name_or_email', with: ' '
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Administrators: User search')
|
||||
expect(page).to have_content('users cannot be found')
|
||||
expect(page).to_not have_content(@administrator1.name)
|
||||
expect(page).to_not have_content(@administrator2.name)
|
||||
end
|
||||
|
||||
scenario 'search by name' do
|
||||
expect(page).to have_content(@administrator1.name)
|
||||
expect(page).to have_content(@administrator2.name)
|
||||
|
||||
fill_in 'name_or_email', with: 'Sumn'
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Administrators: User search')
|
||||
expect(page).to have_content(@administrator1.name)
|
||||
expect(page).to_not have_content(@administrator2.name)
|
||||
end
|
||||
|
||||
scenario 'search by email' do
|
||||
expect(page).to have_content(@administrator1.email)
|
||||
expect(page).to have_content(@administrator2.email)
|
||||
|
||||
fill_in 'name_or_email', with: @administrator2.email
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Administrators: User search')
|
||||
expect(page).to have_content(@administrator2.email)
|
||||
expect(page).to_not have_content(@administrator1.email)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user