From: Date: Mon, 20 Aug 2007 20:46:41 +0000 (-0400) Subject: Streamlined some graphign contorller methods and took out the 'voters_per_day' method... X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/257d5a4c8c02d7b001fbfbce4aaced9f9937ff61?hp=1544cbab02fbc3333bc53a5ac98d38e377640d4a Streamlined some graphign contorller methods and took out the 'voters_per_day' method in the results page. Merge from mako. --- diff --git a/app/controllers/graph_controller.rb b/app/controllers/graph_controller.rb index 1bde09e..442bb65 100644 --- a/app/controllers/graph_controller.rb +++ b/app/controllers/graph_controller.rb @@ -13,9 +13,10 @@ class GraphController < ApplicationController # fill in the data with the optional data name #Check to see if multiple datasets, if so, fill them all! + #Sort by biggest first piece of data. if options[:data].is_a?(Hash) - options[:data].each_pair do |name, array| - @graph.data( name, array) + options[:data].sort {|a,b| b[1][0] <=> a[1][0]}.each do |dataset| + @graph.data(dataset[0], dataset[1]) end #if each dataset nameless, will have only multiple arrays elsif options[:data].size > 1 && options[:data].all? {|i| i.is_a?(Array)} @@ -75,7 +76,7 @@ class GraphController < ApplicationController :y_axis_label => "Number of Votes") send_data(*graph.output) end - + def borda_bar @election = Election.find(params[:id]) @election.results unless @election.borda_result @@ -91,20 +92,24 @@ class GraphController < ApplicationController send_data(*graph.output) end #Acording to Tufte, small, concomparitive, highly labeled data sets usually - # belong in tables. The following is a bar graph...but would it be better + #belong in tables. The following is a bar graph...but would it be better #as a table? def choices_positions - @election = Election.find(params[:id]) - pref_tally = make_preference_tally(@election) - - fulldata, labels = get_positions_info(@election) - legend = Hash.new + legend = Hash.new + alldata, labels = get_positions_info(@election) + @election.results unless @election.condorcet_result || @election.ssd_result - @election.candidates.each_with_index do |candidate, index| - legend[candidate.name] = fulldata[index] + names = Hash.new + candidates = @election.candidates.sort.collect {|candidate| candidate.id} + candidates.each do |candidate| + names[candidate]= (Candidate.find(candidate)).name end + ranked_candidates = @election.condorcet_result.ranked_candidates.flatten + ranked_candidates.each_with_index \ + {|candidate, index| legend[names[candidate]] = alldata[index]} + graph = GruffGraff.new( :graph_type => Gruff::Bar, :data => legend, :interval_labels => labels, @@ -120,21 +125,23 @@ class GraphController < ApplicationController buckets2= Hash.new rank_labels = Hash.new - election.candidates.each do |candidate| - buckets[candidate.id] = [] - buckets2[candidate.id] = [] - end - #attach the ranking to the candidate's array to which is belongs + #creating a key if necessary election.votes.each do |vote| vote.rankings.each do |ranking| + + unless buckets.has_key?(ranking.candidate_id) + buckets[ranking.candidate_id] = [] + end buckets[ranking.candidate_id] << ranking.rank + end end #count how many times each candidate has been ranked at a certain level buckets.each_pair do |id, array| (1..election.candidates.size).each do |i| + buckets2[id] = [] unless buckets2.has_key?(id) buckets2[id] << (array.find_all {|rank| rank == i}).size end end diff --git a/app/controllers/quickvote_controller.rb b/app/controllers/quickvote_controller.rb index 41d0dc3..5b3acab 100644 --- a/app/controllers/quickvote_controller.rb +++ b/app/controllers/quickvote_controller.rb @@ -12,7 +12,6 @@ class QuickvoteController < ApplicationController def create if params[:quickvote] @quickvote = QuickVote.new(params[:quickvote]) - # store the candidate grabbed through ajax and stored in flash @quickvote.candidatelist = flash[:candlist] @quickvote.description=CGI.escapeHTML(@quickvote.description) @@ -52,11 +51,9 @@ class QuickvoteController < ApplicationController def index @election = QuickVote.ident_to_quickvote(params[:ident]) - # if the person has specified an election, we show them the voting # page. otherwise, we redirect back to main the page if @election - # look to see that the voter has been created and has voted in # this election, and has confirmed their vote @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", @@ -75,8 +72,8 @@ class QuickvoteController < ApplicationController @voter = QuickVoter.new @voter.election = @election @voter.session_id = session.session_id - - # create new vote and make it the defaulted sorted list + + # create new vote and make it the defaulted sorted list @voter.vote = Vote.new @voter.save @voter.vote.set_defaults! @@ -159,7 +156,11 @@ class QuickvoteController < ApplicationController ############################################################### def results - @election = QuickVote.ident_to_quickvote(params[:ident]) + unless @election = QuickVote.ident_to_quickvote(params[:ident]) + flash[:notice] = "Cannot find quickvote #{params[:ident]}." + redirect_to :controller => 'site' + return + end @election.results @candidates = {} @election.candidates.each {|c| @candidates[c.id] = c} diff --git a/app/views/election/edit.rhtml b/app/views/election/edit.rhtml index af0cb49..968d157 100644 --- a/app/views/election/edit.rhtml +++ b/app/views/election/edit.rhtml @@ -1,6 +1,6 @@

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

