1 class QuickVote < Election
2 before_validation :build_candidate_names
3 validates_uniqueness_of :name
5 attr_accessor :candidate_names
6 attr_accessor :reviewed
8 def initialize(params={})
10 self.startdate = Time.now
12 self.anonymous = 1 unless self.anonymous
16 if @candidate_names.length < 2
17 errors.add(nil, "You must list at least two candidates.")
20 @candidate_names.each do |c|
21 unless c.instance_of? String
22 errors.add(nil, "Candidates must be strings")
27 errors.add(nil, "Candidate name must not be empty")
30 end if @candidate_names
32 if @candidate_names and @candidate_names.uniq!
33 errors.add(nil, "Candidates must all be unique")
36 if name =~ /[^A-Za-z0-9]/
37 errors.add(:name, "must only include numbers and letters.")
40 errors.add(:name, "must not be a number")
43 if name =~ /^(create|index|confirm|change|results)$/
44 errors.add(:name, " is a reserved word.")
47 if enddate < startdate
48 errors.add(nil, "QuickVotes can't end before they start")
53 read_attribute( :name ).downcase() if read_attribute( :name )
60 def build_candidate_names
61 @candidate_names ||= []
62 if @candidate_names.empty? and not self.candidates.empty?
63 @candidate_names = self.candidates.collect {|c| c.name}
68 return unless errors.empty?
70 # delete the candidates
71 candidates.each {|c| c.destroy}
73 # create the new list based on the names
74 @candidate_names.each do |name|
75 candidate = Candidate.new({:name => name})
76 self.candidates << candidate
80 ### Convert a shortname or id into a QuickVote
81 def self.ident_to_quickvote(ident)
82 return nil unless ident
83 if ident.match(/^\d+$/)
84 quickvote = QuickVote.find(ident)
86 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]