Answer legislation questions
This commit is contained in:
21
app/controllers/legislation/answers_controller.rb
Normal file
21
app/controllers/legislation/answers_controller.rb
Normal 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
|
||||
@@ -8,5 +8,6 @@ class Legislation::QuestionsController < Legislation::BaseController
|
||||
@commentable = @question
|
||||
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
|
||||
set_comment_flags(@comment_tree.comments)
|
||||
@answer = @question.answer_for_user(current_user) || Legislation::Answer.new
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,6 +14,7 @@ module Abilities
|
||||
can [:read, :draft_publication, :allegations, :final_version_publication], Legislation::Process
|
||||
can [:read], Legislation::DraftVersion
|
||||
can [:read], Legislation::Question
|
||||
can [:create], Legislation::Answer
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,9 +2,9 @@ class Legislation::Process < ActiveRecord::Base
|
||||
acts_as_paranoid column: :hidden_at
|
||||
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_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 :description, presence: true
|
||||
|
||||
@@ -5,7 +5,7 @@ class Legislation::Question < ActiveRecord::Base
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_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 :comments, as: :commentable
|
||||
|
||||
@@ -17,4 +17,8 @@ class Legislation::Question < ActiveRecord::Base
|
||||
def next_question_id
|
||||
@next_question_id ||= process.questions.where("id > ?", id).order('id ASC').limit(1).pluck(:id).first
|
||||
end
|
||||
|
||||
def answer_for_user(user)
|
||||
answers.where(user: user).first
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
<% end %>
|
||||
|
||||
<% 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 %>
|
||||
<span class="icon-budget"></span><%= t("admin.menu.legislation") %>
|
||||
<% end %>
|
||||
|
||||
@@ -29,7 +29,9 @@
|
||||
<td>
|
||||
<%= content_tag :ul do %>
|
||||
<% 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 %>
|
||||
</td>
|
||||
|
||||
@@ -23,16 +23,19 @@
|
||||
<div class="small-12 medium-9 column">
|
||||
<h3 class="quiz-question"><%= @question.title %></h3>
|
||||
<div class="debate-questions">
|
||||
<form class="controls-stacked">
|
||||
<% @question.question_options.each do |question_option| %>
|
||||
<label class="control radio">
|
||||
<input id="quiz-1" name="radio" type="radio">
|
||||
<span class="control-indicator"></span>
|
||||
<%= question_option.value %>
|
||||
</label>
|
||||
<% if @question.question_options.any? %>
|
||||
<%= form_for @answer, url: legislation_process_question_answers_path(@process, @question, @answer), 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? %>
|
||||
<span class="control-indicator"></span>
|
||||
<%= question_option.value %>
|
||||
</label>
|
||||
<% end %>
|
||||
<%= f.submit t('.answer_question'), class: "button" unless @answer.persisted? %>
|
||||
<% end %>
|
||||
</form>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<aside class="small-12 medium-3 column">
|
||||
|
||||
@@ -40,6 +40,15 @@ en:
|
||||
legislation/draft_versions:
|
||||
one: "Draft version"
|
||||
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:
|
||||
comment:
|
||||
body: "Comment"
|
||||
@@ -99,6 +108,10 @@ en:
|
||||
changelog: Changes
|
||||
status: Status
|
||||
final_version: Final version
|
||||
legislation/questions:
|
||||
title: "Title"
|
||||
legislation/question_options:
|
||||
value: "Value"
|
||||
errors:
|
||||
models:
|
||||
user:
|
||||
|
||||
@@ -40,6 +40,15 @@ es:
|
||||
legislation/draft_texts:
|
||||
one: "Borrador"
|
||||
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:
|
||||
comment:
|
||||
body: "Comentario"
|
||||
@@ -99,6 +108,10 @@ es:
|
||||
changelog: Cambios
|
||||
status: Estado
|
||||
final_version: Versión final
|
||||
legislation/questions:
|
||||
title: "Título"
|
||||
legislation/question_options:
|
||||
value: "Valor"
|
||||
errors:
|
||||
models:
|
||||
user:
|
||||
|
||||
@@ -270,6 +270,7 @@ en:
|
||||
other: "%{count} comments"
|
||||
debate: Debate
|
||||
show:
|
||||
answer_question: Submit answer
|
||||
comments: Comments
|
||||
next_question: Next question
|
||||
share: Share
|
||||
|
||||
@@ -270,6 +270,7 @@ es:
|
||||
other: "%{count} comentarios"
|
||||
debate: Debate
|
||||
show:
|
||||
answer_question: Enviar respuesta
|
||||
comments: Comentarios
|
||||
next_question: Siguiente pregunta
|
||||
share: Compartir
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'Legislation' do
|
||||
|
||||
context 'process debate page' do
|
||||
scenario 'shows question list' do
|
||||
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 2")
|
||||
create(:legislation_question, process: process, title: "Question 3")
|
||||
before(:each) do
|
||||
@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 2")
|
||||
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")
|
||||
|
||||
@@ -32,5 +33,59 @@ feature 'Legislation' do
|
||||
expect(page).to have_content("Question 3")
|
||||
expect(page).to_not have_content("Next question")
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user