-<%= start_form_tag :action => 'update', :id => @election %> +<% form_tag(:action => 'update', :id => @election) do %> <%= render :partial => 'overview_form' %> <%= submit_tag 'Done!' %> -<%= end_form_tag %> +<% end %> diff --git a/app/views/election/edit_candidate.rhtml b/app/views/election/edit_candidate.rhtml index e465ae9..fe56aa0 100644 --- a/app/views/election/edit_candidate.rhtml +++ b/app/views/election/edit_candidate.rhtml @@ -1,9 +1,9 @@

Editing <%= @candidate.name %>

<%= error_messages_for :candidate %> -<%= form_tag( { :action => :update_candidate, :id => @candidate.id }, - :multipart => true ) %> +<% form_tag( { :action => :update_candidate, :id => @candidate.id }, + :multipart => true ) do %> <%= render :partial => 'candidate_form' %> <%= submit_tag "Save" %> -<%= end_form_tag %> +<% end %> diff --git a/app/views/election/edit_candidates.rhtml b/app/views/election/edit_candidates.rhtml index cc2c16a..99f2f12 100644 --- a/app/views/election/edit_candidates.rhtml +++ b/app/views/election/edit_candidates.rhtml @@ -16,10 +16,10 @@

Please enter new candidates below.

-<%= form_tag( { :action => :add_candidate, :id => @election.id }, - :multipart => true ) %> +<% form_tag( { :action => :add_candidate, :id => @election.id }, + :multipart => true ) do %> <%= render :partial => 'candidate_form' %> <%= submit_tag "Add Candidate" %> -<%= end_form_tag %> +<% end %> <%= button_to "Done!", :action => 'show', :id => @election %> diff --git a/app/views/election/edit_voters.rhtml b/app/views/election/edit_voters.rhtml index 429a907..1e98fba 100644 --- a/app/views/election/edit_voters.rhtml +++ b/app/views/election/edit_voters.rhtml @@ -3,8 +3,8 @@ <%= render :partial => 'voter_list' %> -<%= form_tag :action => 'edit_voters', :id => @election.id %> +<% form_tag (:action => 'edit_voters', :id => @election.id) do %> <%= render :partial => 'voters_form' %> -<%= end_form_tag %> +<% end %> <%= button_to 'Done!', :action => 'show', :id => @election.id %> diff --git a/app/views/election/new.rhtml b/app/views/election/new.rhtml index fc390a0..7b71748 100644 --- a/app/views/election/new.rhtml +++ b/app/views/election/new.rhtml @@ -2,8 +2,8 @@

Vote Overview

-<%= form_tag :action => 'create_election' %> +<% form_tag (:action => 'create_election') do %> <%= render :partial => 'overview_form' %> <%= submit_tag "Done!" %> -<%= end_form_tag %> +<% end %> diff --git a/app/views/election/new_voters.rhtml b/app/views/election/new_voters.rhtml index b750085..9c3bad3 100644 --- a/app/views/election/new_voters.rhtml +++ b/app/views/election/new_voters.rhtml @@ -3,6 +3,6 @@ <%= render :partial => 'voter_list' %> -<%= form_tag :action => 'new_voters', :id => @election.id %> +<% form_tag(:action => 'new_voters', :id => @election.id) do %> <%= render :partial => 'voters_form' %> -<%= end_form_tag %> +<% end %> diff --git a/app/views/quickvote/_candidate_list.rhtml b/app/views/quickvote/_candidate_list.rhtml index f4f1c06..64aa979 100644 --- a/app/views/quickvote/_candidate_list.rhtml +++ b/app/views/quickvote/_candidate_list.rhtml @@ -1,18 +1,18 @@ <% %> -<% if @flash[:candlist] %> +<% if flash[:candlist] %> <% end %> -<%= form_remote_tag :update => 'candlist', +<% form_remote_tag(:update => 'candlist', :url => { :action => 'add_candidate' }, - :complete => "Field.focus('ajax_newcandidate')" %> + :complete => "Field.focus('ajax_newcandidate')") do %>

<%= text_field "ajax", "newcandidate", :size => 40 %> <%= submit_tag "Add(+)" %>

-<%= end_form_tag %> +<% end %> diff --git a/app/views/quickvote/create.rhtml b/app/views/quickvote/create.rhtml index 6b6ada8..cf91e19 100644 --- a/app/views/quickvote/create.rhtml +++ b/app/views/quickvote/create.rhtml @@ -10,7 +10,7 @@ <%= render :partial => 'candidate_list' %> -<%= form_tag :action => 'create' %> +<% form_tag(:action => 'create') do %>

Please Login

