Merge pull request #2073 from consul/feature/2072#remove_question_valid_answers

Remove valid answers usage
This commit is contained in:
BertoCQ
2017-10-18 11:52:14 +02:00
committed by GitHub
19 changed files with 83 additions and 73 deletions

View File

@@ -15,7 +15,6 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
def new
@polls = Poll.all
@question.valid_answers = I18n.t('poll_questions.default_valid_answers')
proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present?
@question.copy_attributes_from_proposal(proposal)
end
@@ -56,7 +55,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
private
def question_params
params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :valid_answers, :video_url)
params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :video_url)
end
def search_params

View File

@@ -47,7 +47,7 @@ class Officing::ResultsController < Officing::BaseController
results.each_pair do |answer_index, count|
next if count.blank?
answer = question.valid_answers[answer_index.to_i]
answer = question.question_answers.where(given_order: answer_index.to_i + 1).first.title
go_back_to_new if question.blank?
partial_result = ::Poll::PartialResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id,

View File

@@ -9,9 +9,8 @@ class Poll::Answer < ActiveRecord::Base
validates :author, presence: true
validates :answer, presence: true
# temporary skipping validation, review when removing valid_answers
# validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
# unless: ->(a) { a.question.blank? }
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
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) }

View File

@@ -10,8 +10,9 @@ class Poll::PartialResult < ActiveRecord::Base
validates :question, presence: true
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
validates :origin, inclusion: {in: VALID_ORIGINS}
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
unless: ->(a) { a.question.blank? }
validates :origin, inclusion: { in: VALID_ORIGINS }
scope :by_author, ->(author_id) { where(author_id: author_id) }
scope :by_question, ->(question_id) { where(question_id: question_id) }

View File

@@ -39,17 +39,12 @@ class Poll::Question < ActiveRecord::Base
author_visible_name => 'C' }
end
def valid_answers
(super.try(:split, ',')&.compact || []).map(&:strip)
end
def copy_attributes_from_proposal(proposal)
if proposal.present?
self.author = proposal.author
self.author_visible_name = proposal.author.name
self.proposal_id = proposal.id
self.title = proposal.title
self.valid_answers = I18n.t('poll_questions.default_valid_answers')
end
end

View File

@@ -37,11 +37,11 @@
</tr>
</thead>
<tbody>
<% question.valid_answers.each_with_index do |answer, i| %>
<% question.question_answers.each_with_index do |answer, i| %>
<% by_answer = by_question[question.id].present? ? by_question[question.id].group_by(&:answer) : {} %>
<tr id="question_<%= question.id %>_<%= i %>_result">
<td><%= answer %></td>
<td class="text-center"><%= by_answer[answer].present? ? by_answer[answer].sum(&:amount) : 0 %></td>
<td><%= answer.title %></td>
<td class="text-center"><%= by_answer[answer.title].present? ? by_answer[answer.title].sum(&:amount) : 0 %></td>
</tr>
<% end %>
</tbody>

View File

@@ -40,11 +40,11 @@
</tr>
</thead>
<tbody>
<% question.valid_answers.each_with_index do |answer, i| %>
<% question.question_answers.each_with_index do |answer, i| %>
<% by_answer = by_question[question.id].present? ? by_question[question.id].group_by(&:answer) : {} %>
<tr id="question_<%= question.id %>_<%= i %>_result">
<td><%= answer %></td>
<td><%= by_answer[answer].present? ? by_answer[answer].first.amount : 0 %></td>
<td><%= answer.title %></td>
<td><%= by_answer[answer.title].present? ? by_answer[answer.title].first.amount : 0 %></td>
</tr>
<% end %>
</tbody>

View File

@@ -17,9 +17,9 @@
<div class="small-12 column">
<h3><%= question.title %></h3>
</div>
<% question.valid_answers.each_with_index do |answer, i| %>
<% question.question_answers.each_with_index do |answer, i| %>
<div class="small-12 medium-6 large-3 column end">
<label><%= answer %></label>
<label><%= answer.title %></label>
<%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %>
</div>
<% end %>