adds Poll::Answer model for web users

PartialResults is kept for booth results
This commit is contained in:
Juanjo Bazán
2017-01-25 12:46:44 +01:00
parent 8f235c25d3
commit 6bc4f5b307
8 changed files with 58 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
class Polls::QuestionsController < ApplicationController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question'#, through: :poll
load_and_authorize_resource :question, class: 'Poll::Question'
has_orders %w{most_voted newest oldest}, only: :show
@@ -10,17 +10,15 @@ class Polls::QuestionsController < ApplicationController
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
question_answer = @question.partial_results.where(author_id: current_user.try(:id)).first
question_answer = @question.answers.where(author_id: current_user.try(:id)).first
@answers_by_question_id = {@question.id => question_answer.try(:answer)}
end
def answer
partial_result = @question.partial_results.find_or_initialize_by(author: current_user,
amount: 1,
origin: 'web')
answer = @question.answers.find_or_initialize_by(author: current_user)
partial_result.answer = params[:answer]
partial_result.save!
answer.answer = params[:answer]
answer.save!
@answers_by_question_id = {@question.id => params[:answer]}
end

View File

@@ -9,12 +9,12 @@ class PollsController < ApplicationController
end
def show
@questions = @poll.questions.for_render.sort_for_list
@questions = @poll.questions.for_render.sort_for_list
@answers_by_question_id = {}
poll_partial_results = Poll::PartialResult.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_partial_results.each do |result|
@answers_by_question_id[result.question_id] = result.answer
poll_answers = Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_answers.each do |answer|
@answers_by_question_id[answer.question_id] = answer.answer
end
end

13
app/models/poll/answer.rb Normal file
View File

@@ -0,0 +1,13 @@
class Poll::Answer < ActiveRecord::Base
belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
validates :question, presence: true
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
scope :by_author, -> (author_id) { where(author_id: author_id) }
scope :by_question, -> (question_id) { where(question_id: question_id) }
end