1class Admin::Archived::ScheduleDebateController < Admin::AdminController
 
2  before_action :fetch_petition
 
3
 
4  def show
 
5    render 'admin/archived/petitions/show'
 
6  end
 
7
 
8  def update
 
 9    if @petition.update_attributes(params_for_update)
 
10      if send_email_to_petitioners?
 
11        ::Archived::EmailDebateScheduledJob.run_later_tonight(petition: @petition)
 
12        message = :email_sent_overnight
 
13      else
 
14        message = :debate_date_updated
 
15      end
 
 
17      redirect_to admin_archived_petition_url(@petition), notice: message
 
18    else
 
19      render 'admin/archived/petitions/show'
 
20    end
 
21  end
 
 
23  private
 
 
25  def fetch_petition
 
26    @petition = ::Archived::Petition.find(params[:petition_id])
 
27  end
 
 
29  def params_for_update
 
30    params.require(:archived_petition).permit(:scheduled_debate_date)
 
31  end
 
 
33  def send_email_to_petitioners?
 
34    params.key?(:save_and_email)
 
35  end
 
36end