Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Created April 2, 2016 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasklemm/c3b044f589ed1a8f395450ac396b4286 to your computer and use it in GitHub Desktop.
Save thomasklemm/c3b044f589ed1a8f395450ac396b4286 to your computer and use it in GitHub Desktop.
Planning the vote casting for GoVote
# config/routes.rb
# "Cast a Vote"-Workflow
get 'vote', to: 'cast_votes#get_voter_token', as: :new_vote
post 'vote', to: 'cast_votes#verify_voter_token'
get 'cast_vote', to: 'cast_votes#get_choices', as: :cast_vote
post 'cast_vote', to: 'cast_votes#cast_vote'
get 'thank_you', to: 'cast_votes#thank_you', as: :thank_you
class CastVotesController < ApplicationController
# Step 1) Put in the voter token
# GET '/vote', GET new_vote_path
def get_voter_token
end
# Step 2) Verify the voter token is valid and usable
# POST '/vote', POST new_vote_path
def verify_voter_token
verify_voter_token!
set_voter_token_in_session(voter_token)
redirect_to cast_vote_path
end
# Step 3) Get choices
# GET '/cast_vote', GET cast_vote_path
def get_choices
verify_voter_token!
load_voter
end
# Step 4) Select a choice and cast the vote
# POST '/cast_vote', POST cast_vote_path
def cast_vote
verify_voter_token!
load_voter
cast_vote!
remove_voter_token_from_session
redirect_to thank_you_path
end
# Step 5) Thanks for voting & sharing
# GET '/thank_you', GET thank_you_path
def thank_you
load_voter
end
private
def current_voter_token
@current_voter_token ||= params[:voter_token].presence ||
(params[:vote] && params[:vote][:voter_token]).presence ||
session[:voter_token].presence
end
def set_voter_token_in_session(voter_token)
session[:voter_token] = voter_token
@current_voter_token = voter_token
end
def remove_voter_token_from_session
session.delete(:voter_token)
@current_voter_token = nil
end
def verify_voter_token!
CastVote.new.verify_voter_token!(voter_token)
rescue CastVote::UnknownVoterToken
rediect_to new_vote_path, alert: t('.verify_voter_token.unknown_voter_token')
rescue CastVote::UsedVoterToken
rediect_to new_vote_path, alert: t('.verify_voter_token.used_voter_token')
end
def load_voter
@voter ||= Voter.find_by!(voter_token: voter_token)
end
def cast_vote!
voter.vote_for!(choice)
# ActiveRecord::Base.transaction do
# voter.has_voted!
# Vote.create!()
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment