From: Date: Thu, 17 Aug 2006 21:16:15 +0000 (-0400) Subject: Mostly worked on the candidate adding feature to include pictures and X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/2ba40f97b78f529984ff26c7c7d61c91d213e7b0?hp=4efa3e9bc47cc649205a399fde51ed7e61bef7f8 Mostly worked on the candidate adding feature to include pictures and haev a better interface. --- diff --git a/app/controllers/election_controller.rb b/app/controllers/election_controller.rb index f4b89b4..a0549d4 100644 --- a/app/controllers/election_controller.rb +++ b/app/controllers/election_controller.rb @@ -22,7 +22,7 @@ class ElectionController < ApplicationController 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 @@ -56,19 +56,21 @@ class ElectionController < ApplicationController # 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 @@ -76,8 +78,40 @@ class ElectionController < ApplicationController 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 diff --git a/app/models/candidate.rb b/app/models/candidate.rb index e17ba29..7af978a 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -1,5 +1,6 @@ class Candidate < ActiveRecord::Base belongs_to :election + validates_uniqueness_of :name def <=>(other) self.name <=> other.name @@ -9,4 +10,25 @@ class Candidate < ActiveRecord::Base 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 + diff --git a/app/views/election/_candidate_form.rhtml b/app/views/election/_candidate_form.rhtml new file mode 100644 index 0000000..e9770b8 --- /dev/null +++ b/app/views/election/_candidate_form.rhtml @@ -0,0 +1,8 @@ +

New candidate name:
+<%= text_field :candidate, :name %>

+ +

Candidate description/platform (optional):
+<%= text_area :candidate, :description %>

+ +

Candidate picture (optional and < 100x100 pixels):
+<%= file_field :candidate, :picture %>

