adds booth backend for index, show, create and edit actions

This commit is contained in:
rgarcia
2016-09-28 11:56:19 +02:00
parent 7db3683c27
commit 2c3127b6bd
18 changed files with 288 additions and 85 deletions

View File

@@ -1,5 +1,8 @@
class Admin::Poll::BoothsController < Admin::BaseController
skip_authorization_check
load_and_authorize_resource :poll
load_and_authorize_resource class: 'Poll::Booth', through: :poll
before_action :load_polls, only: :index
def index
end
@@ -10,7 +13,33 @@ class Admin::Poll::BoothsController < Admin::BaseController
def new
end
def create
if @booth.save
redirect_to admin_poll_booth_path(@poll, @booth), notice: t("flash.actions.create.poll_booth")
else
render :new
end
end
def edit
end
def update
if @booth.update(booth_params)
redirect_to admin_poll_booth_path(@poll, @booth), notice: t("flash.actions.update.poll_booth")
else
render :edit
end
end
private
def booth_params
params.require(:poll_booth).permit(:name, :location)
end
def load_polls
@polls = Poll.all
end
end

View File

@@ -40,12 +40,13 @@ module Abilities
can [:search, :create, :index, :destroy], ::Manager
can [:search, :create, :index, :destroy], ::Poll::Officer
can [:manage], Poll
can :manage, Annotation
can [:read, :update, :destroy, :summary], SpendingProposal
can [:search, :edit, :update, :create, :index, :destroy], Banner
can [:manage], Poll
can [:manage], Poll::Booth
end
end
end

View File

@@ -2,5 +2,7 @@ class Poll
class Booth < ActiveRecord::Base
belongs_to :poll
has_many :voters
validates :name, presence: true
end
end

View File

@@ -0,0 +1,20 @@
<div class="row">
<div class="small-12 medium-6 column">
<%= f.text_field :name,
placeholder: t('admin.booths.new.name'),
label: t("admin.booths.new.name") %>
</div>
<div class="small-12 column">
<%= f.text_field :location,
placeholder: t("admin.booths.new.location"),
label: t("admin.booths.new.location") %>
</div>
</div>
<div class="row">
<div class="small-12 medium-4 column">
<%= f.submit t("admin.booths.#{admin_submit_action(@booth)}.submit_button"),
class: "button success expanded" %>
</div>
</div>

View File

@@ -1,28 +1,7 @@
<%= render 'shared/back_link' %>
<h2><%= t("admin.booths.edit.title") %>: <%= t("admin.booths.edit.subtitle") %></h2>
<h2><%= t("admin.booths.edit.title", poll: @poll.name) %>: <%= t("admin.booths.edit.subtitle") %></h2>
<form>
<div class="row">
<div class="small-12 medium-6 column">
<label><%= t("admin.booths.edit.name") %></label>
<input type="text" placeholder="<%= t('admin.booths.edit.name') %>">
</div>
<div class="small-12 medium-6 column">
<label><%= t("admin.booths.edit.reference") %></label>
<input type="text" placeholder="<%= t('admin.booths.edit.reference') %>">
</div>
<div class="small-12 column">
<label><%= t("admin.booths.edit.location") %></label>
<input type="text" placeholder="<%= t("admin.booths.edit.location") %>">
</div>
</div>
<div class="row">
<div class="small-12 medium-4 column">
<input type="submit" value="<%= t('admin.booths.edit.submit_button') %>" class="button success expanded">
</div>
</div>
</form>
<%= form_for @booth, url: admin_poll_booth_path(@poll, @booth) do |f| %>
<%= render "form", f: f %>
<% end %>

View File

@@ -1,22 +1,27 @@
<h2><%= t("admin.booths.index.title") %></h2>
<form>
<div class="small-12 medium-6">
<select>
<option><%= t("admin.booths.index.select_poll") %></option>
</select>
</div>
</form>
<h3><%= t("admin.booths.index.title_list") %></h3>
<!-- If booths of this poll == 0 -->
<div class="callout primary">
<%= t("admin.booths.index.no_booths") %>
<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>
<!-- end -->
<%= link_to t("admin.booths.index.add_booth"), new_admin_poll_booth_path, class: "button success" %>
<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,
class: "button success" %>
<table>
<thead>
@@ -26,23 +31,25 @@
<th>&nbsp;</th>
</thead>
<tbody>
<!-- @polls.each do |poll| -->
<tr>
<% @booths.each do |booth| %>
<tr id="booth_<%= booth.id %>" class="booth">
<td>
<strong>
<%= link_to "Urna Moncloa (REFNUM)", "booths/1" %>
<%= link_to booth.name, admin_poll_booth_path(@poll, booth) %>
</strong>
</td>
<td>
C/ Isaac Peral, 25. 28003, Madrid
<%= booth.location %>
</td>
<td>
N <%= t("admin.booths.index.officers") %>
</td>
<td class="text-right">
<%= link_to t("admin.actions.edit"), "booths/1/edit", class: "button hollow" %>
<%= link_to t("admin.actions.edit"),
edit_admin_poll_booth_path(@poll, booth),
class: "button hollow" %>
</td>
</tr>
<!-- end -->
<% end %>
</tbody>
</table>

