diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 1e4c53c9e..2b17075ce 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -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 diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index b6e2c5ce0..9f680c739 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -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)) } diff --git a/app/models/user.rb b/app/models/user.rb index 916c3d0b9..d248e5f48 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,7 +2,7 @@ class User < ActiveRecord::Base include Verification - devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, + devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :async, :password_expirable, :secure_validatable acts_as_voter diff --git a/app/views/admin/poll/polls/_questions.html.erb b/app/views/admin/poll/polls/_questions.html.erb index 1cd8597bb..8adf3596e 100644 --- a/app/views/admin/poll/polls/_questions.html.erb +++ b/app/views/admin/poll/polls/_questions.html.erb @@ -11,9 +11,9 @@ <%= link_to question.title, admin_question_path(question) %> <%= 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 %> <% end %> diff --git a/app/views/admin/poll/polls/_search_questions.html.erb b/app/views/admin/poll/polls/_search_questions.html.erb new file mode 100644 index 000000000..0a6123c1b --- /dev/null +++ b/app/views/admin/poll/polls/_search_questions.html.erb @@ -0,0 +1,14 @@ +<%= form_tag(search_questions_admin_poll_path(@poll), method: :get, remote: true) do |f| %> +
+
+ <%= text_field_tag :search, + @search, + placeholder: t("admin.shared.poll_questions_search.placeholder"), id: "search-questions" %> +
+
+ <%= submit_tag t("admin.shared.poll_questions_search.button"), class: "button" %> +
+
+<% end %> + +
diff --git a/app/views/admin/poll/polls/_search_questions_results.html.erb b/app/views/admin/poll/polls/_search_questions_results.html.erb new file mode 100644 index 000000000..e7012edd3 --- /dev/null +++ b/app/views/admin/poll/polls/_search_questions_results.html.erb @@ -0,0 +1,25 @@ + + + + + + + + <% @questions.each do |question| %> + + + + + + <% end %> + +
<%= @questions.blank? ? t('admin.polls.show.no_search_results') : t('admin.polls.show.search_results') %>
+ <%= question.title %> + + <%= question.summary %> + + <%= 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" %> +
\ No newline at end of file diff --git a/app/views/admin/poll/polls/search_questions.js.erb b/app/views/admin/poll/polls/search_questions.js.erb new file mode 100644 index 000000000..05f5c5167 --- /dev/null +++ b/app/views/admin/poll/polls/search_questions.js.erb @@ -0,0 +1 @@ +$("#search-questions-results").html("<%= j render 'search_questions_results' %>"); \ No newline at end of file diff --git a/app/views/admin/poll/polls/show.html.erb b/app/views/admin/poll/polls/show.html.erb index 3602dff24..b948ee9a5 100644 --- a/app/views/admin/poll/polls/show.html.erb +++ b/app/views/admin/poll/polls/show.html.erb @@ -11,6 +11,7 @@ <%= render "filter_subnav" %>
+ <%= render "search_questions" %> <%= render "questions" %>
diff --git a/app/views/admin/poll/questions/_search.html.erb b/app/views/admin/poll/questions/_search.html.erb index 7ee1308f7..e8a2c52c5 100644 --- a/app/views/admin/poll/questions/_search.html.erb +++ b/app/views/admin/poll/questions/_search.html.erb @@ -2,9 +2,9 @@
<%= text_field_tag :search, @search, - placeholder: t("admin.shared.spending_proposal_search.placeholder") %> + placeholder: t("admin.shared.poll_questions_search.placeholder") %>
- <%= submit_tag t("admin.shared.spending_proposal_search.button"), class: "button" %> + <%= submit_tag t("admin.shared.poll_questions_search.button"), class: "button" %>
<% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 3146f4369..6e8f4e46e 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -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 diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index eaf88ec97..6158f02de 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 434dddcc3..587d1e70e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index 8db706c01..31d7f6128 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -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 \ No newline at end of file