Answer legislation questions

This commit is contained in:
Amaia Castro
2016-12-27 19:49:03 +01:00
parent 5e01b380cd
commit a6ceab6d73
13 changed files with 136 additions and 21 deletions

View File

@@ -0,0 +1,21 @@
class Legislation::AnswersController < Legislation::BaseController
before_action :authenticate_user!
before_action :verify_resident!
load_and_authorize_resource :process
load_and_authorize_resource :question, through: :process
load_and_authorize_resource :answer, through: :question
def create
@answer.user = current_user
@answer.save
redirect_to legislation_process_question_path(@process, @question)
end
private
def answer_params
params.require(:legislation_answer).permit(
:legislation_question_option_id,
)
end
end

View File

@@ -8,5 +8,6 @@ class Legislation::QuestionsController < Legislation::BaseController
@commentable = @question @commentable = @question
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order) @comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments) set_comment_flags(@comment_tree.comments)
@answer = @question.answer_for_user(current_user) || Legislation::Answer.new
end end
end end

View File

@@ -14,6 +14,7 @@ module Abilities
can [:read, :draft_publication, :allegations, :final_version_publication], Legislation::Process can [:read, :draft_publication, :allegations, :final_version_publication], Legislation::Process
can [:read], Legislation::DraftVersion can [:read], Legislation::DraftVersion
can [:read], Legislation::Question can [:read], Legislation::Question
can [:create], Legislation::Answer
end end
end end
end end

View File

@@ -2,9 +2,9 @@ class Legislation::Process < ActiveRecord::Base
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases include ActsAsParanoidAliases
has_many :draft_versions, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id' has_many :draft_versions, -> { order(:id) }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id'
has_one :final_draft_version, -> { where final_version: true }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id' has_one :final_draft_version, -> { where final_version: true }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id'
has_many :questions, class_name: 'Legislation::Question', foreign_key: 'legislation_process_id' has_many :questions, -> { order(:id) }, class_name: 'Legislation::Question', foreign_key: 'legislation_process_id'
validates :title, presence: true validates :title, presence: true
validates :description, presence: true validates :description, presence: true

View File

@@ -5,7 +5,7 @@ class Legislation::Question < ActiveRecord::Base
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
has_many :question_options, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question has_many :question_options, -> { order(:id) }, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
has_many :comments, as: :commentable has_many :comments, as: :commentable
@@ -17,4 +17,8 @@ class Legislation::Question < ActiveRecord::Base
def next_question_id def next_question_id
@next_question_id ||= process.questions.where("id > ?", id).order('id ASC').limit(1).pluck(:id).first @next_question_id ||= process.questions.where("id > ?", id).order('id ASC').limit(1).pluck(:id).first
end end
def answer_for_user(user)
answers.where(user: user).first
end
end end

View File

@@ -44,7 +44,7 @@
<% end %> <% end %>
<% if feature?(:legislation) %> <% if feature?(:legislation) %>
<li <%= "class=active" if controller_name == "processes" %>> <li <%= "class=active" if controller.class.parent == Admin::Legislation %>>
<%= link_to admin_legislation_processes_path do %> <%= link_to admin_legislation_processes_path do %>
<span class="icon-budget"></span><%= t("admin.menu.legislation") %> <span class="icon-budget"></span><%= t("admin.menu.legislation") %>
<% end %> <% end %>

View File

@@ -29,7 +29,9 @@
<td> <td>
<%= content_tag :ul do %> <%= content_tag :ul do %>
<% question.question_options.each do |question_option| %> <% question.question_options.each do |question_option| %>
<%= content_tag :li, question_option.value %> <%= content_tag :li do %>
<%= question_option.value %> (<%= question_option.answers_count %>)
<% end %>
<% end %> <% end %>
<% end %> <% end %>
</td> </td>

View File

@@ -23,16 +23,19 @@
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<h3 class="quiz-question"><%= @question.title %></h3> <h3 class="quiz-question"><%= @question.title %></h3>
<div class="debate-questions"> <div class="debate-questions">
<form class="controls-stacked"> <% if @question.question_options.any? %>
<% @question.question_options.each do |question_option| %> <%= form_for @answer, url: legislation_process_question_answers_path(@process, @question, @answer), class: "controls-stacked" do |f| %>
<label class="control radio"> <% @question.question_options.each do |question_option| %>
<input id="quiz-1" name="radio" type="radio"> <label class="control radio <%= 'active' if @answer.legislation_question_option_id == question_option.id %>">
<span class="control-indicator"></span> <%= f.radio_button :legislation_question_option_id, question_option.id, label: false, disabled: @answer.persisted? %>
<%= question_option.value %> <span class="control-indicator"></span>
</label> <%= question_option.value %>
</label>
<% end %>
<%= f.submit t('.answer_question'), class: "button" unless @answer.persisted? %>
<% end %> <% end %>
</form> <% end %>
</div> </div>
</div> </div>
<aside class="small-12 medium-3 column"> <aside class="small-12 medium-3 column">

