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? %> + + <% end %> + <% @questions.each do |question| %> <%= render 'polls/questions/question', question: question, token: @token %> <% end %> diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 13ab81041..8f999d0ec 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -497,7 +497,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." + voted_token: "You can write down this vote identifier, to check your vote on the final results:" proposal_notifications: new: title: "Send message" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 1650fb37b..677b03a4a 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -497,7 +497,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" + voted_token: "Puedes apuntar este identificador de voto, para comprobar tu votación en el resultado final:" proposal_notifications: new: title: "Enviar mensaje" diff --git a/db/migrate/20171006145053_add_token_to_poll_voters.rb b/db/migrate/20171006145053_add_token_to_poll_voters.rb index 01e68031e..5d07f5065 100644 --- a/db/migrate/20171006145053_add_token_to_poll_voters.rb +++ b/db/migrate/20171006145053_add_token_to_poll_voters.rb @@ -1,6 +1,5 @@ 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 be956a309..cafec3a82 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -761,7 +761,6 @@ ActiveRecord::Schema.define(version: 20171006145053) do 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