]> projects.mako.cc - selectricity-live/commitdiff
Added support for voting in QuickVotes.
author<mako@atdot.cc> <>
Wed, 11 Oct 2006 19:48:31 +0000 (15:48 -0400)
committer<mako@atdot.cc> <>
Wed, 11 Oct 2006 19:48:31 +0000 (15:48 -0400)
21 files changed:
app/controllers/voter_controller.rb
app/models/election.rb
app/models/full_voter.rb [new file with mode: 0644]
app/models/quick_vote.rb
app/models/quick_voter.rb [new file with mode: 0644]
app/models/vote.rb
app/models/voter.rb
app/views/site/index.rhtml
app/views/site/success_quickvote.rhtml
app/views/voter/_vote.rhtml [new file with mode: 0644]
app/views/voter/full_vote.rhtml [new file with mode: 0644]
app/views/voter/quickvote.rhtml [new file with mode: 0644]
app/views/voter/vote.rhtml [deleted file]
config/routes.rb
db/create.sql
db/migrate/002_create_full_voters.rb [new file with mode: 0644]
db/migrate/003_create_quick_voters.rb [new file with mode: 0644]
test/fixtures/full_voters.yml [new file with mode: 0644]
test/fixtures/quick_voters.yml [new file with mode: 0644]
test/unit/full_voter_test.rb [new file with mode: 0644]
test/unit/quick_voter_test.rb [new file with mode: 0644]

index 252711830ac96f7796062b7ab523cf391ed1eab3..d2529f8762416927e7775776ef16ae6f0c6fb6f2 100644 (file)
@@ -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
+
index ba466b4b55928dac2f5291e0513d77b684a8c08d..0860a426ff137d84dc9184ac322725aec05553cf 100644 (file)
@@ -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 (file)
index 0000000..04071a0
--- /dev/null
@@ -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
index d6ab55b124dd2daa328b7452c80880975b553a31..fba31a8e9678f9e5953c3226486b70d9a949ebfa 100644 (file)
@@ -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 (file)
index 0000000..7b934a3
--- /dev/null
@@ -0,0 +1,4 @@
+class QuickVoter < Voter
+  validates_presence_of :session_id
+  validates_uniqueness_of :session_id
+end
index 429e212377781d27aa4a5580d9dcb65abd5b19eb..7678f5463900034de67c6ff2dff60c2d281a059c 100644 (file)
@@ -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="")
index c4700cbbef75d0a82fe8a598c8d454ce5ead4f42..7139b899a725fd6b32de66393f7decc58a5d99c8 100644 (file)
@@ -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
 
 
index 5a68a6b77237ebe9c91a5eb687c7b935025c95e5..b005cb5ccd109facebb79bf52beb65542eefc7ec 100644 (file)
@@ -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.</p>
 
-<p><%= link_to "Create QuickVote.", :action => 'create_quickvote' %></p>
+<p><%= link_to "Create QuickVote.", :controller => 'quickvote', :action => 'create' %></p>
 
 <h2>Voters</h2>
 
index 654e6d3d5408a8dc98f39adc8fee269764aba3b4..299edacdfc54cbc8745c5b7f13ad06271c4f0f48 100644 (file)
@@ -9,7 +9,8 @@ HyperChad site. Voters do not need to log in or authenticate to
 participate in this election.</p>
 
 <p>Direct voters to:</p>
-<blockquote><strong><%= url_for :action => 'quickvote', :id => @quickvote.id, :only_path => false %></strong></blockquote>
+<blockquote><strong><%= quickvote_url( :votename => @quickvote.name ) %></strong></blockquote>
 
 <p>This vote will expire on <em><%= @quickvote.enddate %></em></p>
 
+<p><%= link_to "Visit in or vote in your QuickVote", quickvote_url( :votename => @quickvote.name ) %></p>
diff --git a/app/views/voter/_vote.rhtml b/app/views/voter/_vote.rhtml
new file mode 100644 (file)
index 0000000..e742021
--- /dev/null
@@ -0,0 +1,37 @@
+<% %>
+
+<% if @voter.election.quickvote? %>
+  <h1>QuickVote: <em><%= @voter.election.name %></em></h1>
+  <p><strong>Description:</strong></p>
+  <blockquote><%= @voter.election.description %></blockquote>
+<% else %>
+  <p><strong>Election:</strong> <%= @voter.election.name %></p>
+  <p><strong>Voter:</strong> <%= @voter.email %></p>
+  <p><strong>Description:</strong></p>
+  <blockquote><%= @voter.election.description %></blockquote>
+<% end %>
+
+<p><strong>Candidates:</strong></p>
+<ol>
+<% for candidate in @voter.election.candidates.sort %>
+  <li><%= candidate.name %></li>
+<% end %>
+</ol>
+
+<hr />
+
+<h2>Place Your Vote Here</h2>
+
+<p>Rank each candidate in order of more preferred to least
+preferred. (e.g., 123 or 321 or 213, etc.)</p>
+
+<% 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 (file)
index 0000000..599308f
--- /dev/null
@@ -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 (file)
index 0000000..3cb2eda
--- /dev/null
@@ -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 (file)
index 2d30209..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-<% %>
-
-<h1>Vote Below the Line</h1>
-
-<p><strong>Election:</strong> <%= @voter.election.name %></p>
-
-<p><strong>Voter:</strong> <%= @voter.email %></p>
-
-<p><strong>Candidates:</strong></p>
-
-<ol>
-<% for candidate in @voter.election.candidates.sort %>
-  <li><%= candidate.name %></li>
-<% end %>
-</ol>
-
-<p>If this information is incorrect, please notify the vote
-administrator immediatedly!</p>
-
-<hr />
-
-<h2>Place Your Vote Here</h2>
-
-<p>Rank each candidate in order of more preferred to least
-preferred. (e.g., 123 or 321 or 213, etc.)</p>
-
-<%= form_tag :action => 'review', :id => @voter.password %>
-<%= text_field :vote, :votestring -%>
-<%= submit_tag "Submit!" %>
-<%= end_form_tag %>
-
-
-
-
-
index ed0625d8db889e12d5135761263b766cdadf73b7..af9820fdab90256d33fe6a0b7b17ed8ac4023b52 100644 (file)
@@ -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'
index bbc054ae19ac33601dc5ab6709fc0a630a71c71c..a9d430cf83dbc1b8d753f80762052ed61ea6d7d0 100644 (file)
@@ -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 (file)
index 0000000..692a81d
--- /dev/null
@@ -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 (file)
index 0000000..21bc293
--- /dev/null
@@ -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 (file)
index 0000000..8794d28
--- /dev/null
@@ -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 (file)
index 0000000..8794d28
--- /dev/null
@@ -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 (file)
index 0000000..833ca44
--- /dev/null
@@ -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 (file)
index 0000000..d6e0a11
--- /dev/null
@@ -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

Benjamin Mako Hill || Want to submit a patch?