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
8 def initialize(params={})
10 self.startdate = Time.now
12 self.anonymous = 1 unless self.anonymous
13 self.enddate = read_attribute( :enddate ) || \
14 Time.now + 30.days - 1.second
22 if not @raw_candidates or @raw_candidates.length < 2
23 errors.add(nil, "You must list at least two candidates.")
26 @raw_candidates.each do |c|
27 unless c.instance_of? String
28 errors.add(nil, "Candidates must be strings")
33 errors.add(nil, "Candidate name must not be empty")
36 end if @raw_candidates
38 errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
40 if name =~ /[^A-Za-z0-9]/
41 errors.add(:name, "must only include numbers and letters.")
44 errors.add(:name, "must not be a number")
47 if name =~ /^(create|index|confirm|change|results)$/
48 errors.add(:name, " is a reserved word.")
52 def candidatelist=(candlist)
53 @raw_candidates = candlist
57 read_attribute( :name ).downcase() if read_attribute( :name )
65 return unless errors.empty?
66 @raw_candidates.each do |name|
67 candidate = Candidate.new({:name => name})
68 self.candidates << candidate
72 ### Convert a shortname or id into a QuickVote
73 def self.ident_to_quickvote(ident)
74 return nil unless ident
75 if ident.match(/^\d+$/)
76 quickvote = QuickVote.find(ident)
78 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]