assigns officers to booths

This commit is contained in:
rgarcia
2016-09-28 18:39:01 +02:00
committed by kikito
parent de330b1c02
commit 5ba72f0fe3
10 changed files with 189 additions and 28 deletions

View File

@@ -8,6 +8,7 @@ class Admin::Poll::BoothsController < Admin::BaseController
end
def show
@officers = Poll::Officer.all
end
def new
@@ -35,7 +36,7 @@ class Admin::Poll::BoothsController < Admin::BaseController
private
def booth_params
params.require(:poll_booth).permit(:name, :location)
params.require(:poll_booth).permit(:name, :location, officer_ids: [])
end
def load_polls

View File

@@ -0,0 +1,7 @@
module OfficersHelper
def officer_label(officer)
truncate([officer.name, officer.email].compact.join(' - '), length: 100)
end
end

View File

@@ -2,6 +2,8 @@ class Poll
class Booth < ActiveRecord::Base
belongs_to :poll
has_many :voters
has_many :officing_booths, dependent: :destroy
has_many :officers, through: :officing_booths
validates :name, presence: true
end

View File

@@ -0,0 +1,6 @@
class Poll
class OfficingBooth < ActiveRecord::Base
belongs_to :officer
belongs_to :booth
end
end

View File

@@ -15,34 +15,32 @@
<h3><%= t("admin.booths.show.officers_list") %></h3>
<!-- If officers in this booth == 0 -->
<div class="callout primary">
<%= t("admin.booths.show.no_officers") %>
</div>
<!-- end -->
<%= link_to t("admin.booths.show.assign_officer"), "#", class: "button success" %>
<% if @booth.officers.empty? %>
<div class="callout primary">
<%= t("admin.booths.show.no_officers") %>
</div>
<% end %>
<div class="small-12 column">
<!-- List of officers, something like:
"/admin/spending_proposals/_assigned_valuators.html.erb" -->
<%# f.label :valuator_ids, t("admin.spending_proposals.edit.assigned_valuators") %>
<%= form_for @booth, url: admin_poll_booth_path(@poll, @booth) do |f| %>
<%# f.collection_check_boxes :valuator_ids, @valuators, :id, :email do |b| %>
<%# b.label(title: valuator_label(b.object)) { b.check_box + truncate(b.object.description_or_email, length: 60) } %>
<%# end %>
<%= f.label :officer_ids, t("admin.spending_proposals.edit.assigned_valuators") %>
<%= f.collection_check_boxes :officer_ids, @officers, :id, :email do |b| %>
<% b.label { b.check_box + truncate(officer_label(b.object), length: 60) } %>
<% end %>
<%= f.submit t("admin.booths.show.assign_officer"), class: "button success" %>
<% end %>
</div>
<table>
<%# @officers.each do |officer| %>
<tr>
<table id="assigned_officers">
<% @booth.officers.each do |officer| %>
<tr id="officer_<%= officer.id %>" class="officer">
<td>
<%# officer.name %>
Admin
<%= officer.name %>
</td>
<td>
admin@consul.dev
<%# officer.email %>
<%= officer.email %>
</td>
<td class="text-right">
<%= link_to t('admin.poll_officers.officer.delete'), "#", class: "button hollow alert" %>
@@ -53,5 +51,5 @@
%>
</td>
</tr>
<%# end %>
<% end %>
</table>

View File

@@ -208,7 +208,7 @@ en:
location: "Location"
assign_officer: "Assign officer"
officers_list: "List of officers"
no_officers: "There is no officers in this booth."
no_officers: "There are no officers assigned to this booth"
officials:
edit:
destroy: Remove 'Official' status

View File

@@ -0,0 +1,9 @@
class CreateOfficingBooths < ActiveRecord::Migration
def change
create_table :poll_officing_booths do |t|
t.belongs_to :officer
t.belongs_to :booth
t.timestamps null: false
end
end
end

View File

@@ -280,8 +280,11 @@ ActiveRecord::Schema.define(version: 20161102133838) do
t.integer "user_id"
end
create_table "poll_officers", force: :cascade do |t|
t.integer "user_id"
create_table "poll_officing_booths", force: :cascade do |t|
t.integer "officer_id"
t.integer "booth_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "poll_voters", force: :cascade do |t|

