From 35148015b949f77c0fd14dba895246765bcac097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Fri, 6 Oct 2017 18:43:00 +0200 Subject: [PATCH] Added token to poll voters --- app/controllers/polls/questions_controller.rb | 4 ++-- app/controllers/polls_controller.rb | 1 - app/models/poll/answer.rb | 7 +++++-- db/migrate/20171006102811_add_token_to_poll_answer.rb | 5 ----- db/migrate/20171006145053_add_token_to_poll_voters.rb | 6 ++++++ db/schema.rb | 5 +++-- 6 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 db/migrate/20171006102811_add_token_to_poll_answer.rb create mode 100644 db/migrate/20171006145053_add_token_to_poll_voters.rb diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 32f4b8e54..39fc18252 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -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 diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 18a75534c..ce807eda2 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -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| diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index fc4f07c8b..48287053c 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -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 diff --git a/db/migrate/20171006102811_add_token_to_poll_answer.rb b/db/migrate/20171006102811_add_token_to_poll_answer.rb deleted file mode 100644 index 22fee6db9..000000000 --- a/db/migrate/20171006102811_add_token_to_poll_answer.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddTokenToPollAnswer < ActiveRecord::Migration - def change - add_column :poll_answers, :token, :string - end -end diff --git a/db/migrate/20171006145053_add_token_to_poll_voters.rb b/db/migrate/20171006145053_add_token_to_poll_voters.rb new file mode 100644 index 000000000..01e68031e --- /dev/null +++ b/db/migrate/20171006145053_add_token_to_poll_voters.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 04aab5b4d..be956a309 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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