/
process.rb
161 lines (131 loc) · 4.99 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Legislation::Process < ApplicationRecord
include ActsAsParanoidAliases
include Taggable
include Milestoneable
include Imageable
include Documentable
include SDG::Relatable
include Searchable
acts_as_paranoid column: :hidden_at
acts_as_taggable_on :customs
attribute :background_color, default: "#e7f2fc"
attribute :font_color, default: "#222222"
translates :title, touch: true
translates :summary, touch: true
translates :description, touch: true
translates :additional_info, touch: true
translates :milestones_summary, touch: true
translates :homepage, touch: true
include Globalizable
PHASES_AND_PUBLICATIONS = %i[homepage_phase draft_phase debate_phase allegations_phase
proposals_phase draft_publication result_publication].freeze
CSS_HEX_COLOR = /\A#?(?:[A-F0-9]{3}){1,2}\z/i
has_many :draft_versions, -> { order(:id) },
foreign_key: "legislation_process_id",
inverse_of: :process,
dependent: :destroy
has_one :final_draft_version, -> { where final_version: true, status: "published" },
class_name: "Legislation::DraftVersion",
foreign_key: "legislation_process_id",
inverse_of: :process
has_many :questions, -> { order(:id) },
foreign_key: "legislation_process_id",
inverse_of: :process,
dependent: :destroy
has_many :proposals, -> { order(:id) },
foreign_key: "legislation_process_id",
inverse_of: :process,
dependent: :destroy
validates_translation :title, presence: true
validates :start_date, presence: true
validates :end_date, presence: true
%i[draft debate proposals_phase allegations].each do |phase_name|
enabled_attribute = :"#{phase_name.to_s.gsub("_phase", "")}_phase_enabled?"
validates :"#{phase_name}_start_date", presence: true, if: enabled_attribute
validates :"#{phase_name}_end_date", presence: true, if: enabled_attribute
end
validate :valid_date_ranges
validates :background_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
validates :font_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
class << self; undef :open; end
scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current) }
scope :active, -> { where("end_date >= ?", Date.current) }
scope :past, -> { where("end_date < ?", Date.current) }
scope :published, -> { where(published: true) }
def self.not_in_draft
where("draft_phase_enabled = false or (draft_start_date IS NOT NULL and
draft_end_date IS NOT NULL and (draft_start_date > ? or
draft_end_date < ?))", Date.current, Date.current)
end
def homepage_phase
Legislation::Process::Phase.new(start_date, end_date, homepage_enabled)
end
def draft_phase
Legislation::Process::Phase.new(draft_start_date, draft_end_date, draft_phase_enabled)
end
def debate_phase
Legislation::Process::Phase.new(debate_start_date, debate_end_date, debate_phase_enabled)
end
def allegations_phase
Legislation::Process::Phase.new(allegations_start_date,
allegations_end_date, allegations_phase_enabled)
end
def proposals_phase
Legislation::Process::Phase.new(proposals_phase_start_date,
proposals_phase_end_date, proposals_phase_enabled)
end
def draft_publication
Legislation::Process::Publication.new(draft_publication_date, draft_publication_enabled)
end
def result_publication
Legislation::Process::Publication.new(result_publication_date, result_publication_enabled)
end
def enabled_phases?
PHASES_AND_PUBLICATIONS.any? { |process| send(process).enabled? }
end
def enabled_phases_and_publications_count
PHASES_AND_PUBLICATIONS.count { |process| send(process).enabled? }
end
def total_comments
questions.sum(:comments_count) + draft_versions.sum(&:total_comments)
end
def status
today = Date.current
if today < start_date
:planned
elsif end_date < today
:closed
else
:open
end
end
def searchable_translations_definitions
{
title => "A",
summary => "C",
description => "D"
}
end
def searchable_values
searchable_globalized_values
end
def self.search(terms)
pg_search(terms)
end
private
def valid_date_ranges
if end_date && start_date && end_date < start_date
errors.add(:end_date, :invalid_date_range)
end
if debate_end_date && debate_start_date && debate_end_date < debate_start_date
errors.add(:debate_end_date, :invalid_date_range)
end
if draft_end_date && draft_start_date && draft_end_date < draft_start_date
errors.add(:draft_end_date, :invalid_date_range)
end
if allegations_end_date && allegations_start_date &&
allegations_end_date < allegations_start_date
errors.add(:allegations_end_date, :invalid_date_range)
end
end
end