haev a better interface.
if @election.save
flash[:notice] = 'Election was successfully created.'
- redirect_to :action => 'new_candidates', :id => @election.id
+ redirect_to :action => 'edit_candidates', :id => @election.id
else
render :action => 'new'
end
# methods fod display, adding, deleting, and manipulating candidate
# information for elections
####################################################################
- def new_candidates
+ def edit_candidates
@election = Election.find( params[:id] )
end
def add_candidate
- election = Election.find( params[:id] )
- @candidate = Candidate.new
- @candidate.name = params[:newcandidate][:name]
- @candidate.description = params[:newcandidate][:description]
+ @election = Election.find(params[:id])
+ @candidate = Candidate.new(params[:candidate])
- @candidate.save
- election.candidates << @candidate
- render :partial => 'candidate_line'
+ if @candidate.save
+ @election.candidates << @candidate
+ @candidate = Candidate.new
+ redirect_to :action => 'edit_candidates', :id => @election.id
+ else
+ render :action => 'edit_candidates', :id => @election.id
+ end
end
def delete_candidate
candidate.destroy
end
- def edit_candidates
- @election = Election.find( params[:id] )
+ def lessinfo_candidate
+ @show_details = false
+ @candidate = Candidate.find( params[:id] )
+ render :partial => 'candidate_line'
+ end
+
+ def moreinfo_candidate
+ @show_details = true
+ @candidate = Candidate.find( params[:id] )
+ render :partial => 'candidate_line'
+ end
+
+ def edit_candidate
+ @candidate = Candidate.find( params[:id] )
+ @election = @candidate.election
+ end
+
+ def update_candidate
+ @candidate = Candidate.find(params[:id])
+
+ if @candidate.update_attributes(params[:candidate])
+ flash[:notice] = 'Candidate information was successfully updated.'
+ redirect_to :action => 'edit_candidates', :id => @candidate.election
+ else
+ render :action => 'edit_candidates'
+ end
+ end
+
+ def candidate_picture
+ candidate = Candidate.find( params[:id] )
+ send_data( candidate.picture_data,
+ :filename => candidate.picture_filename,
+ :type => candidate.picture_type,
+ :disposition => 'inline' )
end
## methods for displaying, adding, deleting, and manipulating voters
class Candidate < ActiveRecord::Base
belongs_to :election
+ validates_uniqueness_of :name
def <=>(other)
self.name <=> other.name
name
end
+ def picture=(picture_field)
+ if picture_field
+ unless picture_field.content_type.match(/^image/)
+ return false
+ end
+ self.picture_filename = base_part_of(picture_field.original_filename)
+ self.picture_type = picture_field.content_type.chomp
+ self.picture_data = picture_field.read
+ end
+ end
+
+ def base_part_of(filename)
+ name = File.basename(filename)
+ name.gsub(/[^\w._-]/, '')
+ end
+
+ def picture?
+ !self.picture_filename.nil?
+ end
+
end
+
--- /dev/null
+<p>New candidate name:<br />
+<%= text_field :candidate, :name %></p>
+
+<p>Candidate description/platform (optional):<br />
+<%= text_area :candidate, :description %></p>
+
+<p>Candidate picture (optional and < 100x100 pixels):<br />
+<%= file_field :candidate, :picture %></p>
-<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 } %>
+<% -%>
+<div id="cand<%= @current_candidate.id %>">
+ <li><%= @current_candidate.name -%>
+ <% if @show_details %>
+ (<%= link_to_remote "Hide Details",
+ :update => "cand#{@current_candidate.id}",
+ :url => { :action => :lessinfo_candidate, :id => @current_candidate.id } %>)
+ <br />
+ <blockquote>
+ <%= h(@current_candidate.description) %>
+ </blockquote>
+ <% else %>
+ (<%= link_to_remote "Show Details",
+ :update => "cand#{@current_candidate.id}",
+ :url => { :action => :moreinfo_candidate,
+ :id => @current_candidate.id } %>)
<% end %>
-
</li>
</div>
--- /dev/null
+<% -%>
+<div id="cand<%= @current_candidate.id %>">
+<p><strong><%= @current_candidate.name %></strong>
+ (<%= link_to_remote "Delete",
+ :complete => "Element.remove('cand#{@current_candidate.id}')",
+ :url => { :action => :delete_candidate,
+ :id => @current_candidate.id } %> |
+ <%= link_to "Edit", :action => 'edit_candidate', :id =>
+ @current_candidate.id %>)<br />
+ <blockquote>
+ <table><tr><td valign="top">
+ <% if @current_candidate.picture? %>
+ <img src="<%= url_for :action => 'candidate_picture',
+ :id => @current_candidate.id %>"
+ align="top" />
+ <% end %>
+ </td>
+ <td valign="top">
+ <em>Description:</em><br />
+ <%= h(@current_candidate.description) %>
+ </td></tr></table>
+ </blockquote>
+</p>
+</div>
<% %>
<ul id="candidate_list">
<% @election.candidates.each do |candidate| %>
- <% @candidate = candidate %>
+ <% @current_candidate = candidate %>
<%= render :partial => 'candidate_line' %>
<% end %>
</ul>
<% %>
-<p>Please enter candidates for <strong><%= @election.name %></strong>.</p>
-<%= form_remote_tag(:update => "candidate_list",
- :url => { :action => :add_candidate, :id => @election.id },
- :position => "bottom" ) %>
+<%= form_tag( { :action => :add_candidate, :id => @election.id },
+ :multipart => true ) %>
-<p>New candidate name:<br />
-<%= text_field :newcandidate, :name %></p>
-
-<p>Candidate description/platform (optional):<br />
-<%= text_area :newcandidate, :description %></p>
+<%= render_partial 'candidate_form' %>
<%= submit_tag "Add Candidate" %>
<%= end_form_tag %>
<%
# basic election information template
-%>
+<p>The following voters are currently registered for this election:</p>
-<% unless @election.voters.empty? %>
- <p>The following voters are currently registered for this election:</p>
-
- <ul>
- <% @election.voters.each do |voter| %>
- <div id="voter<%= voter.id %>">
- <li><%= voter.email %>
- <% if @edit %>
- <%= link_to_remote "Delete",
- :complete => "Element.remove('voter#{voter.id}')",
- :url => { :action => :delete_voter, :id => voter.id } %>
- <% end %>
- </li>
- </div>
- <% end %>
- </ul>
-<% end %>
-
+<ul>
+ <% @election.voters.each do |voter| %>
+ <div id="voter<%= voter.id %>">
+ <li><%= voter.email %>
+ <% if @edit %>
+ <%= link_to_remote "Delete",
+ :complete => "Element.remove('voter#{voter.id}')",
+ :url => { :action => :delete_voter, :id => voter.id } %>
+ <% end %>
+ </li>
+ </div>
+ <% end %>
+</ul>
--- /dev/null
+<h1>Editing <%= @candidate.name %></h1>
+
+<%= error_messages_on :candidate %>
+
+<%= form_tag :action => 'update_candidate', :id => @candidate.id %>
+ <%= render :partial => 'candidate_form' %>
+ <%= submit_tag "Done!" %>
+<%= end_form_tag %>
+
-<% @edit = true %>
<h1><strong><%= @election.name %>:</strong> Edit Candidates</h1>
-<p>The following are valid options or candidates in this election:</p>
+<% unless @election.candidates.empty? %>
+ <p>The following are valid options or candidates in this election:</p>
-<ul>
-<% @election.candidates.each do |candidate| %>
- <% @candidate = candidate %>
- <%= render :partial => 'candidate_line' %>
-<% end %>
-</ul>
+ <% @election.candidates.each do |candidate| %>
+ <% @current_candidate = candidate %>
+ <%= render :partial => 'candidate_line_edit' %>
+ <% end %>
+<% else %>
+ <p>There are no candidates registered for this election.</p>
+<% end %>
+<p>Please enter new candidates below.</p>
<%= render :partial => 'candidates_form' %>
<%= button_to "Done!", :action => 'show', :id => @election %>
+++ /dev/null
-<% @edit = true %>
-<h1><strong><%= @election.name %></strong> Candidates</h1>
-
-<ul id="candidate_list">
-<% for candidate in @election.candidates %>
- <li><%= candidate.name %></li>
-<% end %>
-</ul>
-
-<%= render :partial => 'candidates_form' %>
-<%= button_to "Done!", :action => 'show', :id => @election %>
<% %>
<h1>Information On <%= @election.name %></h1>
-<h2>Overview <%= link_to "edit", :action => 'edit', :id => @election.id %></h2>
-
+<h2>Election Overview</h2>
+
<p><strong>Description</strong></p>
<blockquote>
-<%= @election.description %>
+<%= h(@election.description) %>
</blockquote>
-<h2>Candidates <%= link_to "edit", :action => 'edit_candidates', :id => @election.id %></h2>
+<p><strong>Election End Date</strong></p>
+
+<blockquote>
+<%= @election.enddate %>
+</blockquote>
+
+<p><%= link_to "Edit election overview.", :action => 'edit', :id => @election.id %></p>
+
+<h2>Candidates</h2>
+
+<% unless @election.candidates.empty? %>
+ <%= render :partial => 'candidate_list' %>
+ <p><%= link_to "Add, remove, or edit candidates.", :action => 'edit_candidates', :id => @election.id %></p>
+<% else %>
+ <p><em>There are currently no candidates registered for this election.
+ <%= link_to "Add some!", :action => 'edit_candidates', :id => @election.id %></em></p>
-<%= render :partial => 'candidate_list' %>
+<% end %>
-<h2>Voters <%= link_to "edit", :action => 'edit_voters', :id => @election.id %></h2>
+<h2>Voters</h2>
-<%= render :partial => 'voter_list' %>
+<% unless @election.voters.empty? %>
+ <%= render :partial => 'voter_list' %>
+<% else %>
+ <p><em>There are currently no voters registered for this election.
+ <%= link_to "Add some!", :action => 'edit_voters', :id => @election.id %></em></p>
+<% end %>
<% else %>
<ul>
<% for election in @current_elections %>
- <li><%= election.name %> (ends <em><%= election.enddate %></em>)</li>
+ <li><%= link_to election.name, :controller => 'election', :action => 'show', :id => election.id %> (ends in <em><%=
+ distance_of_time_in_words Time.now, election.enddate %></em>)</li>
<% end %>
</ul>
<% end %>
-# CREATE users TABLE
-#####################################
-
-#drop table if exists users;
-#create table users (
-# id int NOT NULL auto_increment,
-# login varchar(80) default NULL,
-# password varchar(40) default NULL,
-# primary key (id)
-#);
-
-## Create a default system user to own stage directions
-## and similar. Users cannot log in.
-#insert into users ( id, login ) values ( 1, "System Defaults" );
-
# CREATE elections TABLE
#####################################
id int NOT NULL auto_increment,
name varchar(100) NOT NULL,
description TEXT NOT NULL,
- anonymous tinyint NOT NULL DEFAULT 0,
- startdate datetime NOT NULL,
- enddate datetime,
+ anonymous tinyint NOT NULL DEFAULT 1,
+ startdate datetime,
+ enddate datetime NOT NULL,
+ active tinyint NOT NULL DEFAULT 0,
user_id int NOT NULL,
primary key (id),
constraint fk_user_election foreign key (user_id) references users(id)
election_id int NOT NULL,
name varchar(100) NOT NULL,
description text NULL,
- picture blob NOT NULL,
+ picture_filename varchar(200),
+ picture_data blob,
+ picture_type varchar(100),
primary key (id)
);
#subtext {
text-align: center;
font-size: 12px;
- font-weight: bold;
-
-}
+ font-weight: bold; }