--- /dev/null
+class ElectionsController < ApplicationController
+ def index
+ list
+ render :action => 'list'
+ end
+
+ def list
+ @election_pages, @elections = paginate :elections, :per_page => 10
+ end
+
+ def show
+ @election = Election.find(params[:id])
+ end
+
+ def new
+ @election = Election.new
+ end
+
+ def create_election
+ @election = Election.new(params[:election])
+ if @election.save
+ flash[:notice] = 'Election was successfully created.'
+ redirect_to :action => 'new_candidates', :id => @election.id
+ else
+ render :action => 'new'
+ end
+ end
+
+ def new_candidates
+ @election = Election.find( params[:id] )
+ end
+
+ def add_candidate
+ election = Election.find( params[:id] )
+ @candidate = Candidate.new
+ @candidate.name = params[:newcandidate]
+ @candidate.save
+ election.candidates << @candidate
+ render :partial => 'candidate_line'
+ end
+
+ def delete_candidate
+ candidate = Candidate.find( params[:id] )
+ candidate.destroy
+ end
+
+ def edit
+ @election = Election.find(params[:id])
+ end
+
+ def edit_candidates
+ @election = Election.find( params[:id] )
+ end
+
+ def edit_voters
+ @election = Election.find( params[:id] )
+ end
+
+ def update
+ @election = Election.find(params[:id])
+ if @election.update_attributes(params[:election])
+ flash[:notice] = 'Election was successfully updated.'
+ redirect_to :action => 'show', :id => @election
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ election = Election.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
--- /dev/null
+module ElectionsHelper
+end
--- /dev/null
+class Candidate < ActiveRecord::Base
+end
--- /dev/null
+class Election < ActiveRecord::Base
+ has_many :candidates
+ validates_presence_of :name, :description
+
+ def destroy
+ self.candidates.each do |candidate|
+ candidate.destroy
+ end
+ super destroy
+ end
+
+end
--- /dev/null
+<div id="cand<%= @candidate.id %>">
+ <li>
+ <%= @candidate.name %>
+ <% if @edit %>
+ <%= link_to_remote "Delete",
+ :complete => "Element.remove('cand#{@candidate.id}')",
+ :url => { :action => :delete_candidate, :id => @candidate.id } %>
+ <% end %>
+
+ </li>
+</div>
--- /dev/null
+<p>Please enter candidates for <strong><%= @election.name %></strong>.</p>
+
+ <ul id="candidates_list">
+ <% @election.candidates.each do |candidate| %>
+ <% @candidate = candidate %>
+ <%= render :partial => 'candidate_line' %>
+ <% end %>
+ </ul>
+
+ <%= form_remote_tag(:update => "candidates_list",
+ :url => { :action => :add_candidate, :id => @election.id },
+ :position => "top" ) %>
+
+ <p>New candidate name:
+ <%= text_field_tag :newcandidate %>
+
+ <%= submit_tag "Add" %>
+ <%= end_form_tag %>
--- /dev/null
+<%= error_messages_for 'election' %>
+
+<!--[form:election]-->
+<p><label for="election_name">Name</label><br/>
+<%= text_field 'election', 'name' %></p>
+
+<p><label for="election_description">Description</label><br/>
+<%= text_area 'election', 'description' %></p>
+
+<p><label for="election_anonymous">Anonymous Vote</label>
+<%= check_box 'election', 'anonymous', {}, 1, 0 %></p>
+<!--[eoform:election]-->
+
--- /dev/null
+<h1><strong><%= @election.name %>:</strong> Edit Overview</h1>
+
+<%= start_form_tag :action => 'update', :id => @election %>
+ <%= render :partial => 'overview_form' %>
+ <%= submit_tag 'Done!' %>
+<%= end_form_tag %>
--- /dev/null
+<% @edit = true %>
+<h1><strong><%= @election.name %>:</strong> Edit Candidates</h1>
+
+<%= render :partial => 'candidates_form' %>
+<%= button_to "Done!", :action => 'show', :id => @election %>
--- /dev/null
+<% @edit = true %>
+<h1><strong><%= @election.name %>:</strong> Edit Voter Rolls</h1>
+
+<%= button_to "Done!", :action => 'show', :id => @election %>
--- /dev/null
+<h1>Listing elections</h1>
+
+<table>
+ <tr>
+ <% for column in Election.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for election in @elections %>
+ <tr>
+ <% for column in Election.content_columns %>
+ <td><%=h election.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => election %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => election %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => election }, :confirm => 'Are you sure?' %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @election_pages.current.previous } if @election_pages.current.previous %>
+<%= link_to 'Next page', { :page => @election_pages.current.next } if @election_pages.current.next %>
+
+<br />
+
+<%= link_to 'New election', :action => 'new' %>
--- /dev/null
+<h1>Create A New Election</h1>
+
+<h2>Step 1: Describe The Election</h2>
+
+<%= form_tag :action => 'create_election' %>
+ <%= render :partial => 'overview_form' %>
+ <%= submit_tag "Done!" %>
+<%= end_form_tag %>
+
--- /dev/null
+<% @edit = true %>
+<h1><strong><%= @election.name %>:</strong> Enter Candidates</h1>
+
+<%= render :partial => 'candidates_form' %>
+<%= button_to "Done!", :action => 'show', :id => @election %>
--- /dev/null
+<h1>Information On <%= @election.name %></h1>
+
+<h2>Overview <%= link_to "edit", :action => 'edit', :id => @election.id %></h2>
+
+<p><strong>Description</strong></p>
+
+<blockquote>
+<%= @election.description %>
+</blockquote>
+
+<h2>Candidates <%= link_to "edit", :action => 'edit_candidates', :id => @election.id %></h2>
+
+
+The following are valid options or candidates in this election:
+
+<ul>
+<% @election.candidates.each do |candidate| %>
+ <% @candidate = candidate %>
+ <%= render :partial => 'candidate_line' %>
+<% end %>
+</ul>
+
+<h2>Voters <%= link_to "edit", :action => 'edit_voters', :id => @election.id %></h2>
--- /dev/null
+<html>
+<head>
+ <title>Elections: <%= controller.action_name %></title>
+ <%= javascript_include_tag "prototype", "effects" %>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= @content_for_layout %>
+
+</body>
+</html>
--- /dev/null
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+.fieldWithErrors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#ErrorExplanation {
+ width: 400px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#ErrorExplanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#ErrorExplanation p {
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
+}
+
+#ErrorExplanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
+
+div.uploadStatus {
+ margin: 5px;
+}
+
+div.progressBar {
+ margin: 5px;
+}
+
+div.progressBar div.border {
+ background-color: #fff;
+ border: 1px solid grey;
+ width: 100%;
+}
+
+div.progressBar div.background {
+ background-color: #333;
+ height: 18px;
+ width: 0%;
+}
+
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+ id: 1
+another:
+ id: 2
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+require 'elections_controller'
+
+# Re-raise errors caught by the controller.
+class ElectionsController; def rescue_action(e) raise e end; end
+
+class ElectionsControllerTest < Test::Unit::TestCase
+ fixtures :elections
+
+ def setup
+ @controller = ElectionsController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_index
+ get :index
+ assert_response :success
+ assert_template 'list'
+ end
+
+ def test_list
+ get :list
+
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:elections)
+ end
+
+ def test_show
+ get :show, :id => 1
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:election)
+ assert assigns(:election).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:election)
+ end
+
+ def test_create
+ num_elections = Election.count
+
+ post :create, :election => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_elections + 1, Election.count
+ end
+
+ def test_edit
+ get :edit, :id => 1
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:election)
+ assert assigns(:election).valid?
+ end
+
+ def test_update
+ post :update, :id => 1
+ assert_response :redirect
+ assert_redirected_to :action => 'show', :id => 1
+ end
+
+ def test_destroy
+ assert_not_nil Election.find(1)
+
+ post :destroy, :id => 1
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Election.find(1)
+ }
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ElectionTest < Test::Unit::TestCase
+ fixtures :elections
+
+ # Replace this with your real tests.
+ def test_truth
+ assert_kind_of Election, elections(:first)
+ end
+end