diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index b73c07474..ce9861547 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -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(){
diff --git a/app/assets/javascripts/polls.js.coffee b/app/assets/javascripts/polls.js.coffee
new file mode 100644
index 000000000..ace7f895e
--- /dev/null
+++ b/app/assets/javascripts/polls.js.coffee
@@ -0,0 +1,21 @@
+App.Polls =
+ generateToken: ->
+ rand = Math.random().toString(36).substr(2) # remove `0.`
+ token = rand + rand # to make it longer
+ 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) ->
+ token_message = $(".js-token-message")
+ token_message.html(token_message.html() + "
" + @token + "");
+ token_message.show()
+ false
diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb
index ccd521294..da68f2d91 100644
--- a/app/models/poll/answer.rb
+++ b/app/models/poll/answer.rb
@@ -17,9 +17,8 @@ class Poll::Answer < ActiveRecord::Base
scope :by_question, ->(question_id) { where(question_id: question_id) }
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.current unless poll_voter.token_seen_at.present?
+ Poll::Voter.find_or_create_by(user: author, poll: poll, origin: "web") do |poll_voter|
+ poll_voter.token = token unless poll_voter.token.present?
end
end
end
diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb
index 8188c4cd1..3801a1964 100644
--- a/app/views/polls/questions/_answers.html.erb
+++ b/app/views/polls/questions/_answers.html.erb
@@ -12,7 +12,7 @@
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 %>
diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb
index bc9cefd2b..be743f2f1 100644
--- a/app/views/polls/show.html.erb
+++ b/app/views/polls/show.html.erb
@@ -38,6 +38,13 @@
<%= t("polls.show.already_voted_in_booth") %>
<% else %>
+
+ <% if !@poll.voters.find_by_user_id(current_user).present? %>
+