Switch from Poll::Answer to Poll::Voter usage and small fixes
This commit is contained in:
@@ -14,6 +14,7 @@ class PollsController < ApplicationController
|
||||
|
||||
def show
|
||||
@questions = @poll.questions.for_render.sort_for_list
|
||||
@token = poll_voter_token(@poll, current_user)
|
||||
@answers_by_question_id = {}
|
||||
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
|
||||
poll_answers.each do |answer|
|
||||
|
||||
@@ -41,9 +41,8 @@ module PollsHelper
|
||||
booth.name + location
|
||||
end
|
||||
|
||||
def poll_answer_author_token(poll, author)
|
||||
existing_token = Poll::Answer.where(question: poll.questions, author: author)
|
||||
existing_token.present? ? existing_token.first.token : ''
|
||||
def poll_voter_token(poll, user)
|
||||
Poll::Voter.where(poll: poll, user: user, origin: "web").first&.token || ''
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -8,7 +8,6 @@ class Poll::Answer < ActiveRecord::Base
|
||||
validates :question, presence: true
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
validates :token, presence: true
|
||||
|
||||
# temporary skipping validation, review when removing valid_answers
|
||||
# validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
|
||||
@@ -19,8 +18,8 @@ class Poll::Answer < ActiveRecord::Base
|
||||
|
||||
def record_voter_participation(token)
|
||||
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web") do |poll_voter|
|
||||
poll_voter.token = token unless poll_voter.token.present?
|
||||
poll_voter.token_seen_at = Time.now unless poll_voter.token_seen_at.present?
|
||||
poll_voter.token = token unless poll_voter.token.present?
|
||||
poll_voter.token_seen_at = Time.current unless poll_voter.token_seen_at.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
<% token = poll_answer_author_token(@question.poll, current_user) %>
|
||||
<% token = poll_voter_token(@question.poll, current_user) %>
|
||||
$("#<%= dom_id(@question) %>_answers").html('<%= j render("polls/questions/answers", question: @question, token: token) %>');
|
||||
|
||||
Reference in New Issue
Block a user