From 75091532075247ae9c47164ea883f4fe803eddf4 Mon Sep 17 00:00:00 2001 From: Date: Wed, 11 Oct 2006 15:48:31 -0400 Subject: [PATCH] Added support for voting in QuickVotes. --- app/controllers/voter_controller.rb | 34 ++++++++++++++++++++--- app/models/election.rb | 4 +++ app/models/full_voter.rb | 12 +++++++++ app/models/quick_vote.rb | 7 ++--- app/models/quick_voter.rb | 4 +++ app/models/vote.rb | 16 +++++------ app/models/voter.rb | 10 ------- app/views/site/index.rhtml | 2 +- app/views/site/success_quickvote.rhtml | 3 ++- app/views/voter/_vote.rhtml | 37 ++++++++++++++++++++++++++ app/views/voter/full_vote.rhtml | 5 ++++ app/views/voter/quickvote.rhtml | 2 ++ app/views/voter/vote.rhtml | 35 ------------------------ config/routes.rb | 12 +++++++++ db/create.sql | 9 ++++--- db/migrate/002_create_full_voters.rb | 11 ++++++++ db/migrate/003_create_quick_voters.rb | 11 ++++++++ test/fixtures/full_voters.yml | 5 ++++ test/fixtures/quick_voters.yml | 5 ++++ test/unit/full_voter_test.rb | 10 +++++++ test/unit/quick_voter_test.rb | 10 +++++++ 21 files changed, 176 insertions(+), 68 deletions(-) create mode 100644 app/models/full_voter.rb create mode 100644 app/models/quick_voter.rb create mode 100644 app/views/voter/_vote.rhtml create mode 100644 app/views/voter/full_vote.rhtml create mode 100644 app/views/voter/quickvote.rhtml delete mode 100644 app/views/voter/vote.rhtml create mode 100644 db/migrate/002_create_full_voters.rb create mode 100644 db/migrate/003_create_quick_voters.rb create mode 100644 test/fixtures/full_voters.yml create mode 100644 test/fixtures/quick_voters.yml create mode 100644 test/unit/full_voter_test.rb create mode 100644 test/unit/quick_voter_test.rb diff --git a/app/controllers/voter_controller.rb b/app/controllers/voter_controller.rb index 2527118..d2529f8 100644 --- a/app/controllers/voter_controller.rb +++ b/app/controllers/voter_controller.rb @@ -1,4 +1,5 @@ class VoterController < ApplicationController + layout 'vb' model :voter model :vote model :election @@ -6,8 +7,8 @@ class VoterController < ApplicationController def index password = params[:id] password = params[:vote][:password] if params[:vote] - if @voter = Voter.find_all( [ "password = ?", password ] )[0] - render :action => 'vote' + if @voter = FullVoter.find_all( [ "password = ?", password ] )[0] + render :action => 'fullvote' end end @@ -29,7 +30,26 @@ class VoterController < ApplicationController end def confirm - if authenticate + if params[:votename] + if Voter.find_all( ["session_id = ?", session.session_id ])[0] + flash[:notice] = "You have already voted!" + redirect_to quickvote_url( :votename => params[:votename] ) + else + @voter = QuickVoter.new() + @voter.election = Election.find_all( [ "name = ?", + params[:votename] ] )[0] + @voter.session_id = session.session_id + @voter.save + @voter.reload + + @voter.vote = Vote.new + @voter.vote.votestring = params[:vote][:votestring] + @voter.vote.save + @voter.vote.confirm! + render :action => 'thanks' + end + + elsif authenticate @voter.vote.confirm! render :action => 'thanks' else @@ -37,9 +57,15 @@ class VoterController < ApplicationController end end + def quickvote + @voter = QuickVoter.new + @voter.election = Election.find_all( [ "name = ?", params[:votename] ] )[0] + end + private def authenticate password = params[:id] - @voter = Voter.find_all( [ "password = ?", password ] )[0] + @voter = FullVoter.find_all( [ "password = ?", password ] )[0] end end + diff --git a/app/models/election.rb b/app/models/election.rb index ba466b4..0860a42 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -39,5 +39,9 @@ class Election < ActiveRecord::Base def activate! self.active = 1 end + + def quickvote? + quickvote.to_i == 1 + end end diff --git a/app/models/full_voter.rb b/app/models/full_voter.rb new file mode 100644 index 0000000..04071a0 --- /dev/null +++ b/app/models/full_voter.rb @@ -0,0 +1,12 @@ +class FullVoter < Voter + before_create :create_password + validates_presence_of :email, :password + + def create_password + token_generator = UniqueTokenGenerator.new( 16 ) + until password and not password.empty? \ + and Voter.find_all( [ "password = ?", password ]).empty? + self.password = token_generator.token + end + end +end diff --git a/app/models/quick_vote.rb b/app/models/quick_vote.rb index d6ab55b..fba31a8 100644 --- a/app/models/quick_vote.rb +++ b/app/models/quick_vote.rb @@ -19,6 +19,7 @@ class QuickVote < Election self.enddate = DateTime.now + 30 self.active = 1 self.anonymous = 1 + self.quickvote = 1 end def candidatelist=(candstring='') @@ -34,11 +35,7 @@ class QuickVote < Election end def reviewed? - if reviewed.to_i == 1 - return true - else - false - end + reviewed.to_i == 1 end def create_candidates diff --git a/app/models/quick_voter.rb b/app/models/quick_voter.rb new file mode 100644 index 0000000..7b934a3 --- /dev/null +++ b/app/models/quick_voter.rb @@ -0,0 +1,4 @@ +class QuickVoter < Voter + validates_presence_of :session_id + validates_uniqueness_of :session_id +end diff --git a/app/models/vote.rb b/app/models/vote.rb index 429e212..7678f54 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -49,18 +49,16 @@ class Vote < ActiveRecord::Base def confirm! self.confirmed = 1 self.save - - token.destroy and token.reload if token - self.token = Token.new - self.save + + unless self.voter.election.quickvote? + token.destroy and token.reload if token + self.token = Token.new + self.save + end end def confirm? - if confirm == 1 - return true - else - return false - end + confirmed == 1 end def votestring=(string="") diff --git a/app/models/voter.rb b/app/models/voter.rb index c4700cb..7139b89 100644 --- a/app/models/voter.rb +++ b/app/models/voter.rb @@ -2,16 +2,6 @@ class Voter < ActiveRecord::Base belongs_to :election has_one :vote - before_create :create_password - - def create_password - token_generator = UniqueTokenGenerator.new( 16 ) - until password and not password.empty? \ - and Voter.find_all( [ "password = ?", password ]).empty? - self.password = token_generator.token - end - end - end diff --git a/app/views/site/index.rhtml b/app/views/site/index.rhtml index 5a68a6b..b005cb5 100644 --- a/app/views/site/index.rhtml +++ b/app/views/site/index.rhtml @@ -12,7 +12,7 @@ HyperChad. They are the quickest way to make a decision using a variety of preferential and non-preficial election methods, or to compare between methods.

