Improve consistency of XMLRPC error reporting. Now, all error conditions will raise...
[selectricity-live] / app / models / quick_vote.rb
index d6ab55b124dd2daa328b7452c80880975b553a31..698dac3f02af15ad516918de69600c4e0bfe0f48 100644 (file)
 class QuickVote < Election
   after_validation :create_candidates
   validates_uniqueness_of :name
+  validates_presence_of :name
   attr_accessor :raw_candidates
   attr_accessor :reviewed
+  attr_accessor :plurality_result
+  attr_accessor :approval_result
+  attr_accessor :condorcet_result
+  attr_accessor :ssd_result
+  attr_accessor :borda_result
 
   def validate
-    if @raw_candidates.length < 2
-      errors.add("You must list at least two candidates.")
+    if not @raw_candidates or @raw_candidates.length < 2
+      errors.add(nil, "You must list at least two candidates.")
     end
+    
+    @raw_candidates.each do |c|
+      unless c.instance_of? String
+        errors.add(nil, "Candidates must be strings")
+        next
+      end
+      c.strip!
+      if c.length == 0
+        errors.add(nil, "Candidate name must not be empty")
+        next
+      end
+    end if @raw_candidates
+
+    errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
 
     if name =~ /[^A-Za-z0-9]/
-      errors.add("The name must only include numbers and letters.")
+      errors.add(:name, "must only include numbers and letters.")
+    end
+    if name =~ /^[0-9]+$/
+      errors.add(:name, "must not be a number")
+    end
+    
+    if name =~ /^(create|index|confirm|change|results)$/
+      errors.add(:name, " is a reserved word.")
     end
   end
   
   def initialize(params={})
     super
-    self.enddate =  DateTime.now + 30
+    self.startdate = Time.now
+    self.enddate =  Time.now + 30.days
     self.active = 1
     self.anonymous = 1
   end
 
-  def candidatelist=(candstring='')
-    @raw_candidates = candstring.split(';').collect {|cand| cand.strip }
-  end
-
-  def candidatelist
-    @raw_candidates.join("; ")
+  def candidatelist=(candlist)
+    @raw_candidates = candlist
   end
 
   def name
-    read_attribute( :name ).downcase()
+    read_attribute( :name ).downcase() if read_attribute( :name )
   end
 
   def reviewed?
-    if reviewed.to_i == 1
-      return true
-    else
-      false
-    end
+    reviewed.to_i == 1
   end
 
   def create_candidates
+    return unless errors.empty?
     @raw_candidates.each do |name|
       candidate = Candidate.new({:name => name})
-      candidate.save
       self.candidates << candidate
     end
   end
+
+  #Calculate Election Results
+  def results
+    # initalize the tallies to empty arrays
+    preference_tally = Array.new
+    plurality_tally = Array.new
+    approval_tally = Array.new
+
+    self.voters.each do |voter|
+      # skip if the voter has not voted or has an unconfirmed vote
+      next unless voter.voted?
+
+      plurality_tally << voter.vote.rankings.sort[0].candidate.id
+      approval_tally << voter.vote.rankings.sort[0..1].collect \
+        { |ranking| ranking.candidate.id }
+      preference_tally << voter.vote.rankings.sort.collect \
+        { |ranking| ranking.candidate.id }
+    end
+    @plurality_result = PluralityVote.new(plurality_tally).result
+    @approval_result = ApprovalVote.new(approval_tally).result
+    @condorcet_result = PureCondorcetVote.new(preference_tally).result
+    @ssd_result = CloneproofSSDVote.new(preference_tally).result
+    @borda_result = BordaVote.new(preference_tally).result
+    #@runoff_result = InstantRunoffVote.new(preference_tally).result
+    #@runoff_results = PluralityVote.new(preference_tally).result
+
+  end
+
+  ### Convert a shortname or id into a QuickVote
+  def self.ident_to_quickvote(ident)
+    return nil unless ident
+    if ident.match(/^\d+$/)
+      quickvote = QuickVote.find(ident)
+    else
+      quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]
+    end
+
+    return quickvote
+  end
 end

Benjamin Mako Hill || Want to submit a patch?