Allow custimizable graph sizes, to fit with newly modular results page. Also,
[selectricity] / app / models / quick_vote.rb
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
7   
8   def initialize(params={})
9     super
10     self.startdate = Time.now
11     self.active = 1
12     self.anonymous = 1 unless self.anonymous
13     self.enddate = read_attribute( :enddate ) || \
14                    Time.now + 30.days - 1.second
15   end
16   
17   def enddate
18     super(30)
19   end
20   
21   def validate
22     if not @raw_candidates or @raw_candidates.length < 2
23       errors.add(nil, "You must list at least two candidates.")
24     end
25     
26     @raw_candidates.each do |c|
27       unless c.instance_of? String
28         errors.add(nil, "Candidates must be strings")
29         next
30       end
31       c.strip!
32       if c.length == 0
33         errors.add(nil, "Candidate name must not be empty")
34         next
35       end
36     end if @raw_candidates
37
38     errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
39
40     if name =~ /[^A-Za-z0-9]/
41       errors.add(:name, "must only include numbers and letters.")
42     end
43     if name =~ /^[0-9]+$/
44       errors.add(:name, "must not be a number")
45     end
46     
47     if name =~ /^(create|index|confirm|change|results)$/
48       errors.add(:name, " is a reserved word.")
49     end
50     
51     if enddate < startdate
52       errors.add(nil, "QuickVotes can't end before they start!")
53     end
54     
55   end
56
57   def candidatelist=(candlist)
58     @raw_candidates = candlist
59   end
60
61   def name
62     read_attribute( :name ).downcase() if read_attribute( :name )
63   end
64
65   def reviewed?
66     reviewed.to_i == 1
67   end
68
69   def create_candidates
70     return unless errors.empty?
71     @raw_candidates.each do |name|
72       candidate = Candidate.new({:name => name})
73       self.candidates << candidate
74     end
75   end
76
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)
82     else
83       quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]
84     end
85
86     return quickvote
87   end
88 end

Benjamin Mako Hill || Want to submit a patch?