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