diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e81e74f34..bccb2132b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -50,6 +50,7 @@ //= require markdown_editor //= require cocoon //= require allegations +//= require legislation_questions //= require custom var initialize_modules = function() { @@ -74,6 +75,7 @@ var initialize_modules = function() { App.SocialShare.initialize(); App.MarkdownEditor.initialize(); App.Allegations.initialize(); + App.LegislationQuestions.initialize(); }; $(function(){ diff --git a/app/assets/javascripts/legislation_questions.js.coffee b/app/assets/javascripts/legislation_questions.js.coffee new file mode 100644 index 000000000..f8bea46f3 --- /dev/null +++ b/app/assets/javascripts/legislation_questions.js.coffee @@ -0,0 +1,8 @@ +App.LegislationQuestions = + + initialize: -> + $('form#new_legislation_answer input.button').hide() + $('form#new_legislation_answer input[type=radio]').on + click: -> + $('form#new_legislation_answer').submit() + diff --git a/app/assets/javascripts/votes.js.coffee b/app/assets/javascripts/votes.js.coffee index b842c3ffc..22f7653b7 100644 --- a/app/assets/javascripts/votes.js.coffee +++ b/app/assets/javascripts/votes.js.coffee @@ -13,4 +13,5 @@ App.Votes = initialize: -> App.Votes.hoverize "div.votes" App.Votes.hoverize "div.supports" + App.Votes.hoverize "div.debate-questions" false diff --git a/app/assets/stylesheets/legislation_process.scss b/app/assets/stylesheets/legislation_process.scss index cf2f40f6d..c178cc36a 100644 --- a/app/assets/stylesheets/legislation_process.scss +++ b/app/assets/stylesheets/legislation_process.scss @@ -296,6 +296,7 @@ } .debate-questions { + position: relative; list-style: none; .control { diff --git a/app/controllers/legislation/answers_controller.rb b/app/controllers/legislation/answers_controller.rb new file mode 100644 index 000000000..ac77ea5f1 --- /dev/null +++ b/app/controllers/legislation/answers_controller.rb @@ -0,0 +1,41 @@ +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 + + 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.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 + def answer_params + params.require(:legislation_answer).permit( + :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/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/answer.rb b/app/models/legislation/answer.rb new file mode 100644 index 000000000..fd4bc90ed --- /dev/null +++ b/app/models/legislation/answer.rb @@ -0,0 +1,9 @@ +class Legislation::Answer < ActiveRecord::Base + belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :answers, counter_cache: true + belongs_to :question_option, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_option_id', dependent: :destroy, inverse_of: :answers, counter_cache: true + belongs_to :user, dependent: :destroy, inverse_of: :legislation_answers + + validates :question, presence: true, uniqueness: { scope: :user_id} + validates :question_option, presence: true + validates :user, presence: true +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 2b779d1f8..7df8f67cf 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -5,7 +5,8 @@ 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 accepts_nested_attributes_for :question_options, :reject_if => proc { |attributes| attributes[:value].blank? }, allow_destroy: true @@ -16,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/models/legislation/question_option.rb b/app/models/legislation/question_option.rb index 93bc20320..f7927dd1a 100644 --- a/app/models/legislation/question_option.rb +++ b/app/models/legislation/question_option.rb @@ -3,6 +3,7 @@ class Legislation::QuestionOption < ActiveRecord::Base include ActsAsParanoidAliases belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options + has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question validates :question, presence: true validates :value, presence: true, uniqueness: { scope: :legislation_question_id } diff --git a/app/models/user.rb b/app/models/user.rb index 03f2db27b..4ded8d99e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,6 +25,7 @@ class User < ActiveRecord::Base has_many :notifications has_many :direct_messages_sent, class_name: 'DirectMessage', foreign_key: :sender_id has_many :direct_messages_received, class_name: 'DirectMessage', foreign_key: :receiver_id + has_many :legislation_answers, class_name: 'Legislation::Answer', dependent: :destroy, inverse_of: :user belongs_to :geozone validates :username, presence: true, if: :username_required? 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/answers/create.js.erb b/app/views/legislation/answers/create.js.erb new file mode 100644 index 000000000..468b2610b --- /dev/null +++ b/app/views/legislation/answers/create.js.erb @@ -0,0 +1 @@ +$("#legislation-answer-form").html('<%= j render("legislation/questions/answer_form", process: @process, question: @question, answer: @answer) %>'); diff --git a/app/views/legislation/questions/_answer_form.html.erb b/app/views/legislation/questions/_answer_form.html.erb new file mode 100644 index 000000000..775e93faa --- /dev/null +++ b/app/views/legislation/questions/_answer_form.html.erb @@ -0,0 +1,28 @@ +<% if question.question_options.any? %> + <% 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 %> + + <% else %> + +
    + <% question.question_options.each do |question_option| %> + + <% end %> +
    + + <% end %> +<% end %> diff --git a/app/views/legislation/questions/_participation_not_allowed.html.erb b/app/views/legislation/questions/_participation_not_allowed.html.erb new file mode 100644 index 000000000..b330f7fe0 --- /dev/null +++ b/app/views/legislation/questions/_participation_not_allowed.html.erb @@ -0,0 +1,20 @@ +<% if user_signed_in? && current_user.organization? %> + +<% elsif user_signed_in? && current_user.unverified? %> + +<% elsif !user_signed_in? %> + +<% end %> diff --git a/app/views/legislation/questions/show.html.erb b/app/views/legislation/questions/show.html.erb index d22289d28..be8e90445 100644 --- a/app/views/legislation/questions/show.html.erb +++ b/app/views/legislation/questions/show.html.erb @@ -22,17 +22,10 @@

    <%= @question.title %>

    -
    -
    - <% @question.question_options.each do |question_option| %> - - <% end %> -
    -
    +
    + <%= render 'answer_form', process: @process, question: @question, answer: @answer %> + <%= render 'participation_not_allowed' %> +