diff --git a/app/controllers/legislation/answers_controller.rb b/app/controllers/legislation/answers_controller.rb new file mode 100644 index 000000000..156cbc68b --- /dev/null +++ b/app/controllers/legislation/answers_controller.rb @@ -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 diff --git a/app/controllers/legislation/questions_controller.rb b/app/controllers/legislation/questions_controller.rb index 7114c98cb..d50443281 100644 --- a/app/controllers/legislation/questions_controller.rb +++ b/app/controllers/legislation/questions_controller.rb @@ -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 diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index e22bcb7e0..466b6a5ba 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -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 diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index dbc31c7b3..86b250b18 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -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 diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index e80d169ce..7df8f67cf 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -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 diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 6da3b23fd..8257b3c8a 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -44,7 +44,7 @@ <% end %> <% if feature?(:legislation) %> -
  • > +
  • > <%= link_to admin_legislation_processes_path do %> <%= t("admin.menu.legislation") %> <% end %> diff --git a/app/views/admin/legislation/questions/index.html.erb b/app/views/admin/legislation/questions/index.html.erb index 7d41f1b83..f0a4638ee 100644 --- a/app/views/admin/legislation/questions/index.html.erb +++ b/app/views/admin/legislation/questions/index.html.erb @@ -29,7 +29,9 @@ <%= 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 %> diff --git a/app/views/legislation/questions/show.html.erb b/app/views/legislation/questions/show.html.erb index d22289d28..eda42d239 100644 --- a/app/views/legislation/questions/show.html.erb +++ b/app/views/legislation/questions/show.html.erb @@ -23,16 +23,19 @@

    <%= @question.title %>

    -
    - <% @question.question_options.each do |question_option| %> - + <% 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| %> + + <% end %> + <%= f.submit t('.answer_question'), class: "button" unless @answer.persisted? %> <% end %> -
    -
    + <% end %> +