Skip to content

Commit

Permalink
adds complete Booth management to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanxu committed Dec 2, 2016
1 parent 6f986f3 commit f67f058
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 142 deletions.
17 changes: 5 additions & 12 deletions app/controllers/admin/poll/booths_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
class Admin::Poll::BoothsController < Admin::BaseController
load_and_authorize_resource :poll
load_and_authorize_resource class: 'Poll::Booth', through: :poll

before_action :load_polls, only: :index
load_and_authorize_resource class: 'Poll::Booth'

def index
@booths = @booths.order(name: :asc).page(params[:page])
end

def show
@officers = Poll::Officer.all
end

def new
end

def create
if @booth.save
redirect_to admin_poll_booth_path(@poll, @booth), notice: t("flash.actions.create.poll_booth")
redirect_to admin_booths_path, notice: t("flash.actions.create.poll_booth")
else
render :new
end
Expand All @@ -27,7 +24,7 @@ def edit

def update
if @booth.update(booth_params)
redirect_to admin_poll_booth_path(@poll, @booth), notice: t("flash.actions.update.poll_booth")
redirect_to admin_booth_path(@booth), notice: t("flash.actions.update.poll_booth")
else
render :edit
end
Expand All @@ -36,11 +33,7 @@ def update
private

def booth_params
params.require(:poll_booth).permit(:name, :location, officer_ids: [])
end

def load_polls
@polls = Poll.all
params.require(:poll_booth).permit(:name, :location)
end

end
7 changes: 2 additions & 5 deletions app/views/admin/poll/booths/_booth.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<tr id="booth_<%= booth.id %>" class="booth">
<td>
<strong>
<%= link_to booth.name, admin_poll_booth_path(@poll, booth) %>
<%= link_to booth.name, admin_booth_path(booth) %>
</strong>
</td>
<td>
<%= booth.location %>
</td>
<td>
N <%= t("admin.booths.index.officers") %>
</td>
<td class="text-right">
<%= link_to t("admin.actions.edit"),
edit_admin_poll_booth_path(@poll, booth),
edit_admin_booth_path(booth),
class: "button hollow" %>
</td>
</tr>
6 changes: 3 additions & 3 deletions app/views/admin/poll/booths/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= render 'shared/back_link' %>
<%= back_link_to admin_booths_path %>

<h2><%= t("admin.booths.edit.title", poll: @poll.name) %>: <%= t("admin.booths.edit.subtitle") %></h2>
<h2><%= t("admin.booths.edit.title") %></h2>

<%= form_for @booth, url: admin_poll_booth_path(@poll, @booth) do |f| %>
<%= form_for @booth, url: admin_booth_path(@booth) do |f| %>
<%= render "form", f: f %>
<% end %>
44 changes: 17 additions & 27 deletions app/views/admin/poll/booths/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
<h2><%= t("admin.booths.index.title") %></h2>

<div class="small-12 medium-6">
<%= form_tag '', method: :get do %>
<%= select_tag "poll_id",
options_for_select(@polls.collect {|poll|
[poll.name, admin_poll_booths_path(poll)]
}),
prompt: t("admin.booths.index.select_poll"),
class: "js-location-changer" %>
<% end %>
</div>

<h3><%= t("admin.booths.index.title_list", poll: @poll.name) %></h3>

<% if @booths.empty? %>
<div class="callout primary">
<%= t("admin.booths.index.no_booths") %>
</div>
<% end %>
<%= link_to t("admin.booths.index.add_booth"),
new_admin_poll_booth_path(@poll),
new_admin_booth_path,
class: "button success" %>
<table>
<thead>
<th><%= t("admin.booths.index.name") %></th>
<th><%= t("admin.booths.index.location") %></th>
<th><%= t("admin.booths.index.officers") %></th>
<th>&nbsp;</th>
</thead>
<tbody>
<% @booths.each do |booth| %>
<%= render partial: "booth", locals: { booth: booth } %>
<% end %>
</tbody>
</table>
<% if @booths.any? %>
<table>
<thead>
<th><%= t("admin.booths.index.name") %></th>
<th><%= t("admin.booths.index.location") %></th>
<th>&nbsp;</th>
</thead>
<tbody>
<% @booths.each do |booth| %>
<%= render partial: "booth", locals: { booth: booth } %>
<% end %>
</tbody>
</table>

<%= paginate @booths %>
<% end %>
6 changes: 3 additions & 3 deletions app/views/admin/poll/booths/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= render 'shared/back_link' %>
<%= back_link_to admin_booths_path %>

<h2><%= t("admin.booths.new.title", poll: @poll.name) %>: <%= t("admin.booths.new.subtitle") %></h2>
<h2><%= t("admin.booths.new.title") %></h2>

<%= form_for @booth, url: admin_poll_booths_path(@poll) do |f| %>
<%= form_for @booth, url: admin_booths_path(@booth) do |f| %>
<%= render "form", f: f %>
<% end %>
46 changes: 3 additions & 43 deletions app/views/admin/poll/booths/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
<%= render 'shared/back_link' %>
<%= back_link_to admin_booths_path %>

<div class="clear"></div>

<h2 class="inline-block">
<%= @booth.name %>
</h2>
<%= link_to t("admin.actions.edit"),
edit_admin_poll_booth_path(@poll, @booth),
edit_admin_booth_path(@booth),
class: "button hollow float-right" %>

<p>
<strong><%= t("admin.booths.show.location") %></strong>:
<%= @booth.location %>
</p>

