adds edition/management of officials to admin zone

This commit is contained in:
Juanjo Bazán
2015-08-17 17:04:29 +02:00
parent 635dfba418
commit fabcb42328
12 changed files with 230 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
class Admin::OfficialsController < Admin::BaseController
def index
@officials = User.officials.page(params[:page])
end
def search
@users = User.with_email(params[:email]).page(params[:page])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
@user.update(user_params)
redirect_to admin_officials_path, notice: t("admin.officials.flash.official_updated")
end
def destroy
@official = User.officials.find(params[:id])
@official.remove_official_position!
redirect_to admin_officials_path, notice: t("admin.officials.flash.official_destroyed")
end
private
def user_params
params.require(:user).permit(:official_position, :official_level)
end
end

View File

@@ -4,6 +4,10 @@ module AdminHelper
render "/#{namespace}/menu" render "/#{namespace}/menu"
end end
def official_level_options
1..5
end
private private
def namespace def namespace

View File

@@ -41,4 +41,8 @@ class User < ActiveRecord::Base
def remove_official_position! def remove_official_position!
update official_position: nil, official_level: 0 update official_position: nil, official_level: 0
end end
def self.with_email(e)
e.present? ? where(email: e) : none
end
end end

View File

@@ -2,4 +2,5 @@
<li><%= link_to t('admin.menu.debate_topics'), admin_tags_path %></li> <li><%= link_to t('admin.menu.debate_topics'), admin_tags_path %></li>
<li><%= link_to t('admin.menu.hidden_debates'), admin_debates_path %></li> <li><%= link_to t('admin.menu.hidden_debates'), admin_debates_path %></li>
<li><%= link_to t('admin.menu.hidden_comments'), admin_comments_path %></li> <li><%= link_to t('admin.menu.hidden_comments'), admin_comments_path %></li>
<li><%= link_to t('admin.officials.index.title'), admin_officials_path %></li>
</ul> </ul>

View File

@@ -0,0 +1,14 @@
<h1><%= t("admin.officials.edit.title") %></h1>
<%= @user.name %> (<%= @user.email %>)
<%= form_for @user, url: admin_official_path(@user) do |f| %>
<%= f.text_field :official_position %>
<%= f.select :official_level, official_level_options %>
<%= f.submit %>
<% if @user.official? %>
<%= link_to t("admin.officials.edit.destroy"), admin_official_path(@user), method: :delete, class: 'button tiny alert' %>
<% else %>
<%= link_to t("admin.officials.edit.cancel"), admin_officials_path, class: 'button tiny alert' %>
<% end %>
<% end %>

View File

@@ -0,0 +1,25 @@
<h1><%= t("admin.officials.index.title") %></h1>
<div>
<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
<%= text_field_tag :email, "", label: false, placeholder: t("admin.officials.index.search_email_placeholder") %>
<%= f.submit t("admin.officials.index.search") %>
<% end %>
</div>
<div>
<%= page_entries_info @officials %>
</div>
<div>
<% @officials.each do |official| %>
<%= link_to official.name, edit_admin_official_path(official) %>
<%= official.official_position %>
<%= official.official_level %>
<br/><br/>
<% end %>
</div>
<div>
<%= paginate @officials %>
</div>

View File

@@ -0,0 +1,21 @@
<h1><%= t("admin.officials.search.title") %></h1>
<div>
<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %>
<%= text_field_tag :email, "", label: false, placeholder: t("admin.officials.index.search_email_placeholder") %>
<%= f.submit t("admin.officials.search.search") %>
<% end %>
</div>
<div>
<%= page_entries_info @users %>
</div>
<div>
<% @users.each do |user| %>
<%= link_to user.name, edit_admin_official_path(user) %>
<%= user.official_position %>
<%= user.official_level %>
<%= link_to user.official? ? t("admin.officials.search.edit_official") : t("admin.officials.search.make_official"), edit_admin_official_path(user) %>
<% end %>
</div>

View File

