adds admin search/add/remove question to poll

This commit is contained in:
Juanjo Bazán
2016-12-26 16:19:35 +01:00
parent 8b4f519d71
commit f0156c5b5a
13 changed files with 177 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
class Admin::Poll::PollsController < Admin::BaseController
load_and_authorize_resource
before_action :load_search, only: [:search_booths]
before_action :load_search, only: [:search_booths, :search_questions]
def index
end
@@ -31,6 +31,30 @@ class Admin::Poll::PollsController < Admin::BaseController
end
end
def add_question
question = ::Poll::Question.find(params[:question_id])
if question.present?
@poll.questions << question
notice = t("admin.polls.flash.question_added")
else
notice = t("admin.polls.flash.error_on_question_added")
end
redirect_to admin_poll_path(@poll, anchor: 'tab-questions'), notice: notice
end
def remove_question
question = ::Poll::Question.find(params[:question_id])
if @poll.questions.include? question
@poll.questions.delete(question)
notice = t("admin.polls.flash.question_removed")
else
notice = t("admin.polls.flash.error_on_question_removed")
end
redirect_to admin_poll_path(@poll, anchor: 'tab-questions'), notice: notice
end
def search_booths
@booths = ::Poll::Booth.search(@search)
respond_to do |format|
@@ -38,6 +62,13 @@ class Admin::Poll::PollsController < Admin::BaseController
end
end
def search_questions #cambiar a @poll.id
@questions = ::Poll::Question.where("poll_id IS ? OR poll_id != ?", nil, search_params[:poll_id]).search({search: @search})
respond_to do |format|
format.js
end
end
private
def poll_params

View File

@@ -21,6 +21,7 @@ class Poll::Question < ActiveRecord::Base
validates :title, length: { in: 4..Poll::Question.title_max_length }
validates :description, length: { maximum: Poll::Question.description_max_length }
scope :no_poll, -> { where(poll_id: nil) }
scope :by_poll_id, -> (poll_id) { where(poll_id: poll_id) }
scope :by_geozone_id, -> (geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }

View File

@@ -11,9 +11,9 @@
<td><%= link_to question.title, admin_question_path(question) %></td>
<td class="text-right">
<%= link_to t('admin.polls.show.remove_question'),
admin_question_path(question),
remove_question_admin_poll_path(poll_id: @poll.id, question_id: question.id),
class: "button hollow alert",
method: :delete %>
method: :patch %>
</td>
</tr>
<% end %>

View File

@@ -0,0 +1,14 @@
<%= form_tag(search_questions_admin_poll_path(@poll), method: :get, remote: true) do |f| %>
<div class="row">
<div class="small-12 medium-6 column">
<%= text_field_tag :search,
@search,
placeholder: t("admin.shared.poll_questions_search.placeholder"), id: "search-questions" %>
</div>
<div class="form-inline small-12 medium-3 column end">
<%= submit_tag t("admin.shared.poll_questions_search.button"), class: "button" %>
</div>
</div>
<% end %>
<div id="search-questions-results"></div>

View File

@@ -0,0 +1,25 @@
<table>
<thead>
<tr>
<th colspan="3"><%= @questions.blank? ? t('admin.polls.show.no_search_results') : t('admin.polls.show.search_results') %></th>
</tr>
</thead>
<tbody>
<% @questions.each do |question| %>
<tr>
<td>
<%= question.title %>
</td>
<td>
<%= question.summary %>
</td>
<td class="text-right">
<%= link_to t("admin.polls.show.add_question"),
add_question_admin_poll_path(poll_id: @poll.id, question_id: question.id),
method: :patch,
class: "button hollow" %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1 @@
$("#search-questions-results").html("<%= j render 'search_questions_results' %>");

View File

@@ -11,6 +11,7 @@
<%= render "filter_subnav" %>
<div class="tabs-panel is-active" id="tab-questions">
<%= render "search_questions" %>
<%= render "questions" %>
</div>

View File

