1class Rejection < ActiveRecord::Base |
|
2 CODES = %w[duplicate irrelevant no-action honours fake-name foi libellous offensive] |
|
3 HIDDEN_CODES = %w[libellous offensive] |
|
5 belongs_to :petition, touch: true |
|
7 validates :petition, presence: true |
|
8 validates :code, presence: true, inclusion: { in: CODES } |
|
9 validates :details, length: { maximum: 4000 }, allow_blank: true |
|
11 after_create do |
|
12 if petition.rejected_at? |
|
13 petition.update!(state: state_for_petition) |
|
14 else |
|
15 petition.update!(state: state_for_petition, rejected_at: Time.current) |
|
16 end |
|
17 end |
|
|
19 def hide_petition? |
20 code.in?(HIDDEN_CODES) |
|
21 end |
|
|
23 def state_for_petition |
24 hide_petition? ? Petition::HIDDEN_STATE : Petition::REJECTED_STATE |
|
25 end |
|
26end |