From:
Date: Tue, 19 Feb 2008 14:24:06 +0000 (-0500)
Subject: added first full working version of embeddable elections
X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/adfcf4dd7d989d0560e2a8f6a590dd10c5a49a08?hp=b47d1fbe36d5aeb9b79d6bbaab035ca448a3c3ef
added first full working version of embeddable elections
- added several new fields to the database to support unauthenticated,
embeddable, and early result visible full elections
- modified full election create to allow for proper options and to
display the full election code
- added new layouts, views, css, and images for embeddable elections
- modified full elections to work with the new form of images in the
last commit
- fixed several bugs related to vote recording and timestamp,s
---
diff --git a/.bzrignore b/.bzrignore
index 57a67d2..5304d54 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -7,3 +7,4 @@ tmp
public/engine_files
.DS_Store
vendor/plugins/sitealizer/lib/last_update
+public/pictures
diff --git a/app/controllers/election_controller.rb b/app/controllers/election_controller.rb
index 5949f56..94c203d 100644
--- a/app/controllers/election_controller.rb
+++ b/app/controllers/election_controller.rb
@@ -54,20 +54,13 @@ class ElectionController < ApplicationController
end
end
- # add filter to verify that the person working on or looking at
+ # TODO add filter to verify that the person working on or looking at
# something is the owner
- def edit
+ def edit_general_information
@election = Election.find(params[:id])
end
-
- def show
- @sidebar_content = render_to_string :partial => 'progress',
- :locals => { :page => 'review' }
-
- @election = Election.find(params[:id])
- end
-
- def update
+
+ def update_general_information
@election = Election.find(params[:id])
if @election.update_attributes(params[:election])
flash[:notice] = 'Election was successfully updated.'
@@ -77,6 +70,14 @@ class ElectionController < ApplicationController
end
end
+
+ def show
+ @sidebar_content = render_to_string :partial => 'progress',
+ :locals => { :page => 'review' }
+
+ @election = Election.find(params[:id])
+ end
+
def start_election
@election = Election.find(params[:id])
@election.voters.each do |voter|
@@ -103,6 +104,12 @@ class ElectionController < ApplicationController
@election.candidates << @candidate
if @candidate.save
+ # check to see if they've uploaded a picture
+ if params[:picture][:uploaded_data]
+ picture = Picture.new(params[:picture])
+ @candidate.picture = picture if picture.save
+ end
+
@candidate = Candidate.new
redirect_to :action => 'edit_candidates', :id => @election.id
else
@@ -145,6 +152,16 @@ class ElectionController < ApplicationController
voter = Voter.find( params[:id] )
voter.destroy
end
+
+ def toggle_authenticated
+ @election = Election.find(params[:id])
+ if params[:authenticated] == "1"
+ @election.authenticated = true
+ else
+ @election.authenticated = false
+ end
+ @election.save
+ end
## methods for computing and printing results
####################################################################
diff --git a/app/controllers/voter_controller.rb b/app/controllers/voter_controller.rb
index 868bc4c..cdc045d 100644
--- a/app/controllers/voter_controller.rb
+++ b/app/controllers/voter_controller.rb
@@ -23,34 +23,48 @@ class VoterController < ApplicationController
require_dependency "election"
def index
- if params[:urlpassword]
+ if params[:election_id]
+ @election = Election.find(params[:election_id])
+ unless @election.authenticated?
+ @voter = Voter.find(:all,
+ :conditions => ["session_id = ? and election_id = ?",
+ session.session_id, @election.id])[0]
+
+ @voter = Voter.new unless @voter
+
+ @voter.election = @election
+ @voter.session_id = session.session_id
+ @password = "open." + @election.id.to_s
+ end
+ elsif params[:urlpassword]
password = params[:urlpassword]
if @voter = FullVoter.find(:all,
:conditions => [ "password = ?", password ] )[0]
+ @election = @voter.election
+ @password = @voter.password
+ end
+ end
- @voter.vote = Vote.new if @voter.vote.nil?
- @voter.vote.set_defaults! if @voter.vote.rankings.empty?
+ if @voter and @election
+ # initialize things if the vote is blank
+ if @voter.vote.nil?
+ @voter.vote = Vote.new
+ @voter.save
+ end
+
+ @voter.vote.set_defaults! if @voter.vote.rankings.empty?
- @election = @voter.election
-
- # if the election is now finished
- if @election.enddate < Time.now
- # compute and display results
-
- @results = @election.results
- @candidates = {}
- @election.candidates.each {|c| @candidates[c.id] = c}
- @names = @election.names_by_id
-
- @sidebar_content = render_to_string(:partial => 'results_sidebar')
- render :action => 'results'
+ # if the election is now finished
+ if @election.enddate < Time.now
+ redirect_to :action => :results, :id => @password
+ else
+ @sidebar_content = render_to_string(:partial => 'vote_sidebar')
+ if @election.embeddable? and params[:embed] == "true"
+ render :template => 'embed/full_vote', :layout => 'embed'
else
- @sidebar_content = render_to_string(:partial => 'vote_sidebar')
render :action => 'full_vote'
end
- elsif params[:urlpassword]
- redirect_to :action => 'index'
end
end
end
@@ -100,7 +114,13 @@ class VoterController < ApplicationController
def confirm
if authenticate
@voter.vote.confirm!
- render :action => 'thanks'
+
+ if @voter.election.embeddable? and params[:embed] == "true" \
+ and @voter.election.early_results?
+ redirect_to :action => :results, :id => @password, :embed => 'true'
+ else
+ render :action => 'thanks'
+ end
else
redirect_to :action => 'index'
end
@@ -117,11 +137,47 @@ class VoterController < ApplicationController
end
end
+ def results
+ if authenticate and
+ (@voter.election.early_results? \
+ or @voter.election.enddate < Time.now)
+
+ @election = @voter.election
+ # compute and display results
+
+ @results = @election.results
+ @candidates = {}
+ @election.candidates.each {|c| @candidates[c.id] = c}
+ @names = @election.names_by_id
+
+ @sidebar_content = render_to_string(:partial => 'results_sidebar')
+ if @election.embeddable? and params[:embed] == "true"
+ render :template => 'embed/results', :layout => 'embed'
+ else
+ render :action => 'results'
+ end
+ else
+ redirect_to :action => 'index'
+ end
+ end
private
def authenticate
password = params[:id]
- @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0]
+ if password == "open"
+ election = Election.find(params[:format])
+ unless election.authenticated?
+ @voter = Voter.find(:all,
+ :conditions => ["session_id = ? and election_id = ?",
+ session.session_id, election.id])[0]
+ @password = "open." + election.id.to_s
+ end
+ else
+ @voter = FullVoter.find(:all,
+ :conditions => [ "password = ?", password ] )[0]
+ @password = @voter.password
+ end
+ @voter
end
end
diff --git a/app/models/election.rb b/app/models/election.rb
index 7d8ad80..08ef1a5 100644
--- a/app/models/election.rb
+++ b/app/models/election.rb
@@ -73,7 +73,7 @@ class Election < ActiveRecord::Base
reasons << "You must have at least two candidates."
end
- if self.voters.length <= 1
+ if self.voters.length <= 1 and self.authenticated?
reasons << "You must have at least two voters."
end
@@ -97,6 +97,10 @@ class Election < ActiveRecord::Base
active == 2
end
+ def authenticated?
+ authenticated
+ end
+
def shortdesc
shortdesc = description.split(/\n/)[0]
end
diff --git a/app/models/picture.rb b/app/models/picture.rb
index 04b368e..9f17ec4 100644
--- a/app/models/picture.rb
+++ b/app/models/picture.rb
@@ -21,7 +21,7 @@ class Picture < ActiveRecord::Base
has_attachment :storage => :file_system,
:max_size => 1.megabytes,
- :thumbnails => { :thumb => '70x53>' },
+ :thumbnails => { :thumb => '70x53' },
:processor => :Rmagick
validates_as_attachment
diff --git a/app/models/token.rb b/app/models/token.rb
index aa432d6..c2d3667 100644
--- a/app/models/token.rb
+++ b/app/models/token.rb
@@ -23,7 +23,7 @@ class Token < ActiveRecord::Base
super
token_generator = UniqueTokenGenerator.new( 16 )
- until not token.empty? and Token.find(:all, :conditions => [ "token = ?", token ]).empty?
+ until not token.empty? and Token.find(:all, :conditions => [ "token = ?", token ]).empty? and token[0..3] != "open"
self.token = token_generator.token
end
diff --git a/app/models/vote.rb b/app/models/vote.rb
index bb91a6b..bef3502 100644
--- a/app/models/vote.rb
+++ b/app/models/vote.rb
@@ -69,13 +69,9 @@ class Vote < ActiveRecord::Base
rankings.each { |ranking| ranking.destroy }
end
- def settime
- self.time = Time.now
- self.save
- end
-
def confirm!
self.confirmed = 1
+ self.time = Time.now
self.save
unless self.voter.election.quickvote?
@@ -105,7 +101,7 @@ class Vote < ActiveRecord::Base
# too. It creates a vote with the candidates listed in order of preference
# based on alphabetical order. Meant to be manipulated and then confirmed
def set_defaults!
- self.votes = voter.election.candidates.sort.collect {|c| c.id }
+ self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id }
self.save
end
diff --git a/app/views/election/_candidate_box_info.rhtml b/app/views/election/_candidate_box_info.rhtml
new file mode 100644
index 0000000..76d56c0
--- /dev/null
+++ b/app/views/election/_candidate_box_info.rhtml
@@ -0,0 +1,15 @@
+
+ <% if @current_candidate.picture %>
+
+
+
+ <% end %>
+
+ <% if @current_candidate.description.length > 0 %>
+ <%= h(@current_candidate.description) %>
+ <% else %>
+
+ <% end %>
+
+
+
diff --git a/app/views/election/_candidate_form.rhtml b/app/views/election/_candidate_form.rhtml
index fcbf072..75e5c3e 100644
--- a/app/views/election/_candidate_form.rhtml
+++ b/app/views/election/_candidate_form.rhtml
@@ -6,6 +6,6 @@
(optional and < 100x100 pixels)
-<%= file_field :candidate, :picture %>
+<%= file_field :picture, :uploaded_data%>
diff --git a/app/views/election/_candidate_line.rhtml b/app/views/election/_candidate_line.rhtml
index 6b5b553..2fc9a63 100644
--- a/app/views/election/_candidate_line.rhtml
+++ b/app/views/election/_candidate_line.rhtml
@@ -10,7 +10,7 @@
- <%=h (@current_candidate.description) %>
+ <%= render :partial => 'candidate_box_info' %>
>
diff --git a/app/views/election/_candidate_line_edit.rhtml b/app/views/election/_candidate_line_edit.rhtml
index 0fd5b67..bcb7cd9 100644
--- a/app/views/election/_candidate_line_edit.rhtml
+++ b/app/views/election/_candidate_line_edit.rhtml
@@ -1,5 +1,7 @@
-
<%=h @current_candidate.name %>
+
+
+ <%=h @current_candidate.name %>
<%= link_to_remote "Delete",
:complete => "Element.remove('cand#{@current_candidate.id}')",
@@ -7,22 +9,6 @@
:id => @current_candidate.id } %>
-
-
-
- <% if @current_candidate.picture? %>
-
- <% end %>
-
-
- <% if @current_candidate.description.length > 0 %>
- <%= h(@current_candidate.description) %>
- <% else %>
-
- <% end %>
-
-
-
+ <%= render :partial => 'candidate_box_info' %>
diff --git a/app/views/election/_overview_form.rhtml b/app/views/election/_overview_form.rhtml
index 8efa1b3..8d1861d 100644
--- a/app/views/election/_overview_form.rhtml
+++ b/app/views/election/_overview_form.rhtml
@@ -2,14 +2,14 @@
-<%= text_field 'election', 'name', :size => 60 %>
+<%= text_field :election, :name, :size => 60 %>
-<%= text_area 'election', 'description', :rows => 5, :cols => 60 %>
+<%= text_area :election, :description, :rows => 5, :cols => 60 %>
diff --git a/app/views/election/_voters_form.rhtml b/app/views/election/_voters_form.rhtml
index 8130ade..7819429 100644
--- a/app/views/election/_voters_form.rhtml
+++ b/app/views/election/_voters_form.rhtml
@@ -11,5 +11,5 @@ address per line).
-->
<%= hidden_field :raw_voter_list, :email, :value => 2 %>
-
<%= submit_tag "Add Voters" %>
+
diff --git a/app/views/election/edit.rhtml b/app/views/election/edit.rhtml
deleted file mode 100644
index 40aaad2..0000000
--- a/app/views/election/edit.rhtml
+++ /dev/null
@@ -1,6 +0,0 @@
-
<%=h @election.name %>: Edit Overview
-
-<% form_tag(:action => 'update', :id => @election) do %>
- <%= render :partial => 'overview_form' %>
- <%= submit_tag 'Done!' %>
-<% end %>
diff --git a/app/views/election/edit_general_information.rhtml b/app/views/election/edit_general_information.rhtml
new file mode 100644
index 0000000..c2f1cf5
--- /dev/null
+++ b/app/views/election/edit_general_information.rhtml
@@ -0,0 +1,9 @@
+
+
+<% form_tag(:action => 'update_general_information', :id => @election) do %>
+ <%= render :partial => 'overview_form' %>
+ <%= submit_tag 'Done' %>
+<% end %>
diff --git a/app/views/election/edit_voters.rhtml b/app/views/election/edit_voters.rhtml
index 8819b95..77093fe 100644
--- a/app/views/election/edit_voters.rhtml
+++ b/app/views/election/edit_voters.rhtml
@@ -3,11 +3,26 @@
+>Anyone will be able to
+vote in this election.
+
+>
<%= render :partial => 'voter_list' %>
<% form_tag (:action => 'edit_voters', :id => @election.id) do %>
<%= render :partial => 'voters_form' %>
<% end %>
+
+
+<%= check_box :election, :authenticated %> Only allow registered voters
+
+<%= observe_field "election_authenticated",
+ :url => { :action => 'toggle_authenticated', :id => @election.id },
+ :complete => 'Element.toggle($("voter_info_box")); Element.toggle($("unauth_notice"));',
+ :with => 'authenticated' %>
+
+