1 class QuickVote < Election
2 after_validation :create_candidates
3 validates_uniqueness_of :name
4 validates_presence_of :name
5 attr_accessor :raw_candidates
6 attr_accessor :reviewed
9 if not @raw_candidates or @raw_candidates.length < 2
10 errors.add(nil, "You must list at least two candidates.")
13 @raw_candidates.each do |c|
14 unless c.instance_of? String
15 errors.add(nil, "Candidates must be strings")
20 errors.add(nil, "Candidate name must not be empty")
23 end if @raw_candidates
25 errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
27 if name =~ /[^A-Za-z0-9]/
28 errors.add(:name, "must only include numbers and letters.")
31 errors.add(:name, "must not be a number")
34 if name =~ /^(create|index|confirm|change|results)$/
35 errors.add(:name, " is a reserved word.")
39 def initialize(params={})
41 self.startdate = Time.now
42 self.enddate = Time.now + 30.days
47 def candidatelist=(candlist)
48 @raw_candidates = candlist
52 read_attribute( :name ).downcase() if read_attribute( :name )
60 return unless errors.empty?
61 @raw_candidates.each do |name|
62 candidate = Candidate.new({:name => name})
63 self.candidates << candidate
67 ### Convert a shortname or id into a QuickVote
68 def self.ident_to_quickvote(ident)
69 return nil unless ident
70 if ident.match(/^\d+$/)
71 quickvote = QuickVote.find(ident)
73 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]