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.")
51 if enddate < startdate
52 errors.add(nil, "QuickVotes can't end before they start!")
57 def candidatelist=(candlist)
58 @raw_candidates = candlist
62 read_attribute( :name ).downcase() if read_attribute( :name )
70 return unless errors.empty?
71 @raw_candidates.each do |name|
72 candidate = Candidate.new({:name => name})
73 self.candidates << candidate
77 ### Convert a shortname or id into a QuickVote
78 def self.ident_to_quickvote(ident)
79 return nil unless ident
80 if ident.match(/^\d+$/)
81 quickvote = QuickVote.find(ident)
83 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]