From: Date: Thu, 12 Oct 2006 01:42:18 +0000 (-0400) Subject: working quickvote support created X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/04f827dd4647f3a7d6e5bd8f5e9083c730a8dccc?ds=sidebyside working quickvote support created --- diff --git a/app/controllers/quickvote_controller.rb b/app/controllers/quickvote_controller.rb new file mode 100644 index 0000000..c3510f6 --- /dev/null +++ b/app/controllers/quickvote_controller.rb @@ -0,0 +1,78 @@ +class QuickvoteController < ApplicationController + layout 'vb' + model :quick_voter + model :vote + model :election + + def index + @election = QuickVote.find_all(["name = ?", params[:votename]])[0] + + @voter = QuickVoter.find_all(["session_id = ? and election_id = ?", + session.session_id, @election.id])[0] + unless @voter + @voter = QuickVoter.new + @voter.election = Election.find_all( [ "name = ?", params[:votename] ] )[0] + end + end + + def create + if params[:quickvote] + @quickvote = QuickVote.new(params[:quickvote]) + if @quickvote.reviewed? and @quickvote.save + @quickvote = @quickvote.reload + render :action => 'success' + end + end + end + + def change + voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0] + voter.destroy + redirect_to quickvote_url( :votename => params[:votename] ) + end + + def confirm + election = QuickVote.find_all(["name = ?", params[:votename]])[0] + + if QuickVoter.find_all(["session_id = ? and election_id = ?", + session.session_id, election.id])[0] + flash[:notice] = "You have already voted!" + redirect_to quickvote_url( :votename => params[:votename] ) + else + @voter = QuickVoter.new() + @voter.election = election + @voter.session_id = session.session_id + @voter.save + @voter.reload + + @voter.vote = Vote.new + @voter.vote.votestring = params[:vote][:votestring] + @voter.vote.confirm! + render :action => 'thanks' + end + end + + def results + @election = Election.find_all( ["name = ?", params[:votename]] )[0] + + preference_tally = [] + plurality_tally = [] + approval_tally = [] + @election.voters.each do |voter| + plurality_tally << voter.vote.rankings.sort[0].candidate.id + approval_tally << voter.vote.rankings.sort[0..1].collect {|ranking| ranking.candidate.id} + preference_tally << voter.vote.rankings.sort.collect {|ranking| ranking.candidate.id} + end + + @plurality_result = PluralityVote.new(plurality_tally).result + @approval_result = ApprovalVote.new(approval_tally).result + @condorcet_result = CloneproofSSDVote.new(preference_tally).result + @ssd_result = PureCondorcetVote.new(preference_tally).result + @borda_result = BordaVote.new(preference_tally).result + @runoff_result = InstantRunoffVote.new(preference_tally).result + + @candidates = {} + @election.candidates.each {|c| @candidates[c.id] = c} + end + +end diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index f4bae42..41eb136 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -11,14 +11,4 @@ class SiteController < ApplicationController end end - def create_quickvote - if params[:quickvote] - @quickvote = QuickVote.new(params[:quickvote]) - if @quickvote.reviewed? and @quickvote.save - @quickvote = @quickvote.reload - render :action => 'success_quickvote' - end - end - end - end diff --git a/app/controllers/voter_controller.rb b/app/controllers/voter_controller.rb index d2529f8..1b00b8b 100644 --- a/app/controllers/voter_controller.rb +++ b/app/controllers/voter_controller.rb @@ -30,26 +30,7 @@ class VoterController < ApplicationController end def confirm - 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 + if authenticate @voter.vote.confirm! render :action => 'thanks' else @@ -57,11 +38,6 @@ 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] diff --git a/app/helpers/quickvote_helper.rb b/app/helpers/quickvote_helper.rb new file mode 100644 index 0000000..5ccdc27 --- /dev/null +++ b/app/helpers/quickvote_helper.rb @@ -0,0 +1,2 @@ +module QuickvoteHelper +end diff --git a/app/models/quick_voter.rb b/app/models/quick_voter.rb index 7b934a3..1a2daf6 100644 --- a/app/models/quick_voter.rb +++ b/app/models/quick_voter.rb @@ -1,4 +1,6 @@ class QuickVoter < Voter validates_presence_of :session_id - validates_uniqueness_of :session_id + + # validates_uniqueness_of :session_id + # instead we shoudl validate that it's unique for a given election_id end diff --git a/app/models/vote.rb b/app/models/vote.rb index 7678f54..08dd1e7 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -41,6 +41,11 @@ class Vote < ActiveRecord::Base self.rankings << ranking end end + + def destroy + self.destroy_rankings + super + end def destroy_rankings rankings.each { |ranking| ranking.destroy } diff --git a/app/models/voter.rb b/app/models/voter.rb index 7139b89..44efee6 100644 --- a/app/models/voter.rb +++ b/app/models/voter.rb @@ -2,6 +2,11 @@ class Voter < ActiveRecord::Base belongs_to :election has_one :vote + def destroy + vote.destroy if vote + super + end + end diff --git a/app/views/layouts/vb.rhtml b/app/views/layouts/vb.rhtml index 3e4f8bd..71003b5 100644 --- a/app/views/layouts/vb.rhtml +++ b/app/views/layouts/vb.rhtml @@ -40,8 +40,9 @@ <%= @content_for_layout %> -
- diff --git a/app/views/site/success_quickvote.rhtml b/app/views/quickvote/success.rhtml similarity index 86% rename from app/views/site/success_quickvote.rhtml rename to app/views/quickvote/success.rhtml index 299edac..982fd12 100644 --- a/app/views/site/success_quickvote.rhtml +++ b/app/views/quickvote/success.rhtml @@ -1,8 +1,6 @@ <% %> -