@@ -2,9 +2,9 @@
<div class="input-group">
<%= text_field_tag :search,
@search,
placeholder: t("admin.shared.spending_proposal_search.placeholder") %>
placeholder: t("admin.shared.poll_questions_search.placeholder") %>
<div class="input-group-button">
<%= submit_tag t("admin.shared.spending_proposal_search.button"), class: "button" %>
<%= submit_tag t("admin.shared.poll_questions_search.button"), class: "button" %>
</div>
</div>
<% end %>

View File

@@ -191,11 +191,17 @@ en:
questions_title: "List of questions"
remove_question: "Remove question from poll"
add_booth: "Assign booth"
add_question: "Include question"
name: "Name"
location: "Location"
email: "Email"
search_results: "Search results"
no_search_results: "No results found"
flash:
question_added: "Question added to this poll"
error_on_question_added: "Question could not be assigned to this poll"
question_removed: "Question removed from this poll"
error_on_question_removed: "Question could not be removed from this poll"
questions:
index:
title: "Questions"
@@ -309,6 +315,9 @@ en:
booths_search:
button: Search
placeholder: Search booth by name
poll_questions_search:
button: Search
placeholder: Search poll questions
proposal_search:
button: Search
placeholder: Search proposals by title, code, description or question

View File

@@ -191,11 +191,17 @@ es:
questions_title: "Listado de preguntas asignadas"
remove_question: "Desasignar pregunta"
add_booth: "Asignar urna"
add_question: "Incluir pregunta"
name: "Nombre"
location: "Ubicación"
email: "Email"
search_results: "Resultados de la búsqueda"
no_search_results: "No se han encontrado resultados"
flash:
question_added: "Pregunta añadida a esta votación"
error_on_question_added: "No se pudo asignar la pregunta"
question_removed: "Pregunta eliminada de esta votación"
error_on_question_removed: "No se pudo quitar la pregunta"
questions:
index:
title: "Preguntas ciudadanas"
@@ -309,6 +315,9 @@ es:
booths_search:
button: Buscar
placeholder: Buscar urna por nombre
poll_questions_search:
button: Buscar
placeholder: Buscar preguntas
proposal_search:
button: Buscar
placeholder: Buscar propuestas por título, código, descripción o pregunta

View File

@@ -189,6 +189,9 @@ Rails.application.routes.draw do
end
resources :polls do
get :search_booths, on: :member
get :search_questions, on: :member
patch :add_question, on: :member
patch :remove_question, on: :member
end
resources :booths

View File

@@ -165,4 +165,81 @@ feature 'Admin polls' do
end
end
context "Questions" do
context "Poll show" do
scenario "Question list", :js do
poll = create(:poll)
question = create(:poll_question, poll: poll)
other_question = create(:poll_question)
visit admin_poll_path(poll)
click_link "Questions (1)"
expect(page).to have_content question.title
expect(page).to_not have_content other_question.title
expect(page).to_not have_content "There are no questions assigned to this poll"
end
scenario 'Add question to poll', :js do
poll = create(:poll)
question = create(:poll_question, poll: nil, title: 'Should we rebuild the city?')
visit admin_poll_path(poll)
within('#poll-resources') do
click_link 'Questions (0)'
end
expect(page).to have_content 'There are no questions assigned to this poll'
fill_in 'search-questions', with: 'rebuild'
click_button 'Search'
within('#search-questions-results') do
click_link 'Include question'
end
expect(page).to have_content 'Question added to this poll'
visit admin_poll_path(poll)
within('#poll-resources') do
click_link 'Questions (1)'
end
expect(page).to_not have_content 'There are no questions assigned to this poll'
expect(page).to have_content question.title
end
scenario 'Remove question from poll', :js do
poll = create(:poll)
question = create(:poll_question, poll: poll)
visit admin_poll_path(poll)
within('#poll-resources') do
click_link 'Questions (1)'
end
expect(page).to_not have_content 'There are no questions assigned to this poll'
expect(page).to have_content question.title
within("#poll_question_#{question.id}") do
click_link 'Remove question from poll'
end
expect(page).to have_content 'Question removed from this poll'
visit admin_poll_path(poll)
within('#poll-resources') do
click_link 'Questions (0)'
end
expect(page).to have_content 'There are no questions assigned to this poll'
expect(page).to_not have_content question.title
end
end
end
end