Added the ability to do ajax adding of candidates so there is no longer
author<mako@atdot.cc> <>
Sat, 14 Oct 2006 02:55:14 +0000 (22:55 -0400)
committer<mako@atdot.cc> <>
Sat, 14 Oct 2006 02:55:14 +0000 (22:55 -0400)
any need to do the parsing of the string.

app/controllers/quickvote_controller.rb
app/models/quick_vote.rb
app/views/quickvote/_candidate_list.rhtml [new file with mode: 0644]
app/views/quickvote/create.rhtml
config/routes.rb

index c16755a0a8636da9d8598f2bc65edc9afa004a6e..97f4ab05fb726bd0a03cdce10ee88c025cbbfd98 100644 (file)
@@ -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
index 1df25afb74d1273bcbc2d4d64c58e718187f2ddf..3538a3cc5d9047f2a4a7fccc8cc925bca6c2e2f2 100644 (file)
@@ -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 (file)
index 0000000..1176e97
--- /dev/null
@@ -0,0 +1,17 @@
+<% %>
+<!-- the list of candidates -->
+<% if @flash[:candlist] %>
+  <ul>
+  <% for cand in @flash[:candlist] %>
+    <li><%= cand.capitalize %></li>
+  <% end %>
+  </ul>
+<% end %>
+
+<%= form_remote_tag :update => 'candlist',
+                    :url => { :action => 'add_candidate' } %>
+<p>
+  <%= text_field "ajax", "newcandidate", :size => 40 %>
+  <%= submit_tag "Add" %>
+</p>
+<%= end_form_tag %>
index 0a5d4aab1bb144f684403e6901adddda6c6b1dca..b4304953f1823e28f06e80b34b83cfe48b7ed45c 100644 (file)
@@ -1,20 +1,14 @@
 <% -%>
-<h1><%= @quickvote ? "Review and Confirm" : "Create QuickVote" %></h1>
+<h1>Create QuickVote</h1>
 
 <%= error_messages_for 'quickvote' %>
 
-<% if @quickvote %>
+<p><label for="quickvote_candidatelist">Choices</p>
 
-<p>Please review the data you've entered and confirm it. The candidates
-for the vote you've submitted include:</p>
-
-<ul>
-<% for candidate in @quickvote.raw_candidates %>
-  <li><%= candidate.capitalize %></li>
-<% end %>
-</ul>
-
-<% end %>
+<!-- the list of candidates -->
+<div id="candlist">
+<%= render :partial => 'candidate_list' %>
+</div>
 
 <%= form_tag :action => 'create' %>
 <!--[form:election]-->
@@ -30,18 +24,7 @@ for the vote you've submitted include:</p>
 
 <%= text_area 'quickvote', 'description', :cols => 50, :rows => 4 %></p>
 
-<p><label for="quickvote_candidatelist">Choices/Candidates<em><br />
-<font size="-1">seperate choices with a ";"; at least two are required</font></em></label><br/>
-
-<%= text_area 'quickvote', 'candidatelist', :cols => 50, :rows => 2 %></p>
-
-<p><% if @quickvote -%>
-     <%= submit_tag "Confirm" -%>
-     <%= hidden_field :quickvote, :reviewed, :value => 1 %>
-   <% else %>
-     <%= submit_tag "Submit" -%>
-   <%- end -%>
-</p>
+<%= submit_tag "Create Quickvote" -%>
 
 <%= end_form_tag %>
 
index d1092a0bbe4511b6958c915b078785085e563dea..c914447dccc5c090347cfc23abc8fd16d793c7b0 100644 (file)
@@ -13,9 +13,10 @@ ActionController::Routing::Routes.draw do |map|
   # -- just remember to delete public/index.html.
   map.connect '', :controller => "site"
  
-  map.connect 'quickvote/create',
+  map.connect 'quickvote/:action/:id',
               :controller => 'quickvote',
-             :action => 'create'
+             :requirements => { :action => /(create|add_candidate)/ }
+             
 
   map.quickaction 'quickvote/:votename/:action',
                   :controller => 'quickvote',

Benjamin Mako Hill || Want to submit a patch?