1class Admin::ParliamentsController < Admin::AdminController
 
2  before_action :require_sysadmin
 
3  before_action :fetch_parliament
 
4
 
5  def show
 
6  end
 
7
 
8  def update
 
 9    if @parliament.update(parliament_params)
 
10      if email_creators?
 
11        @parliament.notify_creators!
 
12        redirect_to admin_root_url, notice: :creators_emailed
 
13      elsif schedule_closure?
 
14        @parliament.schedule_closure!
 
15        redirect_to admin_root_url, notice: :closure_scheduled
 
16      elsif archive_petitions?
 
17        @parliament.start_archiving!
 
18        redirect_to admin_root_url, notice: :petitions_archiving
 
19      elsif archive_parliament?
 
20        @parliament.archive!
 
21        redirect_to admin_root_url, notice: :parliament_archived
 
22      else
 
23        redirect_to admin_root_url, notice: :parliament_updated
 
24      end
 
25    else
 
26      render :show
 
27    end
 
28  end
 
 
30  private
 
 
32  def fetch_parliament
 
33    @parliament = Parliament.instance
 
34  end
 
 
36  def parliament_params
 
37    params.require(:parliament).permit(
 
38      :government, :opening_at,
 
39      :dissolution_heading, :dissolution_message,
 
40      :dissolved_heading, :dissolved_message,
 
41      :dissolution_at, :dissolution_faq_url,
 
42      :notification_cutoff_at, :registration_closed_at
 
43    )
 
44  end
 
 
46  def email_creators?
 
47    params.key?(:email_creators) && @parliament.dissolution_announced?
 
48  end
 
 
50  def schedule_closure?
 
51    params.key?(:schedule_closure) && @parliament.dissolution_announced?
 
52  end
 
 
54  def archive_petitions?
 
55    params.key?(:archive_petitions) && @parliament.can_archive_petitions?
 
56  end
 
 
58  def archive_parliament?
 
59    params.key?(:archive_parliament) && @parliament.can_archive?
 
60  end
 
61end