View File

@@ -40,6 +40,15 @@ en:
legislation/draft_versions: legislation/draft_versions:
one: "Draft version" one: "Draft version"
other: "Draft versions" other: "Draft versions"
legislation/questions:
one: "Question"
other: "Questions"
legislation/question_options:
one: "Question option"
other: "Question options"
legislation/answers:
one: "Answer"
other: "Answers"
attributes: attributes:
comment: comment:
body: "Comment" body: "Comment"
@@ -99,6 +108,10 @@ en:
changelog: Changes changelog: Changes
status: Status status: Status
final_version: Final version final_version: Final version
legislation/questions:
title: "Title"
legislation/question_options:
value: "Value"
errors: errors:
models: models:
user: user:

View File

@@ -40,6 +40,15 @@ es:
legislation/draft_texts: legislation/draft_texts:
one: "Borrador" one: "Borrador"
other: "Borradores" other: "Borradores"
legislation/questions:
one: "Pregunta"
other: "Preguntas"
legislation/question_options:
one: "Opción de respuesta cerrada"
other: "Opciones de respuesta cerrada"
legislation/answers:
one: "Respuesta"
other: "Respuestas"
attributes: attributes:
comment: comment:
body: "Comentario" body: "Comentario"
@@ -99,6 +108,10 @@ es:
changelog: Cambios changelog: Cambios
status: Estado status: Estado
final_version: Versión final final_version: Versión final
legislation/questions:
title: "Título"
legislation/question_options:
value: "Valor"
errors: errors:
models: models:
user: user:

View File

@@ -270,6 +270,7 @@ en:
other: "%{count} comments" other: "%{count} comments"
debate: Debate debate: Debate
show: show:
answer_question: Submit answer
comments: Comments comments: Comments
next_question: Next question next_question: Next question
share: Share share: Share

View File

@@ -270,6 +270,7 @@ es:
other: "%{count} comentarios" other: "%{count} comentarios"
debate: Debate debate: Debate
show: show:
answer_question: Enviar respuesta
comments: Comentarios comments: Comentarios
next_question: Siguiente pregunta next_question: Siguiente pregunta
share: Compartir share: Compartir

View File

@@ -1,15 +1,16 @@
require 'rails_helper' require 'rails_helper'
feature 'Legislation' do feature 'Legislation' do
context 'process debate page' do context 'process debate page' do
scenario 'shows question list' 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 - 1.day, debate_end_date: Date.current + 2.days)
create(:legislation_question, process: process, title: "Question 1") create(:legislation_question, process: @process, title: "Question 1")
create(:legislation_question, process: process, title: "Question 2") create(:legislation_question, process: @process, title: "Question 2")
create(:legislation_question, process: process, title: "Question 3") create(:legislation_question, process: @process, title: "Question 3")
end
visit legislation_process_path(process) scenario 'shows question list' do
visit legislation_process_path(@process)
expect(page).to have_content("Participate in the debate") expect(page).to have_content("Participate in the debate")
@@ -32,5 +33,59 @@ feature 'Legislation' do
expect(page).to have_content("Question 3") expect(page).to have_content("Question 3")
expect(page).to_not have_content("Next question") expect(page).to_not have_content("Next question")
end end
scenario 'shows question page' do
visit legislation_process_question_path(@process, @process.questions.first)
expect(page).to have_content("Question 1")
expect(page).to have_content("Comments (0)")
end
scenario 'shows next question link in question page' do
visit legislation_process_question_path(@process, @process.questions.first)
expect(page).to have_content("Question 1")
expect(page).to have_content("Next question")
click_link "Next question"
expect(page).to have_content("Question 2")
expect(page).to have_content("Next question")
click_link "Next question"
expect(page).to have_content("Question 3")
expect(page).to_not have_content("Next question")
end
scenario 'answer question' do
question = @process.questions.first
create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_question_option, question: question, value: "No")
option = 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_content("Yes")
expect(page).to have_content("No")
expect(page).to have_content("I don't know")
expect(page).to have_selector(:link_or_button, "Submit answer")
choose("I don't know")
click_button "Submit answer"
within(:css, "label.active") do
expect(page).to have_content("I don't know")
expect(page).to_not have_content("Yes")
expect(page).to_not have_content("No")
end
expect(page).to_not have_selector(:link_or_button, "Submit answer")
expect(question.reload.answers_count).to eq(1)
expect(option.reload.answers_count).to eq(1)
end
end end
end end