]> projects.mako.cc - selectricity-live/blob - app/models/quick_vote.rb
Made a large number of mostly cosmetic fixes after a detailed
[selectricity-live] / app / models / quick_vote.rb
1 class QuickVote < Election
2   after_validation :create_candidates
3   validates_uniqueness_of :name
4   attr_accessor :raw_candidates
5   attr_accessor :reviewed
6
7   def validate
8     if @raw_candidates.length < 2
9       errors.add("You must list at least two candidates.")
10     end
11
12     if name =~ /[^A-Za-z0-9]/
13       errors.add("The name must only include numbers and letters.")
14     end
15   end
16   
17   def initialize(params={})
18     super
19     self.enddate =  DateTime.now + 30
20     self.active = 1
21     self.anonymous = 1
22     self.quickvote = 1
23   end
24
25   def candidatelist=(candstring='')
26     @raw_candidates = candstring.split(';').collect {|cand| cand.strip }
27   end
28
29   def candidatelist
30     @raw_candidates.join("; ")
31   end
32
33   def name
34     read_attribute( :name ).downcase()
35   end
36
37   def reviewed?
38     reviewed.to_i == 1
39   end
40
41   def create_candidates
42     @raw_candidates.each do |name|
43       candidate = Candidate.new({:name => name})
44       candidate.save
45       self.candidates << candidate
46     end
47   end
48 end

Benjamin Mako Hill || Want to submit a patch?