From 404bd293fe621a270748df828c67e507b350920b Mon Sep 17 00:00:00 2001
From:
Date: Fri, 13 Oct 2006 22:55:14 -0400
Subject: [PATCH 1/1] Added the ability to do ajax adding of candidates so
there is no longer any need to do the parsing of the string.
---
app/controllers/quickvote_controller.rb | 44 ++++++++++++++++++-----
app/models/quick_vote.rb | 8 ++---
app/views/quickvote/_candidate_list.rhtml | 17 +++++++++
app/views/quickvote/create.rhtml | 31 ++++------------
config/routes.rb | 5 +--
5 files changed, 65 insertions(+), 40 deletions(-)
create mode 100644 app/views/quickvote/_candidate_list.rhtml
diff --git a/app/controllers/quickvote_controller.rb b/app/controllers/quickvote_controller.rb
index c16755a..97f4ab0 100644
--- a/app/controllers/quickvote_controller.rb
+++ b/app/controllers/quickvote_controller.rb
@@ -6,23 +6,51 @@ class QuickvoteController < ApplicationController
def index
@election = QuickVote.find_all(["name = ?", params[:votename]])[0]
-
- @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
+
+ if @election
+ @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
session.session_id, @election.id])[0]
- unless @voter
- @voter = QuickVoter.new
- @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
+ unless @voter
+ @voter = QuickVoter.new
+ @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
+ end
+ else
+ redirect_to :controller => 'site'
end
end
def create
if params[:quickvote]
@quickvote = QuickVote.new(params[:quickvote])
- if @quickvote.reviewed? and @quickvote.save
- @quickvote = @quickvote.reload
- render :action => 'success'
+
+ # store the candidate grabbed through ajax and stored in flash
+ @quickvote.candidatelist = flash[:candlist]
+
+ # try to save, if it fails, show the page again (the flash should
+ # still be intact
+ if @quickvote.save
+ @quickvote = @quickvote.reload
+ render :action => 'success'
+ else
+ flash.keep(:candlist)
end
+ else
+ # if we don't have a quickvote param, it means that the person
+ # here has not been hitting this page and we can clear any
+ # candlist in the flash
+ flash.delete(:candlist) if flash.has_key?(:candlist)
+ end
+ end
+
+ def add_candidate
+ candidate_name = params[:ajax][:newcandidate]
+ if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
+ flash[:candlist] << candidate_name
+ else
+ flash[:candlist] = [ candidate_name ]
end
+ flash.keep(:candlist)
+ render_partial 'candidate_list'
end
def change
diff --git a/app/models/quick_vote.rb b/app/models/quick_vote.rb
index 1df25af..3538a3c 100644
--- a/app/models/quick_vote.rb
+++ b/app/models/quick_vote.rb
@@ -22,12 +22,8 @@ class QuickVote < Election
self.quickvote = 1
end
- def candidatelist=(candstring='')
- @raw_candidates = candstring.split(';').collect {|cand| cand.strip }
- end
-
- def candidatelist
- @raw_candidates.join("; ")
+ def candidatelist=(candlist)
+ @raw_candidates = candlist
end
def name
diff --git a/app/views/quickvote/_candidate_list.rhtml b/app/views/quickvote/_candidate_list.rhtml
new file mode 100644
index 0000000..1176e97
--- /dev/null
+++ b/app/views/quickvote/_candidate_list.rhtml
@@ -0,0 +1,17 @@
+<% %>
+
+<% if @flash[:candlist] %>
+