@@ -29,4 +29,20 @@ en:
title: Hidden debates title: Hidden debates
restore: restore:
success: The debate has been restored success: The debate has been restored
officials:
index:
title: Officials
search_email_placeholder: 'Search user by email'
search: Search
search:
title: 'Officials: Search users'
edit_official: Edit official
make_official: Make this user an official
search: Search
edit:
title: 'Officials: edit user'
destroy: "Remove 'Official' condition"
cancel: "Cancel"
flash:
official_updated: 'Official position saved!'
official_destroyed: 'User is not an official anymore'

View File

@@ -29,3 +29,20 @@ es:
title: Debates ocultos title: Debates ocultos
restore: restore:
success: El debate ha sido permitido success: El debate ha sido permitido
officials:
index:
title: Cargos Públicos
search_email_placeholder: 'Buscar usuario por email'
search: Buscar
search:
title: 'Cargos Públicos: Búsqueda de usuarios'
edit_official: Editar cargo público
make_official: Convertir en cargo público
search: Buscar
edit:
title: 'Cargos Públicos: Editar usuario'
destroy: "Eliminar condición de 'Cargo Público'"
cancel: "Cancelar"
flash:
official_updated: 'Datos del cargo público guardados'
official_destroyed: 'Datos guardados: el usuario ya no es cargo público'

View File

@@ -29,6 +29,9 @@ Rails.application.routes.draw do
end end
resources :tags, only: [:index, :create, :update, :destroy] resources :tags, only: [:index, :create, :update, :destroy]
resources :officials, only: [:index, :edit, :update, :destroy] do
collection { get :search}
end
end end
namespace :moderation do namespace :moderation do

View File

@@ -0,0 +1,78 @@
require 'rails_helper'
feature 'Admin officials' do
background do
@citizen = create(:user, first_name: "Citizen", last_name: "Kane")
@official = create(:user, official_position: "Mayor", official_level: 5)
@admin = create(:administrator)
login_as(@admin.user)
end
scenario 'Index' do
visit admin_officials_path
expect(page).to have_content @official.name
expect(page).to_not have_content @citizen.name
expect(page).to have_content @official.official_position
expect(page).to have_content @official.official_level
end
scenario 'Edit an official' do
visit admin_officials_path
click_link @official.name
expect(current_path).to eq(edit_admin_official_path(@official))
expect(page).to_not have_content @citizen.name
expect(page).to have_content @official.name
expect(page).to have_content @official.email
fill_in 'user_official_position', with: 'School Teacher'
select '3', from: 'user_official_level'
click_button 'Update User'
expect(page).to have_content 'Official position saved!'
visit admin_officials_path
expect(page).to have_content @official.name
expect(page).to have_content 'School Teacher'
expect(page).to have_content '3'
end
scenario 'Create an official' do
visit admin_officials_path
fill_in 'email', with: @citizen.email
click_button 'Search'
expect(current_path).to eq(search_admin_officials_path)
expect(page).to_not have_content @official.name
click_link @citizen.name
fill_in 'user_official_position', with: 'Hospital manager'
select '4', from: 'user_official_level'
click_button 'Update User'
expect(page).to have_content 'Official position saved!'
visit admin_officials_path
expect(page).to have_content @official.name
expect(page).to have_content @citizen.name
expect(page).to have_content 'Hospital manager'
expect(page).to have_content '4'
end
scenario 'Destroy' do
visit edit_admin_official_path(@official)
click_link "Remove 'Official' condition"
expect(page).to have_content 'User is not an official anymore'
expect(current_path).to eq(admin_officials_path)
expect(page).to_not have_content @citizen.name
expect(page).to_not have_content @official.name
end
end

View File

@@ -169,4 +169,18 @@ describe User do
end end
end end
describe "self.with_email" do
it "find users by email" do
user1 = create(:user, email: "larry@madrid.es")
user2 = create(:user, email: "bird@madrid.es")
search = User.with_email("larry@madrid.es")
expect(search.size).to eq(1)
expect(search.first).to eq(user1)
end
it "returns no results if no email provided" do
expect(User.with_email(" ").size).to eq(0)
end
end
end end