cf7bba58a2974f409a2828226d4dd43dc2f023ac
[selectricity] / 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 votes
26     votes = Array.new
27     self.voters.each do |voter|
28       votes << voter.vote
29     end
30     return votes
31   end
32
33   def destroy
34     self.candidates.each do |candidate|
35       candidate.destroy
36     end
37     super
38   end
39
40   def start_blockers
41     reasons = []
42     
43     if self.candidates.length <= 1
44       reasons << "You must have at least two candidates."
45     end
46     
47     if self.voters.length <= 1
48       reasons << "You must have at least two voters."
49     end
50
51     reasons
52   end
53
54   def activate!
55     self.active = 1
56     self.save!
57   end
58   
59   def quickvote?
60     type == 'QuickVote'
61   end
62
63   def active?
64     active == 1
65   end 
66
67   def done?
68     active == 2
69   end
70
71   def shortdesc
72     shortdesc = description.split(/\n/)[0]
73   end
74
75   def longdesc
76     longdesc = description.split(/\n/)[1..-1].join("")
77     longdesc.length > 0 ? longdesc : nil 
78   end
79 end

Benjamin Mako Hill || Want to submit a patch?