1class Rejection < ActiveRecord::Base
 
2  CODES = %w[duplicate irrelevant no-action honours fake-name foi libellous offensive]
 
3  HIDDEN_CODES = %w[libellous offensive]
 
4
 
5  belongs_to :petition, touch: true
 
6
 
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
 
  • Complexity 1 » saikuro
19  def hide_petition?
 
20    code.in?(HIDDEN_CODES)
 
21  end
 
  • Complexity 2 » saikuro
23  def state_for_petition
 
24    hide_petition? ? Petition::HIDDEN_STATE : Petition::REJECTED_STATE
 
25  end
 
26end