diff --git a/app/controllers/legislation/answers_controller.rb b/app/controllers/legislation/answers_controller.rb index b090a605a..ac77ea5f1 100644 --- a/app/controllers/legislation/answers_controller.rb +++ b/app/controllers/legislation/answers_controller.rb @@ -9,12 +9,19 @@ class Legislation::AnswersController < Legislation::BaseController respond_to :html, :js def create - @answer.user = current_user - @answer.save - - respond_to do |format| - format.js {} - format.html { redirect_to legislation_process_question_path(@process, @question) } + if @process.open_phase?(:debate) + @answer.user = current_user + @answer.save + track_event + respond_to do |format| + 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 @@ -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 diff --git a/app/views/legislation/questions/_answer_form.html.erb b/app/views/legislation/questions/_answer_form.html.erb index 01254f021..775e93faa 100644 --- a/app/views/legislation/questions/_answer_form.html.erb +++ b/app/views/legislation/questions/_answer_form.html.erb @@ -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| %> - <% question.question_options.each do |question_option| %> - + <% 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| %> + + <% end %> + <%= f.submit t('legislation.questions.show.answer_question'), class: "button" %> <% end %> - <%= f.submit t('legislation.questions.show.answer_question'), class: "button" unless answer.persisted? %> + + <% else %> + +
+ <% end %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 2b7c70334..c3a60ff67 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/config/locales/es.yml b/config/locales/es.yml index 102d7db1b..6c8a99e1d 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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 diff --git a/spec/controllers/legislation/answers_controller_spec.rb b/spec/controllers/legislation/answers_controller_spec.rb new file mode 100644 index 000000000..99efcef67 --- /dev/null +++ b/spec/controllers/legislation/answers_controller_spec.rb @@ -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 diff --git a/spec/features/legislation/questions_spec.rb b/spec/features/legislation/questions_spec.rb index c967d3bf0..c9513e9de 100644 --- a/spec/features/legislation/questions_spec.rb +++ b/spec/features/legislation/questions_spec.rb @@ -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