1require 'active_support/number_helper' |
|
3class Parliament < ActiveRecord::Base |
|
4 include ActiveSupport::NumberHelper |
|
6 CUTOFF_DATE = Date.civil(2015, 5, 7) |
|
8 has_many :petitions, inverse_of: :parliament, class_name: "Archived::Petition" |
|
10 class << self |
|
11 def before_remove_const |
|
12 Thread.current[:__parliament__] = nil |
|
13 end |
|
15 def instance |
|
16 Thread.current[:__parliament__] ||= current_or_create |
|
17 end |
|
19 def archived(now = Time.current) |
|
20 where(arel_table[:archived_at].lteq(now)).order(archived_at: :desc) |
|
21 end |
|
23 def current |
|
24 where(archived_at: nil).order(created_at: :asc) |
|
25 end |
|
27 def government |
|
28 instance.government
|
|
29 end |
|
31 def opening_at |
|
32 instance.opening_at
|
|
33 end |
|
35 def opened?(now = Time.current) |
|
36 instance.opened?(now)
|
|
37 end |
|
39 def dissolution_at |
|
40 instance.dissolution_at
|
|
41 end |
|
43 def notification_cutoff_at |
|
44 instance.notification_cutoff_at
|
|
45 end |
|
47 def dissolution_heading |
|
48 instance.dissolution_heading
|
|
49 end |
|
51 def dissolution_message |
|
52 instance.dissolution_message
|
|
53 end |
|
55 def dissolved_heading |
|
56 instance.dissolved_heading
|
|
57 end |
|
59 def dissolved_message |
|
60 instance.dissolved_message
|
|
61 end |
|
63 def dissolution_faq_url |
|
64 instance.dissolution_faq_url
|
|
65 end |
|
67 def dissolution_faq_url? |
|
68 instance.dissolution_faq_url?
|
|
69 end |
|
71 def dissolved?(now = Time.current) |
|
72 instance.dissolved?(now)
|
|
73 end |
|
75 def dissolution_announced? |
|
76 instance.dissolution_announced?
|
|
77 end |
|
79 def registration_closed? |
|
80 instance.registration_closed?
|
|
81 end |
|
83 def reload |
|
84 Thread.current[:__parliament__] = nil |
|
85 end |
|
87 def current_or_create |
|
88 current.first_or_create(government: "TBC", opening_at: 2.weeks.ago) |
|
89 end |
|
90 end |
|
92 validates_presence_of :government, :opening_at |
|
93 validates_presence_of :dissolution_heading, :dissolution_message, if: :dissolution_at? |
|
94 validates_presence_of :dissolved_heading, :dissolved_message, if: :dissolved? |
|
95 validates_length_of :government, maximum: 100 |
|
96 validates_length_of :dissolution_heading, :dissolved_heading, maximum: 100 |
|
97 validates_length_of :dissolution_message, :dissolved_message, maximum: 600 |
|
98 validates_length_of :dissolution_faq_url, maximum: 500 |
|
99 validates_numericality_of :petition_duration, only_integer: true, allow_blank: true |
|
100 validates_numericality_of :petition_duration, greater_than_or_equal_to: 1, allow_blank: true |
|
101 validates_numericality_of :petition_duration, less_than_or_equal_to: 12, allow_blank: true |
|
103 after_save { Site.touch } |
|
|
105 def name |
106 "#{period} #{government} government" |
|
107 end |
|
|
109 def opened?(now = Time.current) |
110 opening_at? && opening_at <= now
|
|
111 end |
|
|
113 def period |
114 if opening_at? && dissolution_at? |
|
115 "#{opening_at.year}#{dissolution_at.year}" |
|
116 end |
|
117 end |
|
|
119 def period? |
120 period.present?
|
|
121 end |
|
|
123 def dissolved?(now = Time.current) |
124 dissolution_at? && dissolution_at <= now
|
|
125 end |
|
|
127 def dissolution_announced? |
128 dissolution_at?
|
|
129 end |
|
|
131 def registration_closed?(now = Time.current) |
132 registration_closed_at? && registration_closed_at <= now
|
|
133 end |
|
|
135 def archived?(now = Time.current) |
136 archived_at? && archived_at <= now
|
|
137 end |
|
|
139 def archiving? |
140 archiving_started_at? && !archiving_finished?
|
|
141 end |
|
|
143 def archiving_finished? |
144 archiving_started_at? && Petition.unarchived.empty? |
|
145 end |
|
|
147 def start_archiving!(now = Time.current) |
148 unless archiving? || archiving_finished? |
|
149 ArchivePetitionsJob.perform_later |
|
150 update_column(:archiving_started_at, now) |
|
151 end |
|
152 end |
|
|
154 def schedule_closure! |
155 if dissolution_announced? && !dissolved? |
|
156 ClosePetitionsEarlyJob.schedule_for(dissolution_at) |
|
157 StopPetitionsEarlyJob.schedule_for(dissolution_at) |
|
158 end |
|
159 end |
|
|
161 def notify_creators! |
162 if dissolution_announced? && !dissolved? |
|
163 NotifyCreatorsThatParliamentIsDissolvingJob.perform_later |
|
164 end |
|
165 end |
|
|
167 def archive!(now = Time.current) |
168 if archiving_finished? |
|
169 DeletePetitionsJob.perform_later |
|
170 update_column(:archived_at, now) |
|
171 end |
|
172 end |
|
|
174 def can_archive_petitions? |
175 dissolved? && !archiving_finished? && !archiving?
|
|
176 end |
|
|
178 def can_archive? |
179 dissolved? && archiving_finished?
|
|
180 end |
|
|
182 def formatted_threshold_for_response |
183 number_to_delimited(threshold_for_response)
|
|
184 end |
|
|
186 def formatted_threshold_for_debate |
187 number_to_delimited(threshold_for_debate)
|
|
188 end |
|
|
190 def show_on_a_map? |
191 opening_at > CUTOFF_DATE |
|
192 end |
|
193end |