View File

@@ -1,28 +1,7 @@
<%= render 'shared/back_link' %>
<h2><%= t("admin.booths.new.title") %>: <%= t("admin.booths.new.subtitle") %></h2>
<h2><%= t("admin.booths.new.title", poll: @poll.name) %>: <%= t("admin.booths.new.subtitle") %></h2>
<form>
<div class="row">
<div class="small-12 medium-6 column">
<label><%= t("admin.booths.new.name") %></label>
<input type="text" placeholder="<%= t('admin.booths.new.name') %>">
</div>
<div class="small-12 medium-6 column">
<label><%= t("admin.booths.new.reference") %></label>
<input type="text" placeholder="<%= t('admin.booths.new.reference') %>">
</div>
<div class="small-12 column">
<label><%= t("admin.booths.new.location") %></label>
<input type="text" placeholder="<%= t("admin.booths.new.location") %>">
</div>
</div>
<div class="row">
<div class="small-12 medium-4 column">
<input type="submit" value="<%= t('admin.booths.new.submit_button') %>" class="button success expanded">
</div>
</div>
</form>
<%= form_for @booth, url: admin_poll_booths_path(@poll) do |f| %>
<%= render "form", f: f %>
<% end %>

View File

@@ -1,10 +1,17 @@
<%= render 'shared/back_link' %>
<div class="clear"></div>
<h2 class="inline-block">Urna Moncloa (REFNUM)</h2>
<%= link_to t("admin.actions.edit"), "1/edit", class: "button hollow float-right" %>
<h2 class="inline-block">
<%= @booth.name %>
</h2>
<%= link_to t("admin.actions.edit"),
edit_admin_poll_booth_path(@poll, @booth),
class: "button hollow float-right" %>
<p><strong><%= t("admin.booths.show.location") %></strong>: C/ Isaac Peral, 25. 28003, Madrid</p>
<p>
<strong><%= t("admin.booths.show.location") %></strong>:
<%= @booth.location %>
</p>
<h3><%= t("admin.booths.show.officers_list") %></h3>

View File

@@ -5,7 +5,9 @@
<%= @poll.name %>
</h2>
<%= link_to t("admin.actions.edit"), edit_admin_poll_path(@poll), class: "button hollow float-right" %>
<%= link_to t("admin.actions.edit"),
edit_admin_poll_path(@poll),
class: "button hollow float-right" %>
<p>(REFNUM)</p>
<p>Próximamente <small>(15/12/2016 - 15/02/2017)</small></p>

View File

@@ -174,7 +174,7 @@ en:
close_date: "Close date"
submit_button: "Update poll"
show:
no_booths: "There is no booths in this poll."
no_booths: "There are no booths in this poll."
add_booth: "Add booth"
booths_title: "List of booths"
name: "Name"
@@ -184,7 +184,7 @@ en:
title: "List of booths"
select_poll: "Select a poll"
title_list: "List of booths of poll %{poll}"
no_booths: "There is no booths in this poll."
no_booths: "There are no booths in this poll."
add_booth: "Add booth"
name: "Name"
location: "Location"

View File

@@ -7,6 +7,7 @@ en:
debate: "Debate created successfully."
direct_message: "You message has been sent successfully."
poll: "Poll created successfully."
poll_booth: "Booth created successfully."
proposal: "Proposal created successfully."
proposal_notification: "Your message has been sent correctly."
spending_proposal: "Spending proposal created successfully. You can access it from %{activity}"
@@ -16,6 +17,7 @@ en:
notice: "%{resource_name} updated successfully."
debate: "Debate updated successfully."
poll: "Poll updated successfully."
poll_booth: "Booth updated successfully."
proposal: "Proposal updated successfully."
spending_proposal: "Investment project updated succesfully."
destroy:

View File

