Merge pull request #1560 from bertocq/feature/admin_administrators_management
Administrators management in admin area
This commit is contained in:
37
app/controllers/admin/administrators_controller.rb
Normal file
37
app/controllers/admin/administrators_controller.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class Admin::AdministratorsController < Admin::BaseController
|
||||
load_and_authorize_resource
|
||||
|
||||
def index
|
||||
@administrators = @administrators.page(params[:page])
|
||||
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
|
||||
end
|
||||
|
||||
def create
|
||||
@administrator.user_id = params[:user_id]
|
||||
@administrator.save
|
||||
|
||||
redirect_to admin_administrators_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
if current_user.id == @administrator.user_id
|
||||
flash[:error] = I18n.t("admin.administrators.administrator.restricted_removal")
|
||||
else
|
||||
@administrator.destroy
|
||||
end
|
||||
|
||||
redirect_to admin_administrators_path
|
||||
end
|
||||
end
|
||||
@@ -29,7 +29,7 @@ module AdminHelper
|
||||
end
|
||||
|
||||
def menu_profiles?
|
||||
["organizations", "officials", "moderators", "valuators", "managers"].include? controller_name
|
||||
["administrators", "organizations", "officials", "moderators", "valuators", "managers"].include? controller_name
|
||||
end
|
||||
|
||||
def menu_banners?
|
||||
|
||||
@@ -34,6 +34,7 @@ module Abilities
|
||||
|
||||
can :comment_as_administrator, [Debate, Comment, Proposal, Poll::Question, Budget::Investment]
|
||||
|
||||
can [:search, :create, :index, :destroy], ::Administrator
|
||||
can [:search, :create, :index, :destroy], ::Moderator
|
||||
can [:search, :create, :index, :summary], ::Valuator
|
||||
can [:search, :create, :index, :destroy], ::Manager
|
||||
|
||||
@@ -95,6 +95,10 @@
|
||||
<strong><%= t("admin.menu.title_profiles") %></strong>
|
||||
</a>
|
||||
<ul <%= "class=is-active" if menu_profiles? %>>
|
||||
<li <%= "class=active" if controller_name == "administrators" %>>
|
||||
<%= link_to t("admin.menu.administrators"), admin_administrators_path %>
|
||||
</li>
|
||||
|
||||
<li <%= "class=active" if controller_name == "organizations" %>>
|
||||
<%= link_to t("admin.menu.organizations"), admin_organizations_path %>
|
||||
</li>
|
||||
|
||||
26
app/views/admin/administrators/_administrator.html.erb
Normal file
26
app/views/admin/administrators/_administrator.html.erb
Normal file
@@ -0,0 +1,26 @@
|
||||
<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>
|
||||
46
app/views/admin/administrators/index.html.erb
Normal file
46
app/views/admin/administrators/index.html.erb
Normal file
@@ -0,0 +1,46 @@
|
||||
<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>
|
||||
|
||||
<h3><%= page_entries_info @administrators %></h3>
|
||||
|
||||
<table id="administrators">
|
||||
<% @administrators.each do |administrator| %>
|
||||
<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>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<%= paginate @administrators %>
|
||||
1
app/views/admin/administrators/search.js.erb
Normal file
1
app/views/admin/administrators/search.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
$("#search-result").html("<%= j render 'administrator', administrator: @administrator %>");
|
||||
1
app/views/admin/administrators/user_not_found.js.erb
Normal file
1
app/views/admin/administrators/user_not_found.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
$("#search-result").html("<div class=\"small-12 column\"><div class=\"callout alert\"><%= j t('admin.administrators.search.user_not_found') %></div></div>");
|
||||
@@ -203,6 +203,7 @@ en:
|
||||
hidden_debates: Hidden debates
|
||||
hidden_proposals: Hidden proposals
|
||||
hidden_users: Hidden users
|
||||
administrators: Administrators
|
||||
managers: Managers
|
||||
moderators: Moderators
|
||||
newsletter: Newsletters
|
||||
@@ -227,6 +228,17 @@ en:
|
||||
title_profiles: Profiles
|
||||
title_banners: Banners
|
||||
title_site_customization: Site customization
|
||||
administrators:
|
||||
index:
|
||||
title: Administrators
|
||||
administrator:
|
||||
add: Add
|
||||
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
|
||||
moderators:
|
||||
index:
|
||||
title: Moderators
|
||||
|
||||
@@ -181,6 +181,17 @@ es:
|
||||
with_confirmed_hide: Confirmados
|
||||
without_confirmed_hide: Pendientes
|
||||
title: Debates ocultos
|
||||
administrators:
|
||||
index:
|
||||
title: Administradores
|
||||
administrator:
|
||||
add: Añadir como Administrador
|
||||
delete: Borrar
|
||||
restricted_removal: "Lo sentimos, no puedes te eliminar a ti mismo de la lista"
|
||||
search:
|
||||
email_placeholder: Buscar usuario por email
|
||||
search: Buscar
|
||||
user_not_found: Usuario no encontrado
|
||||
managers:
|
||||
index:
|
||||
title: Gestores
|
||||
@@ -204,6 +215,7 @@ es:
|
||||
hidden_proposals: Propuestas ocultas
|
||||
hidden_users: Usuarios bloqueados
|
||||
managers: Gestores
|
||||
administrators: Administradores
|
||||
moderators: Moderadores
|
||||
newsletter: Envío de Newsletters
|
||||
valuators: Evaluadores
|
||||
|
||||
@@ -207,6 +207,10 @@ Rails.application.routes.draw do
|
||||
get :search, on: :collection
|
||||
end
|
||||
|
||||
resources :administrators, only: [:index, :create, :destroy] do
|
||||
get :search, on: :collection
|
||||
end
|
||||
|
||||
scope module: :poll do
|
||||
resources :polls do
|
||||
get :search_questions, on: :member
|
||||
|
||||
45
spec/features/admin/administrators_spec.rb
Normal file
45
spec/features/admin/administrators_spec.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'Admin administrators' do
|
||||
background do
|
||||
@admin = create(:administrator)
|
||||
@user = create(:user, username: 'Jose Luis Balbin')
|
||||
@administrator = create(:administrator)
|
||||
login_as(@admin.user)
|
||||
visit admin_administrators_path
|
||||
end
|
||||
|
||||
scenario 'Index' do
|
||||
expect(page).to have_content @administrator.name
|
||||
expect(page).to have_content @administrator.email
|
||||
expect(page).to_not have_content @user.name
|
||||
end
|
||||
|
||||
scenario 'Create Administrator', :js do
|
||||
fill_in 'email', with: @user.email
|
||||
click_button 'Search'
|
||||
|
||||
expect(page).to have_content @user.name
|
||||
click_link 'Add'
|
||||
within("#administrators") do
|
||||
expect(page).to have_content @user.name
|
||||
end
|
||||
end
|
||||
|
||||
scenario 'Delete Administrator' do
|
||||
find(:xpath, "//tr[contains(.,'#{@administrator.name}')]/td/a", text: 'Delete').click
|
||||
|
||||
within("#administrators") do
|
||||
expect(page).to_not have_content @administrator.name
|
||||
end
|
||||
end
|
||||
|
||||
scenario 'Delete Administrator when its the current user' do
|
||||
find(:xpath, "//tr[contains(.,'#{@admin.name}')]/td/a", text: 'Delete').click
|
||||
|
||||
within("#error") do
|
||||
expect(page).to have_content I18n.t("admin.administrators.administrator.restricted_removal")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user