View File

@@ -267,6 +267,18 @@ FactoryGirl.define do
sequence(:name) { |n| "Poll #{n}" }
end
<<<<<<< HEAD
=======
factory :poll_officer, class: 'Poll::Officer' do
user
end
factory :officing_booth, class: 'Poll::OfficingBooth' do
association :officer, factory: :poll_officer
association :booth, factory: :poll_booth
end
>>>>>>> assigns officers to booths
factory :poll_booth, class: 'Poll::Booth' do
sequence(:name) { |n| "Booth #{n}" }
sequence(:location) { |n| "Street #{n}" }

View File

@@ -1,12 +1,13 @@
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
visit admin_officers_path
end
scenario 'Index' do
@@ -15,7 +16,7 @@ feature 'Admin poll officers' do
expect(page).to_not have_content @user.name
end
scenario 'Create poll officer', :js do
scenario 'Create', :js do
fill_in 'email', with: @user.email
click_button 'Search'
@@ -26,11 +27,133 @@ feature 'Admin poll officers' do
end
end
scenario 'Delete poll officer' do
scenario 'Delete' do
click_link 'Delete'
within("#officers") do
expect(page).to_not have_content @officer.name
end
end
context "Booth" do
scenario 'No officers assigned to booth' do
poll = create(:poll)
booth = create(:poll_booth, poll: poll)
visit admin_poll_booth_path(poll, booth)
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 0
end
expect(page).to have_content "There are no officers assigned to this booth"
end
scenario "Assigned to booth" do
john = create(:poll_officer)
isabel = create(:poll_officer)
eve = create(:poll_officer)
poll = create(:poll)
booth = create(:poll_booth, poll: poll)
officing_booth1 = create(:officing_booth, officer: john, booth: booth)
officing_booth2 = create(:officing_booth, officer: isabel, booth: booth)
visit admin_poll_booth_path(poll, booth)
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 2
expect(page).to have_content john.name
expect(page).to have_content isabel.name
expect(page).to_not have_content eve.name
end
expect(page).to_not have_content "There are no officers assigned to this booth"
end
scenario 'Assign to booth' do
john = create(:poll_officer)
isabel = create(:poll_officer)
poll = create(:poll)
booth = create(:poll_booth, poll: poll)
visit admin_poll_booth_path(poll, booth)
check "#{john.name} - #{john.email}"
click_button "Assign officer"
expect(page).to have_content "Booth updated successfully."
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 1
expect(page).to have_content john.name
end
end
scenario "Unassign from booth" do
john = create(:poll_officer)
isabel = create(:poll_officer)
poll = create(:poll)
booth = create(:poll_booth, poll: poll)
officing_booth = create(:officing_booth, officer: john, booth: booth)
officing_booth = create(:officing_booth, officer: isabel, booth: booth)
visit admin_poll_booth_path(poll, booth)
uncheck "#{john.name} - #{john.email}"
click_button "Assign officer"
expect(page).to have_content "Booth updated successfully."
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 1
expect(page).to have_content isabel.name
expect(page).to_not have_content john.name
end
end
scenario "Assigned multiple officers to different booths" do
john = create(:poll_officer)
isabel = create(:poll_officer)
eve = create(:poll_officer)
peter = create(:poll_officer)
poll1 = create(:poll)
poll2 = create(:poll)
booth1 = create(:poll_booth, poll: poll1)
booth2 = create(:poll_booth, poll: poll1)
booth3 = create(:poll_booth, poll: poll2)
officing_booth = create(:officing_booth, officer: john, booth: booth1)
officing_booth = create(:officing_booth, officer: isabel, booth: booth1)
officing_booth = create(:officing_booth, officer: eve, booth: booth2)
officing_booth = create(:officing_booth, officer: peter, booth: booth3)
visit admin_poll_booth_path(poll1, booth1)
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 2
expect(page).to have_content john.name
expect(page).to have_content isabel.name
end
visit admin_poll_booth_path(poll1, booth2)
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 1
expect(page).to have_content eve.name
end
visit admin_poll_booth_path(poll2, booth3)
within("#assigned_officers") do
expect(page).to have_css ".officer", count: 1
expect(page).to have_content peter.name
end
end
end
end