Added token to poll voters

This commit is contained in:
María Checa
2017-10-06 18:43:00 +02:00
parent 6994f6d7fe
commit 35148015b9
6 changed files with 16 additions and 12 deletions

View File

@@ -7,11 +7,11 @@ class Polls::QuestionsController < ApplicationController
def answer
answer = @question.answers.find_or_initialize_by(author: current_user)
token = params[:token]
answer.answer = params[:answer]
answer.token = params[:token]
answer.save!
answer.record_voter_participation
answer.record_voter_participation(token)
@answers_by_question_id = { @question.id => params[:answer] }
end

View File

@@ -14,7 +14,6 @@ class PollsController < ApplicationController
def show
@questions = @poll.questions.for_render.sort_for_list
@token = poll_answer_author_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|

View File

@@ -17,7 +17,10 @@ class Poll::Answer < ActiveRecord::Base
scope :by_author, ->(author_id) { where(author_id: author_id) }
scope :by_question, ->(question_id) { where(question_id: question_id) }
def record_voter_participation
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web")
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?
end
end
end

View File

@@ -1,5 +0,0 @@
class AddTokenToPollAnswer < ActiveRecord::Migration
def change
add_column :poll_answers, :token, :string
end
end

View File

@@ -0,0 +1,6 @@
class AddTokenToPollVoters < ActiveRecord::Migration
def change
add_column :poll_voters, :token, :string
add_column :poll_voters, :token_seen_at, :date
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171006102811) do
ActiveRecord::Schema.define(version: 20171006145053) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -592,7 +592,6 @@ ActiveRecord::Schema.define(version: 20171006102811) do
t.string "answer"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
end
add_index "poll_answers", ["author_id"], name: "index_poll_answers_on_author_id", using: :btree
@@ -761,6 +760,8 @@ ActiveRecord::Schema.define(version: 20171006102811) do
t.integer "user_id"
t.string "origin"
t.integer "officer_id"
t.string "token"
t.date "token_seen_at"
end
add_index "poll_voters", ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id", using: :btree