diff --git a/app/views/election/_candidate_line.rhtml b/app/views/election/_candidate_line.rhtml index 7f68e6f..5145b88 100644 --- a/app/views/election/_candidate_line.rhtml +++ b/app/views/election/_candidate_line.rhtml @@ -1,11 +1,19 @@ -
-
  • - <%= @candidate.name %> - <% if @edit %> - <%= link_to_remote "Delete", - :complete => "Element.remove('cand#{@candidate.id}')", - :url => { :action => :delete_candidate, :id => @candidate.id } %> +<% -%> +
    +
  • <%= @current_candidate.name -%> + <% if @show_details %> + (<%= link_to_remote "Hide Details", + :update => "cand#{@current_candidate.id}", + :url => { :action => :lessinfo_candidate, :id => @current_candidate.id } %>) +
    +
    + <%= h(@current_candidate.description) %> +
    + <% else %> + (<%= link_to_remote "Show Details", + :update => "cand#{@current_candidate.id}", + :url => { :action => :moreinfo_candidate, + :id => @current_candidate.id } %>) <% end %> -
  • diff --git a/app/views/election/_candidate_line_edit.rhtml b/app/views/election/_candidate_line_edit.rhtml new file mode 100644 index 0000000..ea20a4f --- /dev/null +++ b/app/views/election/_candidate_line_edit.rhtml @@ -0,0 +1,24 @@ +<% -%> +
    +

    <%= @current_candidate.name %> + (<%= 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 %>)
    +

    + +
    + <% if @current_candidate.picture? %> + + <% end %> + + Description:
    + <%= h(@current_candidate.description) %> +
    +
    +

    +
    diff --git a/app/views/election/_candidate_list.rhtml b/app/views/election/_candidate_list.rhtml index ca8a5cc..74d4c3c 100644 --- a/app/views/election/_candidate_list.rhtml +++ b/app/views/election/_candidate_list.rhtml @@ -1,7 +1,7 @@ <% %> diff --git a/app/views/election/_candidates_form.rhtml b/app/views/election/_candidates_form.rhtml index ca6e73e..4528923 100644 --- a/app/views/election/_candidates_form.rhtml +++ b/app/views/election/_candidates_form.rhtml @@ -1,15 +1,9 @@ <% %> -

    Please enter candidates for <%= @election.name %>.

    -<%= 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 ) %> -

    New candidate name:
    -<%= text_field :newcandidate, :name %>

    - -

    Candidate description/platform (optional):
    -<%= text_area :newcandidate, :description %>

    +<%= render_partial 'candidate_form' %> <%= submit_tag "Add Candidate" %> <%= end_form_tag %> diff --git a/app/views/election/_voter_list.rhtml b/app/views/election/_voter_list.rhtml index c1035d7..2a39b8c 100644 --- a/app/views/election/_voter_list.rhtml +++ b/app/views/election/_voter_list.rhtml @@ -1,23 +1,19 @@ <% # basic election information template -%> +

    The following voters are currently registered for this election:

    -<% unless @election.voters.empty? %> -

    The following voters are currently registered for this election:

    - - -<% end %> - + diff --git a/app/views/election/edit_candidate.rhtml b/app/views/election/edit_candidate.rhtml new file mode 100644 index 0000000..9bc9954 --- /dev/null +++ b/app/views/election/edit_candidate.rhtml @@ -0,0 +1,9 @@ +

    Editing <%= @candidate.name %>

    + +<%= error_messages_on :candidate %> + +<%= form_tag :action => 'update_candidate', :id => @candidate.id %> + <%= render :partial => 'candidate_form' %> + <%= submit_tag "Done!" %> +<%= end_form_tag %> + diff --git a/app/views/election/edit_candidates.rhtml b/app/views/election/edit_candidates.rhtml index 0c416d5..b5f06ff 100644 --- a/app/views/election/edit_candidates.rhtml +++ b/app/views/election/edit_candidates.rhtml @@ -1,14 +1,16 @@ -<% @edit = true %>

    <%= @election.name %>: Edit Candidates

    -

    The following are valid options or candidates in this election:

    +<% unless @election.candidates.empty? %> +

    The following are valid options or candidates in this election:

    - + <% @election.candidates.each do |candidate| %> + <% @current_candidate = candidate %> + <%= render :partial => 'candidate_line_edit' %> + <% end %> +<% else %> +

    There are no candidates registered for this election.

    +<% end %> +

    Please enter new candidates below.

    <%= render :partial => 'candidates_form' %> <%= button_to "Done!", :action => 'show', :id => @election %> diff --git a/app/views/election/new_candidates.rhtml b/app/views/election/new_candidates.rhtml deleted file mode 100644 index f95cd83..0000000 --- a/app/views/election/new_candidates.rhtml +++ /dev/null @@ -1,11 +0,0 @@ -<% @edit = true %> -

    <%= @election.name %> Candidates

    - - - -<%= render :partial => 'candidates_form' %> -<%= button_to "Done!", :action => 'show', :id => @election %> diff --git a/app/views/election/show.rhtml b/app/views/election/show.rhtml index 954e480..f389059 100644 --- a/app/views/election/show.rhtml +++ b/app/views/election/show.rhtml @@ -1,18 +1,38 @@ <% %>

    Information On <%= @election.name %>

    -

    Overview <%= link_to "edit", :action => 'edit', :id => @election.id %>

    - +

    Election Overview

    +

    Description

    -<%= @election.description %> +<%= h(@election.description) %>
    -

    Candidates <%= link_to "edit", :action => 'edit_candidates', :id => @election.id %>

    +

    Election End Date

    + +
    +<%= @election.enddate %> +
    + +

    <%= link_to "Edit election overview.", :action => 'edit', :id => @election.id %>

    + +

    Candidates

    + +<% unless @election.candidates.empty? %> + <%= render :partial => 'candidate_list' %> +

    <%= link_to "Add, remove, or edit candidates.", :action => 'edit_candidates', :id => @election.id %>

    +<% else %> +

    There are currently no candidates registered for this election. + <%= link_to "Add some!", :action => 'edit_candidates', :id => @election.id %>

    -<%= render :partial => 'candidate_list' %> +<% end %> -

    Voters <%= link_to "edit", :action => 'edit_voters', :id => @election.id %>

    +

    Voters

    -<%= render :partial => 'voter_list' %> +<% unless @election.voters.empty? %> + <%= render :partial => 'voter_list' %> +<% else %> +

    There are currently no voters registered for this election. + <%= link_to "Add some!", :action => 'edit_voters', :id => @election.id %>

    +<% end %> diff --git a/app/views/site/_user_summary.rhtml b/app/views/site/_user_summary.rhtml index a231497..310d98c 100644 --- a/app/views/site/_user_summary.rhtml +++ b/app/views/site/_user_summary.rhtml @@ -7,7 +7,8 @@ <% else %> <% end %> diff --git a/db/create.sql b/db/create.sql index 4a2025f..bbc054a 100644 --- a/db/create.sql +++ b/db/create.sql @@ -1,18 +1,3 @@ -# 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 ##################################### @@ -21,9 +6,10 @@ create table elections ( 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) @@ -38,7 +24,9 @@ create table candidates ( 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) ); diff --git a/public/stylesheets/vb.css b/public/stylesheets/vb.css index a054b85..176fd28 100644 --- a/public/stylesheets/vb.css +++ b/public/stylesheets/vb.css @@ -99,7 +99,5 @@ a:active { color: #FFFFFF; text-decoration: none; background: #0259C4; } #subtext { text-align: center; font-size: 12px; - font-weight: bold; - -} + font-weight: bold; }