Enable search by name/email for Moderators
This commit is contained in:
@@ -6,16 +6,10 @@ class Admin::ModeratorsController < Admin::BaseController
|
||||
end
|
||||
|
||||
def search
|
||||
@user = User.find_by(email: params[:email])
|
||||
|
||||
respond_to do |format|
|
||||
if @user
|
||||
@moderator = Moderator.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(:moderator)
|
||||
.page(params[:page])
|
||||
.for_render
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<div class="small-12 column">
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<%= moderator.name %>
|
||||
</td>
|
||||
<td>
|
||||
<%= moderator.email %>
|
||||
</td>
|
||||
<td class="text-right">
|
||||
<% if moderator.persisted? %>
|
||||
<%= link_to t('admin.moderators.moderator.delete'),
|
||||
admin_moderator_path(moderator),
|
||||
method: :delete,
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t('admin.moderators.moderator.add'),{ controller: "admin/moderators", action: :create, user_id: moderator.user_id },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -2,30 +2,15 @@
|
||||
|
||||
<h2 class="inline-block"><%= t("admin.moderators.index.title") %></h2>
|
||||
|
||||
<div class="row">
|
||||
<%= form_tag search_admin_moderators_path, method: :get, remote: true do %>
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= text_field_tag :email, '', placeholder: t('admin.moderators.search.email_placeholder') %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= submit_tag t('admin.moderators.search.search'), class: 'button' %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div id="search-result" class="row"></div>
|
||||
<%= render 'admin/shared/user_search', url: search_admin_moderators_path %>
|
||||
|
||||
<h3><%= page_entries_info @moderators %></h3>
|
||||
|
||||
<table id="moderators">
|
||||
<% @moderators.each do |moderator| %>
|
||||
<tr>
|
||||
<td>
|
||||
<%= moderator.name %>
|
||||
</td>
|
||||
<td>
|
||||
<%= moderator.email %>
|
||||
</td>
|
||||
<td><%= moderator.name %></td>
|
||||
<td><%= moderator.email %></td>
|
||||
<td class="text-right">
|
||||
<% if moderator.persisted? %>
|
||||
<%= link_to t('admin.moderators.moderator.delete'),
|
||||
@@ -35,7 +20,8 @@
|
||||
%>
|
||||
<% else %>
|
||||
<%= link_to t('admin.moderators.moderator.add'),
|
||||
{ controller: "admin/moderators", action: :create,
|
||||
{ controller: "admin/moderators",
|
||||
action: :create,
|
||||
user_id: moderator.user_id },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
|
||||
29
app/views/admin/moderators/search.html.erb
Normal file
29
app/views/admin/moderators/search.html.erb
Normal file
@@ -0,0 +1,29 @@
|
||||
<h2><%= t("admin.moderators.search.title") %></h2>
|
||||
|
||||
<%= render 'admin/shared/user_search', url: search_admin_moderators_path %>
|
||||
|
||||
<h3><%= page_entries_info @users %></h3>
|
||||
|
||||
<table id="moderators">
|
||||
<% @users.each do |user| %>
|
||||
<tr>
|
||||
<td><%= user.name %></td>
|
||||
<td><%= user.email %></td>
|
||||
<td class="text-right">
|
||||
<% if user.moderator? && user.moderator.persisted? %>
|
||||
<%= link_to t('admin.moderators.moderator.delete'),
|
||||
admin_moderator_path(user),
|
||||
method: :delete,
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t('admin.moderators.moderator.add'),
|
||||
{ controller: "admin/moderators",
|
||||
action: :create,
|
||||
user_id: user },
|
||||
method: :post,
|
||||
class: "button success" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
@@ -1 +0,0 @@
|
||||
$("#search-result").html("<%= j render 'moderator', moderator: @moderator %>");
|
||||
@@ -1 +0,0 @@
|
||||
$("#search-result").html("<div class=\"small-12 column\"><div class=\"callout alert\"><%= j t('admin.moderators.search.user_not_found') %></div></div>");
|
||||
@@ -446,9 +446,7 @@ en:
|
||||
add: Add
|
||||
delete: Delete
|
||||
search:
|
||||
email_placeholder: Search user by email
|
||||
search: Search
|
||||
user_not_found: User not found
|
||||
title: 'Moderators: User search'
|
||||
newsletters:
|
||||
index:
|
||||
title: Newsletters
|
||||
|
||||
@@ -446,9 +446,7 @@ es:
|
||||
add: Añadir como Moderador
|
||||
delete: Borrar
|
||||
search:
|
||||
email_placeholder: Buscar usuario por email
|
||||
search: Buscar
|
||||
user_not_found: Usuario no encontrado
|
||||
title: 'Moderadores: Búsqueda de usuarios'
|
||||
newsletters:
|
||||
index:
|
||||
title: Envío de newsletters
|
||||
|
||||
@@ -16,7 +16,7 @@ feature 'Admin moderators' do
|
||||
end
|
||||
|
||||
scenario 'Create Moderator', :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
|
||||
@@ -33,5 +33,53 @@ feature 'Admin moderators' do
|
||||
expect(page).to_not have_content @moderator.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'Search' do
|
||||
|
||||
background do
|
||||
user = create(:user, username: 'Elizabeth Bathory', email: 'elizabeth@bathory.com')
|
||||
user2 = create(:user, username: 'Ada Lovelace', email: 'ada@lovelace.com')
|
||||
@moderator1 = create(:moderator, user: user)
|
||||
@moderator2 = create(:moderator, user: user2)
|
||||
visit admin_moderators_path
|
||||
end
|
||||
|
||||
scenario 'returns no results if search term is empty' do
|
||||
expect(page).to have_content(@moderator1.name)
|
||||
expect(page).to have_content(@moderator2.name)
|
||||
|
||||
fill_in 'name_or_email', with: ' '
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Moderators: User search')
|
||||
expect(page).to have_content('users cannot be found')
|
||||
expect(page).to_not have_content(@moderator1.name)
|
||||
expect(page).to_not have_content(@moderator2.name)
|
||||
end
|
||||
|
||||
scenario 'search by name' do
|
||||
expect(page).to have_content(@moderator1.name)
|
||||
expect(page).to have_content(@moderator2.name)
|
||||
|
||||
fill_in 'name_or_email', with: 'Eliz'
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Moderators: User search')
|
||||
expect(page).to have_content(@moderator1.name)
|
||||
expect(page).to_not have_content(@moderator2.name)
|
||||
end
|
||||
|
||||
scenario 'search by email' do
|
||||
expect(page).to have_content(@moderator1.email)
|
||||
expect(page).to have_content(@moderator2.email)
|
||||
|
||||
fill_in 'name_or_email', with: @moderator2.email
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content('Moderators: User search')
|
||||
expect(page).to have_content(@moderator2.email)
|
||||
expect(page).to_not have_content(@moderator1.email)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user