]> projects.mako.cc - selectricity-live/blob - app/models/election.rb
removed save line that was causing the new elections to fail
[selectricity-live] / app / models / election.rb
1 class Election < ActiveRecord::Base
2   has_many :candidates
3   has_many :voters
4   has_many :votes
5   belongs_to :user
6   validates_presence_of :name, :description
7
8   require 'date'
9
10   def startdate
11     read_attribute( :startdate ) || Time.now
12   end
13
14   def enddate
15     date = read_attribute( :enddate ) || Time.now + 14
16     date - 1.second
17   end
18
19   def enddate=(date)
20     date += 1.day
21     date = Time.gm(*date)
22     super(date)
23   end
24
25   def destroy
26     self.candidates.each do |candidate|
27       candidate.destroy
28     end
29     super
30   end
31
32   def start_blockers
33     reasons = []
34     
35     if self.candidates.length <= 1
36       reasons << "You must have at least two candidates."
37     end
38     
39     if self.voters.length <= 1
40       reasons << "You must have at least two voters."
41     end
42
43     reasons
44   end
45
46   def activate!
47     self.active = 1
48     self.save!
49   end
50   
51   def quickvote?
52     quickvote.to_i == 1
53   end
54
55   def active?
56     active == 1
57   end 
58
59   def done?
60     active == 2
61   end
62
63   def shortdesc
64     shortdesc = description.split(/\n/)[0]
65   end
66
67   def longdesc
68     longdesc = description.split(/\n/)[1..-1].join("")
69     longdesc.length > 0 ? longdesc : nil 
70   end
71 end

Benjamin Mako Hill || Want to submit a patch?