- <%= start_form_tag :action => 'login' %> + <% form_tag(:action => 'login') do %> <%= form_input :text_field, "Login ID", "login", :size => 30 %> <%= form_input :password_field, "Password", "password", :size => 30 %> @@ -13,6 +13,6 @@

<%= link_to 'Register for an account', :action => 'signup' %>

<%= link_to 'Forgot my password', :action => 'forgot_password' %>

- <%= end_form_tag %> + <% end %> diff --git a/app/views/voter/_vote.rhtml b/app/views/voter/_vote.rhtml index d144791..b95fc96 100644 --- a/app/views/voter/_vote.rhtml +++ b/app/views/voter/_vote.rhtml @@ -12,12 +12,12 @@ least preferred. Please list all choices in every vote. (For example, 123 or 321 or 213, etc.)

<% if @voter.election.quickvote? %> - <%= form_tag quickaction_url( :ident => @voter.election.name, :action => 'confirm') %> + <% form_tag(quickaction_url( :ident => @voter.election.name, :action => 'confirm')) do %> <% else %> <%= form_tag :action => 'review', :id => @voter.password %> <% end %> <%= text_field :vote, :votestring -%> <%= submit_tag "Vote!" %> -<%= end_form_tag %> +<% end %> diff --git a/app/views/voter/index.rhtml b/app/views/voter/index.rhtml index 7e31c92..5c2ea27 100644 --- a/app/views/voter/index.rhtml +++ b/app/views/voter/index.rhtml @@ -2,7 +2,7 @@

Please enter your password/token to log in and vote:

-<%= form_tag :action => 'index' %> +<% form_tag(:action => 'index') do %> <%= text_field :vote, :password %> <%= submit_tag "Log In" %> -<%= end_form_tag %> +<% end %> diff --git a/test/functional/quickvote_controller_test.rb b/test/functional/quickvote_controller_test.rb index d0b37d9..076a104 100644 --- a/test/functional/quickvote_controller_test.rb +++ b/test/functional/quickvote_controller_test.rb @@ -1,6 +1,16 @@ require File.dirname(__FILE__) + '/../test_helper' require 'quickvote_controller' +class ActionController::TestSession + def session_id + # Override this so we can set session ID + # pass in the 'test_session_id' session variable to override default + return "12345678" unless @attributes + if @attributes.has_key? 'test_session_id' + return @attributes['test_session_id'] + end + end +end # Re-raise errors caught by the controller. class QuickvoteController; def rescue_action(e) raise e end; end @@ -12,7 +22,97 @@ class QuickvoteControllerTest < Test::Unit::TestCase end # Replace this with your real tests. - def test_truth - assert true + def test_index + get :index + assert_response 302 + end + + def test_create_quickvote + post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]}) + assert_template "quickvote/success" + get :index, { 'ident' => "variable"} + assert_response :success + end + + def test_create_dupe_quickvote + test_create_quickvote + assert_raise(Test::Unit::AssertionFailedError) do + test_create_quickvote + end + end + + def test_create_quickvote_badname + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]}) + assert_template "quickvote/create" + end + + def test_create_quickvote_dupe_candidate + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar", "foobar"]}) + assert_template "quickvote/create" + end + + def test_create_quickvote_nil_candidate + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil}) + assert_template "quickvote/create" + post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []}) + assert_template "quickvote/create" + end + + def test_get_quickvote_nonexistent + get :index, { 'ident' => "idontexist" } + assert_redirected_to :controller => 'site' + end + + def test_get_result_empty_vote + test_create_quickvote + get :results, { 'ident' => 'variable' } + assert_response :success + end + + def test_get_result_nonexistent + test_create_quickvote + get :results, { 'ident' => 'asdflaksdjf' } + assert_redirected_to :controller => 'site' + end + + def test_get_result_with_a_vote + test_cast_quickvote + get :results, { 'ident' => 'variable' } + assert_response :success + end + + def test_get_result_multi_votes + test_create_quickvote + votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id} + 5.times do |time| + get :index, { 'ident' => 'variable' }, { 'test_session_id' => (time+1)*50 } + assert_response :success + post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }, { 'test_session_id' => (time+1)*50 } + assert_template 'quickvote/thanks' + end + get :results, { 'ident' => 'variable' } + assert_response :success + end + + def test_cast_quickvote + test_create_quickvote + votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id} + post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} } + assert_template 'quickvote/thanks' + end + + def test_cast_dupe_quickvote + test_create_quickvote + votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id} + get :index, { 'ident' => 'variable' } + assert_response :success + + post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} } + assert_template 'quickvote/thanks' + + get :index, { 'ident' => 'variable' } + assert_response :success + post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} } + assert_redirected_to :controller => 'quickvote', :ident => 'variable' end end diff --git a/test/functional/site_controller_test.rb b/test/functional/site_controller_test.rb index 69b9348..ad9f06c 100644 --- a/test/functional/site_controller_test.rb +++ b/test/functional/site_controller_test.rb @@ -12,7 +12,8 @@ class SiteControllerTest < Test::Unit::TestCase end # Replace this with your real tests. - def test_truth - assert true + def test_index + get :index + assert_response :success end end