<h3><%= t("admin.booths.show.officers_list") %></h3>

<% if @booth.officers.empty? %>
<div class="callout primary">
<%= t("admin.booths.show.no_officers") %>
</div>
<% end %>

<div class="small-12 column">
<%= form_for @booth, url: admin_poll_booth_path(@poll, @booth) do |f| %>
<%= 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 id="assigned_officers">
<% @booth.officers.each do |officer| %>
<tr id="officer_<%= officer.id %>" class="officer">
<td>
<%= officer.name %>
</td>
<td>
<%= officer.email %>
</td>
<td class="text-right">
<%= link_to t('admin.poll_officers.officer.delete'), "#", class: "button hollow alert" %>
<%# link_to t('admin.poll_officers.officer.delete'),
admin_poll_officer_path(Poll.last, officer),
method: :delete,
class: "button hollow alert"
%>
</td>
</tr>
<% end %>
</table>
11 changes: 3 additions & 8 deletions config/locales/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,18 @@ en:
booths:
index:
title: "List of booths"
select_poll: "Select a poll"
title_list: "List of booths of poll %{poll}"
no_booths: "There are no booths in this poll."
no_booths: "There are no booths."
add_booth: "Add booth"
name: "Name"
location: "Location"
officers: "Officers"
new:
title: "Poll %{poll}"
subtitle: "New booth"
title: "New booth"
name: "Name"
reference: "Reference number"
location: "Location"
submit_button: "Create booth"
edit:
title: "Poll %{poll}"
subtitle: "Edit booth"
title: "Edit booth"
name: "Name"
reference: "Reference number"
location: "Location"
Expand Down
11 changes: 3 additions & 8 deletions config/locales/admin.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,18 @@ es:
booths:
index:
title: "Lista de urnas"
select_poll: "Selecciona una votación"
title_list: "Lista de urnas de la votación %{poll}"
no_booths: "No hay urnas en esta votación."
no_booths: "No hay urnas."
add_booth: "Añadir urna"
name: "Nombre"
location: "Ubicación"
officers: "Presidentes de mesa"
new:
title: "Votación %{poll}"
subtitle: "Nueva urna"
title: "Nueva urna"
name: "Nombre"
reference: "Número de referencia"
location: "Ubicación"
submit_button: "Crear urna"
edit:
title: "Votación %{poll}"
subtitle: "Editar urna"
title: "Editar urna"
name: "Nombre"
reference: "Número de referencia"
location: "Ubicación"
Expand Down
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@
resources :officers do
get :search, on: :collection
end
resources :polls do
resources :booths
end
resources :polls
resources :booths
resources :questions
end

Expand Down
46 changes: 17 additions & 29 deletions spec/features/admin/poll/booths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

feature 'Admin booths' do

let!(:poll) { create(:poll) }

background do
admin = create(:administrator)
login_as(admin.user)
Expand All @@ -13,25 +11,21 @@
visit admin_root_path

within('#side_menu') do
click_link "Polls"
click_link "Booths"
end

click_link poll.name

expect(page).to have_content "There are no booths in this poll"
expect(page).to have_content "There are no booths"
end

scenario 'Index' do
3.times { create(:poll_booth, poll: poll) }
3.times { create(:poll_booth) }

visit admin_root_path

within('#side_menu') do
click_link "Polls"
click_link "Booths"
end

click_link poll.name

booths = Poll::Booth.all
booths.each do |booth|
within("#booth_#{booth.id}") do
Expand All @@ -43,57 +37,51 @@
end

scenario 'Show' do
booth = create(:poll_booth, poll: poll)
booth = create(:poll_booth)

visit admin_poll_booths_path(poll)
visit admin_booths_path
click_link booth.name

expect(page).to have_content booth.name
expect(page).to have_content booth.location
end

scenario "Create" do
visit admin_poll_booths_path(poll)
visit admin_booths_path
click_link "Add booth"

expect(page).to have_content "Poll #{poll.name}"

fill_in "poll_booth_name", with: "Upcoming booth"
fill_in "poll_booth_location", with: "39th Street, number 2, ground floor"
click_button "Create booth"

expect(page).to have_content "Booth created successfully"

visit admin_booths_path
expect(page).to have_content "Upcoming booth"
expect(page).to have_content "39th Street, number 2, ground floor"
end

scenario "Edit" do
booth = create(:poll_booth, poll: poll)

visit admin_poll_booths_path(poll)
booth = create(:poll_booth)

click_link "Edit"
visit admin_booths_path

expect(page).to have_content "Poll #{poll.name}"
within("#booth_#{booth.id}") do
click_link "Edit"
end

fill_in "poll_booth_name", with: "Next booth"
fill_in "poll_booth_location", with: "40th Street, number 1, firts floor"
click_button "Update booth"

expect(page).to have_content "Booth updated successfully"
expect(page).to have_content "Next booth"
expect(page).to have_content "40th Street, number 1, firts floor"
end

scenario 'Edit from index' do
booth = create(:poll_booth, poll: poll)
visit admin_poll_booths_path(poll)
visit admin_booths_path

within("#booth_#{booth.id}") do
click_link "Edit"
expect(page).to have_content "Next booth"
expect(page).to have_content "40th Street, number 1, firts floor"
end

expect(current_path).to eq(edit_admin_poll_booth_path(poll, booth))
end

end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,4 @@
# as the one that triggered the failure.
Kernel.srand config.seed


end

0 comments on commit f67f058

Please sign in to comment.