1class Admin::ModerationDelaysController < Admin::AdminController
 
2  before_action :build_moderation_delay
 
3
 
4  def new
 
5    respond_to do |format|
 
6      format.html
 
7    end
 
8  end
 
 9
 
10  def create
 
11    if @moderation_delay.valid?
 
12      save_attributes_to_session
 
13      enqeue_job
 
 
15      if send_email_to_creators?
 
16        redirect_to admin_petitions_url(state: :overdue_in_moderation), notice: :moderation_delay_sent
 
17      else
 
18        respond_to do |format|
 
19          format.html { render :new, notice: :moderation_delay_preview_sent }
 
20        end
 
21      end
 
22    else
 
23      respond_to do |format|
 
24        format.html { render :new }
 
25      end
 
26    end
 
27  end
 
 
29  private
 
 
31  def build_moderation_delay
 
32    @moderation_delay = ModerationDelay.new(moderation_delay_params)
 
33  end
 
 
35  def moderation_delay_params
 
36    if params.key?(:moderation_delay)
 
37      params.require(:moderation_delay).permit(:subject, :body)
 
38    else
 
39      session[:moderation_delay] || {}
 
40    end
 
41  end
 
 
43  def save_attributes_to_session
 
44    session[:moderation_delay] = @moderation_delay.attributes
 
45  end
 
 
47  def send_email_to_creators?
 
48    params.key?(:email_creators)
 
49  end
 
 
51  def enqeue_job
 
52    if send_email_to_creators?
 
53      job_class = NotifyCreatorsThatModerationIsDelayedJob
 
54      job_args = []
 
55      job_method = :perform_later
 
56    else
 
57      job_class = NotifyCreatorThatModerationIsDelayedJob
 
58      job_args = [feedback_signature]
 
59      job_method = :perform_now
 
60    end
 
 
62    job_args << @moderation_delay.subject
 
63    job_args << @moderation_delay.body
 
 
65    job_class.public_send(job_method, *job_args)
 
66  end
 
 
68  def feedback_signature
 
69    FeedbackSignature.new(example_petition)
 
70  end
 
 
72  def example_petition
 
73    Petition.overdue_in_moderation.last
 
74  end
 
75end