From: Date: Fri, 24 Aug 2007 23:36:06 +0000 (-0400) Subject: * refactored the system so that it keeps pictures in a seperate table X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/7b649ccad8804580632c255b9f451482fdbbe32d * refactored the system so that it keeps pictures in a seperate table but works almost almost identically * added a new progress bar * fixed a few minor bugs and walked through creating a full-fledged election --- diff --git a/app/controllers/election_controller.rb b/app/controllers/election_controller.rb index d333294..7040c38 100644 --- a/app/controllers/election_controller.rb +++ b/app/controllers/election_controller.rb @@ -12,7 +12,12 @@ class ElectionController < ApplicationController #################################################################### def new + redirect_to :action => 'general_information' + end + + def general_information @election = Election.new + render :action => 'general_information' end def create_election @@ -27,7 +32,7 @@ class ElectionController < ApplicationController flash[:notice] = 'Election was successfully created.' redirect_to :action => 'edit_candidates', :id => @election.id else - render :action => 'new' + render :action => 'general_information' end end @@ -73,9 +78,9 @@ class ElectionController < ApplicationController def add_candidate @election = Election.find(params[:id]) @candidate = Candidate.new(params[:candidate]) - + @election.candidates << @candidate + if @candidate.save - @election.candidates << @candidate @candidate = Candidate.new redirect_to :action => 'edit_candidates', :id => @election.id else @@ -118,9 +123,9 @@ class ElectionController < ApplicationController def candidate_picture candidate = Candidate.find( params[:id] ) - send_data( candidate.picture_data, - :filename => candidate.picture_filename, - :type => candidate.picture_type, + send_data( candidate.picture.data, + :filename => candidate.picture.filename, + :type => candidate.picture.filetype, :disposition => 'inline' ) end @@ -128,12 +133,7 @@ class ElectionController < ApplicationController ## for a particular election #################################################################### def new_voters - @election = Election.find( params[:id] ) - if params.has_key?[:raw_voter_list] - process_incoming_voters( params[:raw_voter_list] ) - end - @raw_voter_list = RawVoterList.new - + edit_voters end def edit_voters @@ -207,8 +207,8 @@ class ElectionController < ApplicationController end # the new voter should be in good shape. save add to the election - new_voter.save @election.voters << new_voter + new_voter.save end end diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 430b6ab..eeba4d2 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -2,6 +2,10 @@ class Candidate < ActiveRecord::Base belongs_to :election validates_presence_of :name + # i have to call this picture_assoc because picture= does not overload + # the normal association method made by has_one + has_one :picture_obj, :class_name => "Picture" + # validate uniqueness of a name *within a given election* def <=>(other) @@ -12,24 +16,21 @@ 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 + def picture + picture_obj end - def base_part_of(filename) - name = File.basename(filename) - name.gsub(/[^\w._-]/, '') + def picture=(field) + if field and field.length > 0 + self.picture_obj = Picture.new.set_from_field(field) + return picture_obj.save + else + return false + end end def picture? - !self.picture_filename.nil? + !self.picture_obj.nil? end end diff --git a/app/models/election.rb b/app/models/election.rb index a574139..a77f445 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -57,7 +57,6 @@ class Election < ActiveRecord::Base def start_blockers reasons = [] - debugger if self.candidates.length <= 1 reasons << "You must have at least two candidates." end diff --git a/app/models/picture.rb b/app/models/picture.rb new file mode 100644 index 0000000..e7df084 --- /dev/null +++ b/app/models/picture.rb @@ -0,0 +1,20 @@ +class Picture < ActiveRecord::Base + belongs_to :candidate + + def set_from_field(field) + unless field.content_type.match(/^image/) + return false + end + self.filename = base_part_of(field.original_filename) + self.filetype = field.content_type.chomp + self.data = field.read + self + end + + def base_part_of(filename) + name = File.basename(filename) + name.gsub(/[^\w._-]/, '') + end + +end + diff --git a/app/views/election/_progress.rhtml b/app/views/election/_progress.rhtml new file mode 100644 index 0000000..520894c --- /dev/null +++ b/app/views/election/_progress.rhtml @@ -0,0 +1,16 @@ +<% progress_steps = [ ['overview', 'General Information'], + ['candidates', 'Candidates'], + ['voters', 'Voters'], + ['review', 'Review'] ] %> +
+ + + +
diff --git a/app/views/election/edit_candidates.rhtml b/app/views/election/edit_candidates.rhtml index 99f2f12..837f755 100644 --- a/app/views/election/edit_candidates.rhtml +++ b/app/views/election/edit_candidates.rhtml @@ -1,9 +1,11 @@ +<%= render_partial 'progress', 'candidates' %>

Edit/Add Candidates

<%= error_messages_for :candidate %> <% unless @election.candidates.empty? %>

The following are valid options or candidates in this election:

+1;3A <% @election.candidates.each do |candidate| %> <% @current_candidate = candidate %> @@ -22,4 +24,4 @@ <%= submit_tag "Add Candidate" %> <% end %> -<%= button_to "Done!", :action => 'show', :id => @election %> +<%= button_to "Done!", :action => 'new_voters', :id => @election %> diff --git a/app/views/election/new.rhtml b/app/views/election/general_information.rhtml similarity index 80% rename from app/views/election/new.rhtml rename to app/views/election/general_information.rhtml index 7b71748..0a1a513 100644 --- a/app/views/election/new.rhtml +++ b/app/views/election/general_information.rhtml @@ -1,3 +1,5 @@ +<%= render_partial 'progress', 'overview' %> +

Create A New Vote

Vote Overview

diff --git a/app/views/election/new_voters.rhtml b/app/views/election/new_voters.rhtml index 9c3bad3..fc49e13 100644 --- a/app/views/election/new_voters.rhtml +++ b/app/views/election/new_voters.rhtml @@ -1,3 +1,4 @@ +<%= render_partial 'progress', 'voters' %> <% @edit = true %>

<%= @election.name %>: Enter List of Voter Email Addresses

@@ -6,3 +7,4 @@ <% form_tag(:action => 'new_voters', :id => @election.id) do %> <%= render :partial => 'voters_form' %> <% end %> +<%= button_to "Done", :action => 'show', :id => @election.id %> diff --git a/app/views/election/show.rhtml b/app/views/election/show.rhtml index 0051861..ff06521 100644 --- a/app/views/election/show.rhtml +++ b/app/views/election/show.rhtml @@ -1,4 +1,4 @@ -<% %> +<%= render_partial 'progress', 'review' %>

Vote Information

<% if @election.active? %> @@ -54,7 +54,7 @@

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

<% end %> -<% unless @election.active %> +<% unless @election.active? %>

Start Election

<% if @election.start_blockers.length > 0 %> diff --git a/app/views/site/_basic_login.rhtml b/app/views/site/_basic_login.rhtml index f02d362..689a3f5 100644 --- a/app/views/site/_basic_login.rhtml +++ b/app/views/site/_basic_login.rhtml @@ -15,12 +15,5 @@

<%= link_to 'Lost or forgot your password?', :controller => 'account', :action => 'forgot_password' %>

-

Unfortunately, Selectricity is currently being tested and new - accounts for full votes (i.e., non-QuickVotes) can not - be automatically created. If you are interested in using - Selectricity to - run an organizational election, contact Benjamin Mako Hill.

- diff --git a/db/create.sql b/db/create.sql index a5a2e0a..298b99a 100644 --- a/db/create.sql +++ b/db/create.sql @@ -27,9 +27,20 @@ create table candidates ( election_id int NOT NULL, name varchar(100) NOT NULL, description text NULL, - picture_filename varchar(200), - picture_data blob, - picture_type varchar(100), + primary key (id) +); + +# CREATE pictures TABLE +##################################### + +drop table if exists pictures; +create table pictures ( + id int NOT NULL auto_increment, + filename varchar(200), + data blob, + filetype varchar(100), + candidate_id int NULL, + constraint fk_candidate_picture foreign key (candidate_id) references candidates(id), primary key (id) ); diff --git a/db/schema.rb b/db/schema.rb index 761fe47..57e39ac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -5,12 +5,9 @@ ActiveRecord::Schema.define() do create_table "candidates", :force => true do |t| - t.column "election_id", :integer, :null => false - t.column "name", :string, :limit => 100, :default => "", :null => false - t.column "description", :text - t.column "picture_filename", :string, :limit => 200 - t.column "picture_data", :binary - t.column "picture_type", :string, :limit => 100 + t.column "election_id", :integer, :null => false + t.column "name", :string, :limit => 100, :default => "", :null => false + t.column "description", :text end create_table "elections", :force => true do |t| @@ -28,6 +25,15 @@ ActiveRecord::Schema.define() do add_index "elections", ["user_id"], :name => "fk_user_election" + create_table "pictures", :force => true do |t| + t.column "filename", :string, :limit => 200 + t.column "data", :binary + t.column "filetype", :string, :limit => 100 + t.column "candidate_id", :integer + end + + add_index "pictures", ["candidate_id"], :name => "fk_candidate_picture" + create_table "rankings", :force => true do |t| t.column "vote_id", :integer t.column "candidate_id", :integer diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index 5072139..76eff61 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -313,3 +313,26 @@ li.moveable { padding: 5px; } +#election_creation_progress_bar ul li { + display: inline; + list-style: default; +} + +#election_creation_progress_bar ul li:after { + font-weight: normal; + color: #000; + content: " || "; +} + +#election_creation_progress_bar ul li.last:after { + content: ""; +} + +#election_creation_progress_bar li.step_selected { + font-weight: bold; +} + +#election_creation_progress_bar li.step_unselected { + color: #CCCCCC; + font-weight: bold; +}