]> projects.mako.cc - selectricity/blob - app/models/quick_vote.rb
d6ab55b124dd2daa328b7452c80880975b553a31
[selectricity] / 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   end
23
24   def candidatelist=(candstring='')
25     @raw_candidates = candstring.split(';').collect {|cand| cand.strip }
26   end
27
28   def candidatelist
29     @raw_candidates.join("; ")
30   end
31
32   def name
33     read_attribute( :name ).downcase()
34   end
35
36   def reviewed?
37     if reviewed.to_i == 1
38       return true
39     else
40       false
41     end
42   end
43
44   def create_candidates
45     @raw_candidates.each do |name|
46       candidate = Candidate.new({:name => name})
47       candidate.save
48       self.candidates << candidate
49     end
50   end
51 end

Benjamin Mako Hill || Want to submit a patch?