Merge pull request #1994 from consul/feature/1985#voting_token
Voter Answer unique Token
This commit is contained in:
@@ -70,6 +70,7 @@
|
||||
//= require polls_admin
|
||||
//= require leaflet
|
||||
//= require map
|
||||
//= require polls
|
||||
|
||||
var initialize_modules = function() {
|
||||
App.Comments.initialize();
|
||||
@@ -108,6 +109,7 @@ var initialize_modules = function() {
|
||||
App.TagAutocomplete.initialize();
|
||||
App.PollsAdmin.initialize();
|
||||
App.Map.initialize();
|
||||
App.Polls.initialize();
|
||||
};
|
||||
|
||||
$(function(){
|
||||
|
||||
28
app/assets/javascripts/polls.js.coffee
Normal file
28
app/assets/javascripts/polls.js.coffee
Normal file
@@ -0,0 +1,28 @@
|
||||
App.Polls =
|
||||
generateToken: ->
|
||||
token = ''
|
||||
rand = ''
|
||||
for n in [0..5]
|
||||
rand = Math.random().toString(36).substr(2) # remove `0.`
|
||||
token = token + rand;
|
||||
|
||||
token = token.substring(0, 64)
|
||||
return token
|
||||
|
||||
replaceToken: ->
|
||||
for link in $('.js-question-answer')
|
||||
token_param = link.search.slice(-6)
|
||||
if token_param == "token="
|
||||
link.href = link.href + @token
|
||||
|
||||
initialize: ->
|
||||
@token = App.Polls.generateToken()
|
||||
App.Polls.replaceToken()
|
||||
|
||||
$(".js-question-answer").on
|
||||
click: =>
|
||||
token_message = $(".js-token-message")
|
||||
if !token_message.is(':visible')
|
||||
token_message.html(token_message.html() + "<br><strong>" + @token + "</strong>");
|
||||
token_message.show()
|
||||
false
|
||||
@@ -7,11 +7,12 @@ class Polls::QuestionsController < ApplicationController
|
||||
|
||||
def answer
|
||||
answer = @question.answers.find_or_initialize_by(author: current_user)
|
||||
token = params[:token]
|
||||
|
||||
answer.answer = params[:answer]
|
||||
answer.touch if answer.persisted?
|
||||
answer.save!
|
||||
answer.record_voter_participation
|
||||
answer.record_voter_participation(token)
|
||||
|
||||
@answers_by_question_id = { @question.id => params[:answer] }
|
||||
end
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
class PollsController < ApplicationController
|
||||
|
||||
include PollsHelper
|
||||
|
||||
load_and_authorize_resource
|
||||
|
||||
has_filters %w{current expired incoming}
|
||||
@@ -12,7 +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,6 +41,10 @@ module PollsHelper
|
||||
booth.name + location
|
||||
end
|
||||
|
||||
def poll_voter_token(poll, user)
|
||||
Poll::Voter.where(poll: poll, user: user, origin: "web").first&.token || ''
|
||||
end
|
||||
|
||||
def voted_before_sign_in(question)
|
||||
question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at >= vote.updated_at }
|
||||
end
|
||||
|
||||
@@ -16,7 +16,7 @@ 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", token: token)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,11 +10,11 @@
|
||||
</span>
|
||||
<% else %>
|
||||
<%= link_to answer.title,
|
||||
answer_question_path(question, answer: answer.title),
|
||||
answer_question_path(question, answer: answer.title, token: token),
|
||||
method: :post,
|
||||
remote: true,
|
||||
title: t("poll_questions.show.vote_answer", answer: answer.title),
|
||||
class: "button secondary hollow" %>
|
||||
class: "button secondary hollow js-question-answer" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
</h3>
|
||||
|
||||
<div id="<%= dom_id(question) %>_answers" class="padding">
|
||||
<%= render 'polls/questions/answers', question: question %>
|
||||
<%= render 'polls/questions/answers', question: question, token: token %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
$("#<%= dom_id(@question) %>_answers").html('<%= j render("polls/questions/answers", question: @question) %>');
|
||||
<% token = poll_voter_token(@question.poll, current_user) %>
|
||||
$("#<%= dom_id(@question) %>_answers").html('<%= j render("polls/questions/answers", question: @question, token: token) %>');
|
||||
|
||||
@@ -38,13 +38,21 @@
|
||||
<%= t("polls.show.already_voted_in_booth") %>
|
||||
</div>
|
||||
<% else %>
|
||||
|
||||
<% if poll_voter_token(@poll, current_user).empty? %>
|
||||
<div class="callout primary js-token-message" style="display: none">
|
||||
<%= t('poll_questions.show.voted_token') %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if current_user && !@poll.votable_by?(current_user) %>
|
||||
<div class="callout warning">
|
||||
<%= t("polls.show.already_voted_in_web") %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% @questions.each do |question| %>
|
||||
<%= render 'polls/questions/question', question: question %>
|
||||
<%= render 'polls/questions/question', question: question, token: @token %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -500,6 +500,7 @@ en:
|
||||
show:
|
||||
vote_answer: "Vote %{answer}"
|
||||
voted: "You have voted %{answer}"
|
||||
voted_token: "You can write down this vote identifier, to check your vote on the final results:"
|
||||
proposal_notifications:
|
||||
new:
|
||||
title: "Send message"
|
||||
|
||||
@@ -500,6 +500,7 @@ es:
|
||||
show:
|
||||
vote_answer: "Votar %{answer}"
|
||||
voted: "Has votado %{answer}"
|
||||
voted_token: "Puedes apuntar este identificador de voto, para comprobar tu votación en el resultado final:"
|
||||
proposal_notifications:
|
||||
new:
|
||||
title: "Enviar mensaje"
|
||||
|
||||
5
db/migrate/20171006145053_add_token_to_poll_voters.rb
Normal file
5
db/migrate/20171006145053_add_token_to_poll_voters.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddTokenToPollVoters < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :poll_voters, :token, :string
|
||||
end
|
||||
end
|
||||
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20171004210108) do
|
||||
ActiveRecord::Schema.define(version: 20171006145053) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -768,6 +768,7 @@ ActiveRecord::Schema.define(version: 20171004210108) do
|
||||
t.integer "user_id"
|
||||
t.string "origin"
|
||||
t.integer "officer_id"
|
||||
t.string "token"
|
||||
end
|
||||
|
||||
add_index "poll_voters", ["booth_assignment_id"], name: "index_poll_voters_on_booth_assignment_id", using: :btree
|
||||
|
||||
@@ -32,6 +32,11 @@ feature "Voter" do
|
||||
expect(page).to_not have_link('Yes')
|
||||
end
|
||||
|
||||
find(:css, ".js-token-message").should be_visible
|
||||
token = find(:css, ".js-question-answer")[:href].gsub(/.+?(?=token)/, '').gsub('token=', '')
|
||||
|
||||
expect(page).to have_content "You can write down this vote identifier, to check your vote on the final results: #{token}"
|
||||
|
||||
expect(Poll::Voter.count).to eq(1)
|
||||
expect(Poll::Voter.first.origin).to eq("web")
|
||||
end
|
||||
@@ -101,6 +106,8 @@ feature "Voter" do
|
||||
|
||||
visit poll_path(poll)
|
||||
|
||||
expect(page).to_not have_selector('.js-token-message')
|
||||
|
||||
expect(page).to have_content "You have already participated in this poll. If you vote again it will be overwritten."
|
||||
within("#poll_question_#{question.id}_answers") do
|
||||
expect(page).to_not have_link('Yes')
|
||||
|
||||
@@ -46,7 +46,7 @@ describe Poll::Answer do
|
||||
answer = create(:poll_answer, question: question, author: author, answer: "Yes")
|
||||
expect(answer.poll.voters).to be_blank
|
||||
|
||||
answer.record_voter_participation
|
||||
answer.record_voter_participation('token')
|
||||
expect(poll.reload.voters.size).to eq(1)
|
||||
voter = poll.voters.first
|
||||
|
||||
@@ -57,12 +57,12 @@ describe Poll::Answer do
|
||||
|
||||
it "updates a poll_voter with user and poll data" do
|
||||
answer = create(:poll_answer, question: question, author: author, answer: "Yes")
|
||||
answer.record_voter_participation
|
||||
answer.record_voter_participation('token')
|
||||
|
||||
expect(poll.reload.voters.size).to eq(1)
|
||||
|
||||
answer = create(:poll_answer, question: question, author: author, answer: "No")
|
||||
answer.record_voter_participation
|
||||
answer.record_voter_participation('token')
|
||||
|
||||
expect(poll.reload.voters.size).to eq(1)
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ describe :voter do
|
||||
|
||||
it "should not be valid if the user has voted via web" do
|
||||
answer = create(:poll_answer)
|
||||
answer.record_voter_participation
|
||||
answer.record_voter_participation('token')
|
||||
|
||||
voter = build(:poll_voter, poll: answer.question.poll, user: answer.author)
|
||||
expect(voter).to_not be_valid
|
||||
@@ -162,11 +162,12 @@ describe :voter do
|
||||
|
||||
it "sets user info" do
|
||||
user = create(:user, document_number: "1234A", document_type: "1")
|
||||
voter = build(:poll_voter, user: user)
|
||||
voter = build(:poll_voter, user: user, token: "1234abcd")
|
||||
voter.save
|
||||
|
||||
expect(voter.document_number).to eq("1234A")
|
||||
expect(voter.document_type).to eq("1")
|
||||
expect(voter.token).to eq("1234abcd")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user