-

<%= link_to "Create QuickVote.", :action => 'create_quickvote' %>

+

<%= link_to "Create QuickVote.", :controller => 'quickvote', :action => 'create' %>

Voters

diff --git a/app/views/site/success_quickvote.rhtml b/app/views/site/success_quickvote.rhtml index 654e6d3..299edac 100644 --- a/app/views/site/success_quickvote.rhtml +++ b/app/views/site/success_quickvote.rhtml @@ -9,7 +9,8 @@ HyperChad site. Voters do not need to log in or authenticate to participate in this election.

Direct voters to:

-
<%= url_for :action => 'quickvote', :id => @quickvote.id, :only_path => false %>
+
<%= quickvote_url( :votename => @quickvote.name ) %>

This vote will expire on <%= @quickvote.enddate %>

+

<%= link_to "Visit in or vote in your QuickVote", quickvote_url( :votename => @quickvote.name ) %>

diff --git a/app/views/voter/_vote.rhtml b/app/views/voter/_vote.rhtml new file mode 100644 index 0000000..e742021 --- /dev/null +++ b/app/views/voter/_vote.rhtml @@ -0,0 +1,37 @@ +<% %> + +<% if @voter.election.quickvote? %> +

QuickVote: <%= @voter.election.name %>

+

Description:

+
<%= @voter.election.description %>
+<% else %> +

Election: <%= @voter.election.name %>

+

Voter: <%= @voter.email %>

+

Description:

+
<%= @voter.election.description %>
+<% end %> + +

Candidates:

+
    +<% for candidate in @voter.election.candidates.sort %> +
  1. <%= candidate.name %>
  2. +<% end %> +
+ +
+ +

Place Your Vote Here

+ +

Rank each candidate in order of more preferred to least +preferred. (e.g., 123 or 321 or 213, etc.)

