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 end
def show def show
@officers = Poll::Officer.all
end end
def new def new
@@ -35,7 +36,7 @@ class Admin::Poll::BoothsController < Admin::BaseController
private private
def booth_params def booth_params
params.require(:poll_booth).permit(:name, :location) params.require(:poll_booth).permit(:name, :location, officer_ids: [])
end end
def load_polls 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 class Booth < ActiveRecord::Base
belongs_to :poll belongs_to :poll
has_many :voters has_many :voters
has_many :officing_booths, dependent: :destroy
has_many :officers, through: :officing_booths
validates :name, presence: true validates :name, presence: true
end 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> <h3><%= t("admin.booths.show.officers_list") %></h3>
<!-- If officers in this booth == 0 --> <% if @booth.officers.empty? %>
<div class="callout primary"> <div class="callout primary">
<%= t("admin.booths.show.no_officers") %> <%= t("admin.booths.show.no_officers") %>
</div> </div>
<!-- end --> <% end %>
<%= link_to t("admin.booths.show.assign_officer"), "#", class: "button success" %>
<div class="small-12 column"> <div class="small-12 column">
<!-- List of officers, something like: <%= form_for @booth, url: admin_poll_booth_path(@poll, @booth) do |f| %>
"/admin/spending_proposals/_assigned_valuators.html.erb" -->
<%# f.label :valuator_ids, t("admin.spending_proposals.edit.assigned_valuators") %>
<%# f.collection_check_boxes :valuator_ids, @valuators, :id, :email do |b| %> <%= f.label :officer_ids, t("admin.spending_proposals.edit.assigned_valuators") %>
<%# b.label(title: valuator_label(b.object)) { b.check_box + truncate(b.object.description_or_email, length: 60) } %> <%= f.collection_check_boxes :officer_ids, @officers, :id, :email do |b| %>
<%# end %> <% 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> </div>
<table> <table id="assigned_officers">
<%# @officers.each do |officer| %> <% @booth.officers.each do |officer| %>
<tr> <tr id="officer_<%= officer.id %>" class="officer">
<td> <td>
<%# officer.name %> <%= officer.name %>
Admin
</td> </td>
<td> <td>
admin@consul.dev <%= officer.email %>
<%# officer.email %>
</td> </td>
<td class="text-right"> <td class="text-right">
<%= link_to t('admin.poll_officers.officer.delete'), "#", class: "button hollow alert" %> <%= link_to t('admin.poll_officers.officer.delete'), "#", class: "button hollow alert" %>
@@ -53,5 +51,5 @@
%> %>
</td> </td>
</tr> </tr>
<%# end %> <% end %>
</table> </table>

View File

@@ -208,7 +208,7 @@ en:
location: "Location" location: "Location"
assign_officer: "Assign officer" assign_officer: "Assign officer"
officers_list: "List of officers" 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: officials:
edit: edit:
destroy: Remove 'Official' status 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" t.integer "user_id"
end end
create_table "poll_officers", force: :cascade do |t| create_table "poll_officing_booths", force: :cascade do |t|
t.integer "user_id" t.integer "officer_id"
t.integer "booth_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
create_table "poll_voters", force: :cascade do |t| create_table "poll_voters", force: :cascade do |t|

View File

@@ -267,6 +267,18 @@ FactoryGirl.define do
sequence(:name) { |n| "Poll #{n}" } sequence(:name) { |n| "Poll #{n}" }
end 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 factory :poll_booth, class: 'Poll::Booth' do
sequence(:name) { |n| "Booth #{n}" } sequence(:name) { |n| "Booth #{n}" }
sequence(:location) { |n| "Street #{n}" } sequence(:location) { |n| "Street #{n}" }

View File

@@ -1,12 +1,13 @@
require 'rails_helper' require 'rails_helper'
feature 'Admin poll officers' do feature 'Admin poll officers' do
background do background do
@admin = create(:administrator) @admin = create(:administrator)
@user = create(:user, username: 'Pedro Jose Garcia') @user = create(:user, username: 'Pedro Jose Garcia')
@officer = create(:poll_officer) @officer = create(:poll_officer)
login_as(@admin.user) login_as(@admin.user)
visit admin_poll_officers_path visit admin_officers_path
end end
scenario 'Index' do scenario 'Index' do
@@ -15,7 +16,7 @@ feature 'Admin poll officers' do
expect(page).to_not have_content @user.name expect(page).to_not have_content @user.name
end end
scenario 'Create poll officer', :js do scenario 'Create', :js do
fill_in 'email', with: @user.email fill_in 'email', with: @user.email
click_button 'Search' click_button 'Search'
@@ -26,11 +27,133 @@ feature 'Admin poll officers' do
end end
end end
scenario 'Delete poll officer' do scenario 'Delete' do
click_link 'Delete' click_link 'Delete'
within("#officers") do within("#officers") do
expect(page).to_not have_content @officer.name expect(page).to_not have_content @officer.name
end end
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 end