Check if debate phase is open to create answers

This commit is contained in:
Amaia Castro
2016-12-28 21:43:03 +01:00
parent 28689a5a7d
commit f505d6fec7
6 changed files with 112 additions and 18 deletions

View File

@@ -9,13 +9,20 @@ class Legislation::AnswersController < Legislation::BaseController
respond_to :html, :js
def create
if @process.open_phase?(:debate)
@answer.user = current_user
@answer.save
track_event
respond_to do |format|
format.js {}
format.js
format.html { redirect_to legislation_process_question_path(@process, @question) }
end
else
respond_to do |format|
format.js
format.html { redirect_to legislation_process_question_path(@process, @question), alert: t('legislation.questions.participation.phase_not_open') }
end
end
end
private
@@ -24,4 +31,11 @@ class Legislation::AnswersController < Legislation::BaseController
:legislation_question_option_id,
)
end
def track_event
ahoy.track "legislation_answer_created".to_sym,
"legislation_answer_id": @answer.id,
"legislation_question_option_id": @answer.legislation_question_option_id,
"legislation_question_id": @answer.legislation_question_id
end
end

View File

@@ -1,12 +1,28 @@
<% if question.question_options.any? %>
<%= form_for answer, url: legislation_process_question_answers_path(process, question, answer), remote: true , html: { class: "controls-stacked participation-allowed"} do |f| %>
<% if process.open_phase?(:debate) && !answer.persisted? %>
<%= form_for answer, url: legislation_process_question_answers_path(process, question, answer), remote: true , html: { class: "controls-stacked"} do |f| %>
<% question.question_options.each do |question_option| %>
<label class="control radio <%= 'active' if @answer.legislation_question_option_id == question_option.id %>">
<%= f.radio_button :legislation_question_option_id, question_option.id, label: false, disabled: answer.persisted? %>
<%= f.radio_button :legislation_question_option_id, question_option.id, label: false %>
<span class="control-indicator"></span>
<%= question_option.value %>
</label>
<% end %>
<%= f.submit t('legislation.questions.show.answer_question'), class: "button" unless answer.persisted? %>
<%= f.submit t('legislation.questions.show.answer_question'), class: "button" %>
<% end %>
<% else %>
<form class="controls-stacked disabled">
<% question.question_options.each do |question_option| %>
<label class="control radio <%= 'active' if answer.persisted? && (answer.legislation_question_option_id == question_option.id) %>">
<input id="quiz-1" name="radio" type="radio" disabled="true">
<span class="control-indicator"></span>
<%= question_option.value %>
</label>
<% end %>
</form>
<% end %>
<% end %>

View File

@@ -279,6 +279,7 @@ en:
share_gplus: Share on Google+
title: Collaborative legislation process
participation:
phase_not_open: This phase is not open
organizations: Organisations are not permitted to participate in the debate
signin: Sign in
signup: Sign up

View File

@@ -279,6 +279,7 @@ es:
share_gplus: Compartir en Google+
title: Proceso de legislación colaborativa
participation:
phase_not_open: Esta fase no está abierta
organizations: Las organizaciones no pueden participar en el debate
signin: iniciar sesión
signup: registrarte

View File

@@ -0,0 +1,43 @@
require 'rails_helper'
describe Legislation::AnswersController do
describe 'POST create' do
before(:each) do
InvisibleCaptcha.timestamp_enabled = false
@process = create(:legislation_process, debate_start_date: Date.current - 3.day, debate_end_date: Date.current + 2.days)
@question = create(:legislation_question, process: @process, title: "Question 1")
@question_option = create(:legislation_question_option, question: @question, value: "Yes")
@user = create(:user, :level_two)
end
after(:each) do
InvisibleCaptcha.timestamp_enabled = true
end
it 'should create an ahoy event' do
sign_in @user
post :create, process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }
expect(Ahoy::Event.where(name: :legislation_answer_created).count).to eq 1
expect(Ahoy::Event.last.properties['legislation_answer_id']).to eq Legislation::Answer.last.id
end
it 'should create an answer if the process debate phase is open' do
sign_in @user
expect do
xhr :post, :create, process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }
end.to change { @question.reload.answers_count }.by(1)
end
it 'should not create an answer if the process debate phase is not open' do
sign_in @user
@process.update_attribute(:debate_end_date, Date.current - 1.day)
expect do
xhr :post, :create, process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }
end.to_not change { @question.reload.answers_count }
end
end
end

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
feature 'Legislation' do
context 'process debate page' do
before(:each) do
@process = create(:legislation_process, debate_start_date: Date.current - 1.day, debate_end_date: Date.current + 2.days)
@process = create(:legislation_process, debate_start_date: Date.current - 3.day, debate_end_date: Date.current + 2.days)
create(:legislation_question, process: @process, title: "Question 1")
create(:legislation_question, process: @process, title: "Question 2")
create(:legislation_question, process: @process, title: "Question 3")
@@ -69,9 +69,9 @@ feature 'Legislation' do
visit legislation_process_question_path(@process, question)
expect(page).to have_content("Yes")
expect(page).to have_content("No")
expect(page).to have_content("I don't know")
expect(page).to have_selector(:radio_button, "Yes")
expect(page).to have_selector(:radio_button, "No")
expect(page).to have_selector(:radio_button, "I don't know")
expect(page).to have_selector(:link_or_button, "Submit answer")
choose("I don't know")
@@ -87,5 +87,24 @@ feature 'Legislation' do
expect(question.reload.answers_count).to eq(1)
expect(option.reload.answers_count).to eq(1)
end
scenario 'cannot answer question when phase not open' do
@process.update_attribute(:debate_end_date, Date.current - 1.day)
question = @process.questions.first
create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_question_option, question: question, value: "No")
create(:legislation_question_option, question: question, value: "I don't know")
user = create(:user, :level_two)
login_as(user)
visit legislation_process_question_path(@process, question)
expect(page).to have_selector(:radio_button, "Yes", disabled: true)
expect(page).to have_selector(:radio_button, "No", disabled: true)
expect(page).to have_selector(:radio_button, "I don't know", disabled: true)
expect(page).to_not have_selector(:link_or_button, "Submit answer")
end
end
end