1class Admin::ModerationController < Admin::AdminController
 
2  before_action :fetch_petition
 
3
 
4  def update
 
5    if @petition.moderate(moderation_params)
 
6      send_notifications
 
7      redirect_to [:admin, @petition]
 
8    else
 
 9      render 'admin/petitions/show'
 
10    end
 
11  end
 
 
13  private
 
 
15  def fetch_petition
 
16    @petition = Petition.todo_list.find(params[:petition_id])
 
17  end
 
 
19  def moderation_params
 
20    params.require(:petition).permit(:moderation, rejection: [:code, :details])
 
21  end
 
 
23  def send_notifications
 
24    if send_email_to_creator_and_sponsors?
 
25      NotifyEveryoneOfModerationDecisionJob.perform_later(@petition)
 
26    end
 
27  end
 
 
29  def send_email_to_creator_and_sponsors?
 
30    params.key?(:save_and_email)
 
31  end
 
32end