@@ -7,6 +7,7 @@ es:
debate: "Debate creado correctamente."
direct_message: "Tu mensaje ha sido enviado correctamente."
poll: "Votación presencial creada correctamente."
poll_booth: "Urna creada correctamente."
proposal: "Propuesta creada correctamente."
proposal_notification: "Tu message ha sido enviado correctamente."
spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}"
@@ -16,6 +17,8 @@ es:
notice: "%{resource_name} actualizado correctamente."
debate: "Debate actualizado correctamente."
proposal: "Propuesta actualizada correctamente."
poll: "Votación presencial actualizada correctamente."
poll_booth: "Urna actualizada correctamente."
spending_proposal: "Propuesta de inversión actualizada correctamente."
destroy:
spending_proposal: "Propuesta de inversión eliminada."

View File

@@ -0,0 +1,5 @@
class AddLocationToBooths < ActiveRecord::Migration
def change
add_column :poll_booths, :location, :string
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160914172535) do
ActiveRecord::Schema.define(version: 20160926090107) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -273,6 +273,11 @@ ActiveRecord::Schema.define(version: 20160914172535) do
create_table "poll_booths", force: :cascade do |t|
t.string "name"
t.integer "poll_id"
t.string "location"
end
create_table "poll_officers", force: :cascade do |t|
t.integer "user_id"
end
create_table "poll_voters", force: :cascade do |t|
@@ -281,10 +286,6 @@ ActiveRecord::Schema.define(version: 20160914172535) do
t.string "document_type"
end
create_table "poll_officers", force: :cascade do |t|
t.integer "user_id"
end
create_table "polls", force: :cascade do |t|
t.string "name"
end

View File

@@ -269,6 +269,7 @@ FactoryGirl.define do
factory :poll_booth, class: 'Poll::Booth' do
sequence(:name) { |n| "Booth #{n}" }
sequence(:location) { |n| "Street #{n}" }
poll
end

View File

@@ -0,0 +1,146 @@
require 'rails_helper'
feature 'Admin booths' do
let!(:poll) { create(:poll) }
background do
admin = create(:administrator)
login_as(admin.user)
end
scenario 'Index empty' do
visit admin_root_path
within('#side_menu') do
click_link "Booths"
end
expect(page).to have_content "There are no booths in this poll"
end
scenario 'No link to booths when no polls' do
Poll.destroy_all
visit admin_root_path
within('#side_menu') do
expect(page).to_not have_link "Booths"
end
end
scenario 'Index' do
3.times { create(:poll_booth, poll: poll) }
visit admin_root_path
within('#side_menu') do
click_link "Booths"
end
expect(page).to have_css ".booth", count: 3
booths = Poll::Booth.all
booths.each do |booth|
within("#booth_#{booth.id}") do
expect(page).to have_content booth.name
expect(page).to have_content booth.location
end
end
expect(page).to_not have_content "There are no booths"
end
scenario "Index default to last poll" do
poll1 = create(:poll)
poll2 = create(:poll)
booth1 = create(:poll_booth, poll: poll1)
booth2 = create(:poll_booth, poll: poll2)
visit admin_root_path
within('#side_menu') do
click_link "Booths"
end
expect(page).to have_css ".booth", count: 1
expect(page).to have_content booth2.name
expect(page).to_not have_content booth1.name
end
scenario "Index select poll", :js do
poll1 = create(:poll)
poll2 = create(:poll)
booth1 = create(:poll_booth, poll: poll1)
booth2 = create(:poll_booth, poll: poll2)
visit admin_root_path
within('#side_menu') do
click_link "Booths"
end
select poll1.name, from: "poll_id"
expect(page).to have_content "List of booths of poll #{poll1.name}"
expect(page).to have_css ".booth", count: 1
expect(page).to have_content booth1.name
expect(page).to_not have_content booth2.name
end
scenario 'Show' do
booth = create(:poll_booth, poll: poll)
visit admin_poll_booths_path(poll)
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)
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"
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)
click_link "Edit"
expect(page).to have_content "Poll #{poll.name}"
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)
within("#booth_#{booth.id}") do
click_link "Edit"
end
expect(current_path).to eq(edit_admin_poll_booth_path(poll, booth))
end
end

View File

@@ -56,4 +56,7 @@ describe "Abilities::Administrator" do
it { should be_able_to(:update, SpendingProposal) }
it { should be_able_to(:valuate, SpendingProposal) }
it { should be_able_to(:destroy, SpendingProposal) }
it { should be_able_to(:manage, Poll) }
it { should be_able_to(:manage, Poll::Booth) }
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
describe :booth do
let(:booth) { build(:poll_booth) }
it "should be valid" do
expect(booth).to be_valid
end
it "should not be valid without a name" do
booth.name = nil
expect(booth).to_not be_valid
end
end