Merge branch 'master' into fix/shift_creation_polls
This commit is contained in:
30
app/controllers/admin/poll/questions/answers_controller.rb
Normal file
30
app/controllers/admin/poll/questions/answers_controller.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
|
||||
before_action :load_question
|
||||
|
||||
load_and_authorize_resource :question, class: "::Poll::Question"
|
||||
|
||||
def new
|
||||
@answer = ::Poll::Question::Answer.new
|
||||
end
|
||||
|
||||
def create
|
||||
@answer = ::Poll::Question::Answer.new(answer_params)
|
||||
|
||||
if @answer.save
|
||||
redirect_to admin_question_path(@question),
|
||||
notice: t("flash.actions.create.poll_question_answer")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def answer_params
|
||||
params.require(:poll_question_answer).permit(:title, :description, :poll_question_id)
|
||||
end
|
||||
|
||||
def load_question
|
||||
@question = ::Poll::Question.find(params[:question_id])
|
||||
end
|
||||
end
|
||||
@@ -12,7 +12,7 @@ class Polls::QuestionsController < ApplicationController
|
||||
answer.save!
|
||||
answer.record_voter_participation
|
||||
|
||||
@answers_by_question_id = {@question.id => params[:answer]}
|
||||
@answers_by_question_id = { @question.id => params[:answer] }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -8,8 +8,10 @@ class Poll::Answer < ActiveRecord::Base
|
||||
validates :question, presence: true
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
|
||||
unless: ->(a) { a.question.blank? }
|
||||
|
||||
# temporary skipping validation, review when removing valid_answers
|
||||
# validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
|
||||
# unless: ->(a) { a.question.blank? }
|
||||
|
||||
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
||||
scope :by_question, ->(question_id) { where(question_id: question_id) }
|
||||
|
||||
@@ -14,7 +14,8 @@ class Poll::Question < ActiveRecord::Base
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
|
||||
has_many :comments, as: :commentable
|
||||
has_many :answers
|
||||
has_many :answers, class_name: 'Poll::Answer'
|
||||
has_many :question_answers, class_name: 'Poll::Question::Answer'
|
||||
has_many :partial_results
|
||||
belongs_to :proposal
|
||||
|
||||
|
||||
9
app/models/poll/question/answer.rb
Normal file
9
app/models/poll/question/answer.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class Poll::Question::Answer < ActiveRecord::Base
|
||||
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
|
||||
|
||||
validates :title, presence: true
|
||||
|
||||
def description
|
||||
super.try :html_safe
|
||||
end
|
||||
end
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
<%= f.text_field :title, maxlength: Poll::Question.title_max_length %>
|
||||
|
||||
<%= f.label :valid_answers %>
|
||||
<p class="help-text" id="valid-answers-help-text"><%= t("admin.questions.new.valid_answers_note") %></p>
|
||||
<%= f.text_field :valid_answers, label: false, aria: {describedby: "valid-answers-help-text"} %>
|
||||
|
||||
<div class="documents small-12">
|
||||
<%= render 'documents/nested_documents', documentable: @question, f: f %>
|
||||
</div>
|
||||
|
||||
24
app/views/admin/poll/questions/answers/_form.html.erb
Normal file
24
app/views/admin/poll/questions/answers/_form.html.erb
Normal file
@@ -0,0 +1,24 @@
|
||||
<%= form_for(@answer, url: form_url) do |f| %>
|
||||
|
||||
<%= render 'shared/errors', resource: @answer %>
|
||||
|
||||
<%= f.hidden_field :question_id, value: @question.id %>
|
||||
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<%= f.text_field :title %>
|
||||
|
||||
<div class="ckeditor">
|
||||
<%= f.cktext_area :description,
|
||||
maxlength: Poll::Question.description_max_length,
|
||||
ckeditor: { language: I18n.locale } %>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="actions small-12 medium-4 column margin-top">
|
||||
<%= f.submit(class: "button expanded", value: t("shared.save")) %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
7
app/views/admin/poll/questions/answers/new.html.erb
Normal file
7
app/views/admin/poll/questions/answers/new.html.erb
Normal file
@@ -0,0 +1,7 @@
|
||||
<%= back_link_to %>
|
||||
|
||||
<h2><%= t('admin.answers.new.title') %></h2>
|
||||
|
||||
<div class="poll-question-answer-form">
|
||||
<%= render "form", form_url: admin_question_answers_path %>
|
||||
</div>
|
||||
@@ -24,15 +24,30 @@
|
||||
<%= link_to @question.author.name, user_path(@question.author) %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong><%= t("admin.questions.show.valid_answers") %></strong>
|
||||
</p>
|
||||
<table>
|
||||
<tr>
|
||||
<th colspan="1" scope="colgroup">
|
||||
<%= t('admin.questions.show.valid_answers') %>
|
||||
</th>
|
||||
<th colspan="1" scope="colgroup">
|
||||
<%= link_to t("admin.questions.show.add_answer"),
|
||||
new_admin_question_answer_path(@question),
|
||||
class: "button hollow float-right" %>
|
||||
</th>
|
||||
</tr>
|
||||
|
||||
<% @question.valid_answers.each do |answer| %>
|
||||
<span class="button">
|
||||
<%= answer %>
|
||||
</span>
|
||||
<% end %>
|
||||
<tr>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.title") %></th>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.description") %></th>
|
||||
</tr>
|
||||
|
||||
<% @question.question_answers.each do |answer| %>
|
||||
<tr id="<%= dom_id(answer) %>" class="poll_question_answer">
|
||||
<th scope="col"><%= answer.title %></th>
|
||||
<th scope="col"><%= answer.description %></th>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<% if @question.video_url.present? %>
|
||||
<p>
|
||||
|
||||
@@ -18,5 +18,9 @@
|
||||
<div class="callout alert">
|
||||
<%= t('polls.show.cant_answer_expired') %>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="callout warning">
|
||||
<%= t('polls.show.cant_answer_wrong_geozone') %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<% if poll.incoming? %>
|
||||
<div class="callout primary">
|
||||
<%= t('poll_questions.show.cant_answer_incoming') %>
|
||||
</div>
|
||||
<% elsif poll.expired? %>
|
||||
<div class="callout alert">
|
||||
<%= t('poll_questions.show.cant_answer_expired') %>
|
||||
</div>
|
||||
<% elsif current_user.nil? %>
|
||||
<div class="callout primary">
|
||||
<%= t("poll_questions.show.not_logged_in",
|
||||
signin: link_to(t("poll_questions.show.signin"), new_user_session_path, class: "probe-message"),
|
||||
signup: link_to(t("poll_questions.show.signup"), new_user_registration_path, class: "probe-message")).html_safe %>
|
||||
</div>
|
||||
<% elsif current_user.unverified? %>
|
||||
<div class="callout warning">
|
||||
<%= t('poll_questions.show.cant_answer_verify_html',
|
||||
verify_link: link_to(t('poll_questions.show.verify_link'), verification_path)) %>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="callout warning">
|
||||
<%= t('poll_questions.show.cant_answer_wrong_geozone') %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -1,22 +1,23 @@
|
||||
<div class="poll-question-answers">
|
||||
<% if can? :answer, question %>
|
||||
<% question.valid_answers.each do |answer| %>
|
||||
<% if @answers_by_question_id[question.id] == answer %>
|
||||
<span class="button answered" title="<%= t("poll_questions.show.voted", answer: answer)%>">
|
||||
<%= answer %>
|
||||
<% question.question_answers.each do |answer| %>
|
||||
<% if @answers_by_question_id[question.id] == answer.title %>
|
||||
<span class="button answered"
|
||||
title="<%= t("poll_questions.show.voted", answer: answer)%>">
|
||||
<%= answer.title %>
|
||||
</span>
|
||||
<% else %>
|
||||
<%= link_to answer,
|
||||
answer_question_path(question, answer: answer),
|
||||
<%= link_to answer.title,
|
||||
answer_question_path(question, answer: answer.title),
|
||||
method: :post,
|
||||
remote: true,
|
||||
title: t("poll_questions.show.vote_answer", answer: answer),
|
||||
title: t("poll_questions.show.vote_answer", answer: answer.title),
|
||||
class: "button secondary hollow" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<% question.valid_answers.each do |answer| %>
|
||||
<span class="button secondary hollow disabled"><%= answer %></span>
|
||||
<% question.question_answers.each do |answer| %>
|
||||
<span class="button secondary hollow disabled"><%= answer.title %></span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user