Vote Created

- -

You have successfully created a QuickVote.

+

Vote Created

QuickVotes are open to the public but are not publicly listed on the HyperChad site. Voters do not need to log in or authenticate to diff --git a/app/views/quickvote/thanks.rhtml b/app/views/quickvote/thanks.rhtml new file mode 100644 index 0000000..794f412 --- /dev/null +++ b/app/views/quickvote/thanks.rhtml @@ -0,0 +1,23 @@ +<% %> + +

Your vote has been recorded with the following ranked +preferences:

+ +
    + <% for rank in @voter.vote.rankings.sort %> +
  1. <%= rank.candidate.name %>
  2. + <% end %> +
+ +

Thanks you voting! You can:

+ + + + + + + diff --git a/app/views/voter/_vote.rhtml b/app/views/voter/_vote.rhtml index e742021..b9a2688 100644 --- a/app/views/voter/_vote.rhtml +++ b/app/views/voter/_vote.rhtml @@ -1,37 +1,22 @@ <% %> -<% 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:

+

Candidates or choices:

    <% 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.)

+

In the box below, list each choice in order of most preferred to +least preferred. (For example, 123 or 321 or 213, etc.)

<% if @voter.election.quickvote? %> - <%= form_tag quickconfirm_url( :votename => @voter.election.name ) %> + <%= form_tag quickaction_url( :votename => @voter.election.name, :action => 'confirm') %> <% else %> <%= form_tag :action => 'review', :id => @voter.password %> <% end %> <%= text_field :vote, :votestring -%> -<%= submit_tag "Submit!" %> +<%= submit_tag "Vote!" %> <%= end_form_tag %> diff --git a/app/views/voter/full_vote.rhtml b/app/views/voter/full_vote.rhtml index 599308f..2f1fc22 100644 --- a/app/views/voter/full_vote.rhtml +++ b/app/views/voter/full_vote.rhtml @@ -1,5 +1,10 @@ <% %> +

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

+

Voter: <%= @voter.email %>

+

Description:

+
<%= @voter.election.description %>
+ <%= render_partial 'vote' %> diff --git a/app/views/voter/quickvote.rhtml b/app/views/voter/quickvote.rhtml deleted file mode 100644 index 3cb2eda..0000000 --- a/app/views/voter/quickvote.rhtml +++ /dev/null @@ -1,2 +0,0 @@ -<% %> -<%= render_partial 'vote' %> diff --git a/config/routes.rb b/config/routes.rb index af9820f..d1092a0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,18 +12,18 @@ ActionController::Routing::Routes.draw do |map| # You can have the root of your site routed by hooking up '' # -- 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' + :controller => 'quickvote', + :action => 'create' + + map.quickaction 'quickvote/:votename/:action', + :controller => 'quickvote', + :requirements => { :action => /(change|confirm|results)/ } map.quickvote 'quickvote/:votename', - :controller => 'voter', - :action => 'quickvote' + :controller => 'quickvote', + :action => 'index' # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index 616de48..95663d4 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -142,7 +142,7 @@ class PureCondorcetResult < CondorcetResult victors[candidate].length == @election.candidates.length - 1 end - @winners << winners if winners.length == 1 + @winners = winners if winners.length == 1 end end diff --git a/lib/rubyvote/election.rb b/lib/rubyvote/election.rb index 7fd8396..fc61840 100644 --- a/lib/rubyvote/election.rb +++ b/lib/rubyvote/election.rb @@ -70,7 +70,7 @@ class PluralityVote < ElectionVote protected def verify_vote(vote=nil) - vote.instance_of?( String ) + vote ? true : false end def tally_vote(candidate) diff --git a/public/stylesheets/vb.css b/public/stylesheets/vb.css index 9f0d7c8..e694069 100644 --- a/public/stylesheets/vb.css +++ b/public/stylesheets/vb.css @@ -92,7 +92,8 @@ a:active { color: #FFFFFF; text-decoration: none; background: #0259C4; } #footer { text-align: center; font-size: 12px; - color: #464646; } + color: #464646; + clear: both;} #footer a { font-weight: normal; } @@ -107,3 +108,27 @@ a:active { color: #FFFFFF; text-decoration: none; background: #0259C4; } .fieldWithErrors input, .fieldWithErrors select { background-color: #ffdfdf; } + +.mainresultbox { + width: 100%; + padding: 7px; + margin-right: 10px; + margin-bottom: 10px; + border-style: dotted; + border-width: 1px; +} + +.mainresultbox h1 { + text-align: center; +} +.resultbox { + width:47%; + float: left; + padding: 7px; + margin-right: 10px; + margin-bottom: 10px; + border-style: dotted; + border-width: 1px; +} + + diff --git a/test/functional/quickvote_controller_test.rb b/test/functional/quickvote_controller_test.rb new file mode 100644 index 0000000..d0b37d9 --- /dev/null +++ b/test/functional/quickvote_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'quickvote_controller' + +# Re-raise errors caught by the controller. +class QuickvoteController; def rescue_action(e) raise e end; end + +class QuickvoteControllerTest < Test::Unit::TestCase + def setup + @controller = QuickvoteController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end