creates polling officers from admin

This commit is contained in:
rgarcia
2016-09-14 13:53:35 +02:00
committed by kikito
parent f24ced329f
commit 1ca821c520
19 changed files with 215 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
class Admin::Poll::OfficersController < Admin::BaseController
load_and_authorize_resource :officer, class: "Poll::Officer"
def index
@officers = @officers.page(params[:page])
end
def search
@user = User.find_by(email: params[:email])
respond_to do |format|
if @user
@officer = Poll::Officer.find_or_initialize_by(user: @user)
format.js
else
format.js { render "user_not_found" }
end
end
end
def create
@officer.user_id = params[:user_id]
@officer.save
redirect_to admin_poll_officers_path
end
def destroy
@officer.destroy
redirect_to admin_poll_officers_path
end
end

View File

@@ -19,7 +19,7 @@ module AdminHelper
private
def namespace
controller.class.parent.name.downcase
controller.class.parent.name.downcase.gsub("::", "/")
end
end

View File

@@ -38,6 +38,7 @@ module Abilities
can [:search, :create, :index, :destroy], ::Moderator
can [:search, :create, :index, :summary], ::Valuator
can [:search, :create, :index, :destroy], ::Manager
can [:search, :create, :index, :destroy], ::Poll::Officer
can :manage, Annotation

2
app/models/poll.rb Normal file
View File

@@ -0,0 +1,2 @@
class Poll < ActiveRecord::Base
end

View File

@@ -0,0 +1,8 @@
class Poll
class Officer < ActiveRecord::Base
belongs_to :user
delegate :name, :email, to: :user
validates :user_id, presence: true, uniqueness: true
end
end

View File

@@ -83,6 +83,12 @@
<% end %>
</li>
<li <%= 'class=active' if controller_name == 'officers_controller' %>>
<%= link_to admin_poll_officers_path do %>
<span class="icon-user"></span><%= t('admin.menu.poll_officers') %>
<% end %>
</li>
<li <%= 'class=active' if controller_name == 'activity' %>>
<%= link_to admin_activity_path do %>
<span class="icon-eye"></span><%= t('admin.menu.activity') %>

View File

@@ -0,0 +1 @@
<%= render "admin/menu" %>

View File

@@ -0,0 +1,26 @@
<div class="small-12 column">
<table>
<tbody>
<tr>
<td>
<%= officer.name %>
</td>
<td>
<%= officer.email %>
</td>
<td class="text-right">
<% if officer.persisted? %>
<%= link_to t('admin.poll_officers.officer.delete'),
admin_poll_officer_path(officer),
method: :delete,
class: "button hollow alert" %>
<% else %>
<%= link_to t('admin.poll_officers.officer.add'),{ controller: "admin/poll/officers", action: :create, user_id: officer.user_id },
method: :post,
class: "button success" %>
<% end %>
</td>
</tr>
</tbody>
</table>
</div>

View File

@@ -0,0 +1,46 @@
<h2><%= t("admin.poll_officers.index.title") %></h2>
<div class="row">
<%= form_tag search_admin_poll_officers_path, method: :get, remote: true do %>
<div class="small-12 medium-6 column">
<%= text_field_tag :email, '', placeholder: t('admin.poll_officers.search.email_placeholder') %>
</div>
<div class="small-12 medium-6 column">
<%= submit_tag t('admin.poll_officers.search.search'), class: 'button' %>
</div>
<% end %>
</div>
<div id="search-result" class="row"></div>
<h3><%= page_entries_info @officers %></h3>
<table id="officers">
<% @officers.each do |officer| %>
<tr>
<td>
<%= officer.name %>
</td>
<td>
<%= officer.email %>
</td>
<td class="text-right">
<% if officer.persisted? %>
<%= link_to t('admin.poll_officers.officer.delete'),
admin_poll_officer_path(officer),
method: :delete,
class: "button hollow alert"
%>
<% else %>
<%= link_to t('admin.poll_officers.officer.add'),
{ controller: "admin/poll/officers", action: :create,
user_id: officer.user_id },
method: :post,
class: "button success" %>
<% end %>
</td>
</tr>
<% end %>
</table>
<%= paginate @officers %>