+ +<% if @voter.election.quickvote? %> + <%= form_tag quickconfirm_url( :votename => @voter.election.name ) %> +<% else %> + <%= form_tag :action => 'review', :id => @voter.password %> +<% end %> + +<%= text_field :vote, :votestring -%> +<%= submit_tag "Submit!" %> +<%= end_form_tag %> + diff --git a/app/views/voter/full_vote.rhtml b/app/views/voter/full_vote.rhtml new file mode 100644 index 0000000..599308f --- /dev/null +++ b/app/views/voter/full_vote.rhtml @@ -0,0 +1,5 @@ +<% %> + +<%= render_partial 'vote' %> + + diff --git a/app/views/voter/quickvote.rhtml b/app/views/voter/quickvote.rhtml new file mode 100644 index 0000000..3cb2eda --- /dev/null +++ b/app/views/voter/quickvote.rhtml @@ -0,0 +1,2 @@ +<% %> +<%= render_partial 'vote' %> diff --git a/app/views/voter/vote.rhtml b/app/views/voter/vote.rhtml deleted file mode 100644 index 2d30209..0000000 --- a/app/views/voter/vote.rhtml +++ /dev/null @@ -1,35 +0,0 @@ -<% %> - -

Vote Below the Line

- -

Election: <%= @voter.election.name %>

- -

Voter: <%= @voter.email %>

- -

Candidates:

- -
    -<% for candidate in @voter.election.candidates.sort %> -
  1. <%= candidate.name %>
  2. -<% end %> -
- -

If this information is incorrect, please notify the vote -administrator immediatedly!

- -
- -

Place Your Vote Here

- -

Rank each candidate in order of more preferred to least -preferred. (e.g., 123 or 321 or 213, etc.)

- -<%= form_tag :action => 'review', :id => @voter.password %> -<%= text_field :vote, :votestring -%> -<%= submit_tag "Submit!" %> -<%= end_form_tag %> - - - - - diff --git a/config/routes.rb b/config/routes.rb index ed0625d..af9820f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,18 @@ ActionController::Routing::Routes.draw do |map| # -- just remember to delete public/index.html. map.connect '', :controller => "site" + map.connect 'quickvote/create', + :controller => 'site', + :action => 'create_quickvote' + + map.quickconfirm 'quickvote/:votename/confirm', + :controller => 'voter', + :action => 'confirm' + + map.quickvote 'quickvote/:votename', + :controller => 'voter', + :action => 'quickvote' + # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl' diff --git a/db/create.sql b/db/create.sql index bbc054a..a9d430c 100644 --- a/db/create.sql +++ b/db/create.sql @@ -10,7 +10,8 @@ create table elections ( startdate datetime, enddate datetime NOT NULL, active tinyint NOT NULL DEFAULT 0, - user_id int NOT NULL, + user_id int NULL, + quickvote tinyint NOT NULL DEFAULT 0, primary key (id), constraint fk_user_election foreign key (user_id) references users(id) ); @@ -36,14 +37,16 @@ create table candidates ( drop table if exists voters; create table voters ( id int NOT NULL auto_increment, - email varchar(100) NOT NULL, - password varchar(100) NOT NULL, + email varchar(100) NULL, + password varchar(100) NULL, contacted tinyint NOT NULL DEFAULT 0, election_id int NOT NULL, + session_id varchar(32) DEFAULT NULL, constraint fk_election_voter foreign key (election_id) references election(id), primary key (id) ); + # CREATE tokens TABLE ##################################### diff --git a/db/migrate/002_create_full_voters.rb b/db/migrate/002_create_full_voters.rb new file mode 100644 index 0000000..692a81d --- /dev/null +++ b/db/migrate/002_create_full_voters.rb @@ -0,0 +1,11 @@ +class CreateFullVoters < ActiveRecord::Migration + def self.up + create_table :full_voters do |t| + # t.column :name, :string + end + end + + def self.down + drop_table :full_voters + end +end diff --git a/db/migrate/003_create_quick_voters.rb b/db/migrate/003_create_quick_voters.rb new file mode 100644 index 0000000..21bc293 --- /dev/null +++ b/db/migrate/003_create_quick_voters.rb @@ -0,0 +1,11 @@ +class CreateQuickVoters < ActiveRecord::Migration + def self.up + create_table :quick_voters do |t| + # t.column :name, :string + end + end + + def self.down + drop_table :quick_voters + end +end diff --git a/test/fixtures/full_voters.yml b/test/fixtures/full_voters.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/full_voters.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/fixtures/quick_voters.yml b/test/fixtures/quick_voters.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/quick_voters.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/unit/full_voter_test.rb b/test/unit/full_voter_test.rb new file mode 100644 index 0000000..833ca44 --- /dev/null +++ b/test/unit/full_voter_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class FullVoterTest < Test::Unit::TestCase + fixtures :full_voters + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/unit/quick_voter_test.rb b/test/unit/quick_voter_test.rb new file mode 100644 index 0000000..d6e0a11 --- /dev/null +++ b/test/unit/quick_voter_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class QuickVoterTest < Test::Unit::TestCase + fixtures :quick_voters + + # Replace this with your real tests. + def test_truth + assert true + end +end -- 2.30.2