View File

@@ -0,0 +1 @@
$("#search-result").html("<%= j render 'officer', officer: @officer %>");

View File

@@ -0,0 +1 @@
$("#search-result").html("<div class=\"small-12 column\"><div class=\"callout alert\"><%= j t('admin.officers.search.user_not_found') %></div></div>");

View File

@@ -105,6 +105,7 @@ en:
managers: Managers
moderators: Moderators
valuators: Valuators
poll_officers: Poll officers
officials: Officials
organizations: Organisations
settings: Configuration settings
@@ -140,6 +141,16 @@ en:
in_evaluation_count: In evaluation
total_count: Total
cost: Cost
poll_officers:
index:
title: Poll officers
officer:
add: Add
delete: Delete
search:
email_placeholder: Search user by email
search: Search
user_not_found: User not found
officials:
edit:
destroy: Remove 'Official' status

View File

@@ -103,6 +103,7 @@ es:
managers: Gestores
moderators: Moderadores
valuators: Evaluadores
poll_officers: Presidentes de mesa
officials: Cargos públicos
organizations: Organizaciones
settings: Configuración global
@@ -138,6 +139,16 @@ es:
in_evaluation_count: En evaluación
total_count: Total
cost: Coste total
poll_officers:
index:
title: Presidentes de mesa
officer:
add: Añadir como Presidente de mesa
delete: Borrar
search:
email_placeholder: Buscar usuario por email
search: Buscar
user_not_found: Usuario no encontrado
officials:
edit:
destroy: Eliminar condición de 'Cargo Público'

View File

@@ -179,6 +179,12 @@ Rails.application.routes.draw do
get :search, on: :collection
end
namespace :poll do
resources :officers, only: [:index, :create, :destroy] do
get :search, on: :collection
end
end
resources :verifications, controller: :verifications, only: :index do
get :search, on: :collection
end

View File

@@ -0,0 +1,7 @@
class CreatePolls < ActiveRecord::Migration
def change
create_table :polls do |t|
t.string :name
end
end
end

View File

@@ -0,0 +1,7 @@
class CreatePollOfficers < ActiveRecord::Migration
def change
create_table :poll_officers do |t|
t.integer :user_id
end
end
end

View File

@@ -270,6 +270,14 @@ ActiveRecord::Schema.define(version: 20161102133838) do
add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree
create_table "poll_officers", force: :cascade do |t|
t.integer "user_id"
end
create_table "polls", force: :cascade do |t|
t.string "name"
end
create_table "proposal_notifications", force: :cascade do |t|
t.string "title"
t.text "body"

View File

@@ -263,6 +263,10 @@ FactoryGirl.define do
user
end
factory :poll_officer, class: 'Poll::Officer' do
user
end
factory :organization do
user
responsible_name "Johnny Utah"

View File

@@ -0,0 +1,36 @@
require 'rails_helper'
feature 'Admin poll officers' do
background do
@admin = create(:administrator)
@user = create(:user, username: 'Pedro Jose Garcia')
@officer = create(:poll_officer)
login_as(@admin.user)
visit admin_poll_officers_path
end
scenario 'Index' do
expect(page).to have_content @officer.name
expect(page).to have_content @officer.email
expect(page).to_not have_content @user.name
end
scenario 'Create poll officer', :js do
fill_in 'email', with: @user.email
click_button 'Search'
expect(page).to have_content @user.name
click_link 'Add'
within("#officers") do
expect(page).to have_content @user.name
end
end
scenario 'Delete poll officer' do
click_link 'Delete'
within("#officers") do
expect(page).to_not have_content @officer.name
end
end
end