1class Admin::Archived::PetitionEmailsController < Admin::AdminController |
|
2 before_action :fetch_petition |
|
3 before_action :build_email, only: [:new, :create] |
|
4 before_action :fetch_email, only: [:edit, :update, :destroy] |
|
6 def new |
|
7 render 'admin/archived/petitions/show' |
|
8 end |
|
10 def create |
|
11 if @email.update(email_params) |
|
12 if send_email_to_petitioners? |
|
13 schedule_email_petitioners_job
|
|
14 send_preview_email
|
|
15 message = :email_sent_overnight |
|
16 elsif send_preview_email? |
|
17 send_preview_email
|
|
18 message = :preview_email_sent |
|
19 else |
|
20 message = :petition_email_created |
|
21 end |
|
23 redirect_to admin_archived_petition_url(@petition), notice: message |
|
24 else |
|
25 render 'admin/archived/petitions/show' |
|
26 end |
|
27 end |
|
29 def edit |
|
30 end |
|
32 def update |
|
33 if @email.update(email_params) |
|
34 if send_email_to_petitioners? |
|
35 schedule_email_petitioners_job
|
|
36 send_preview_email
|
|
37 message = :email_sent_overnight |
|
38 elsif send_preview_email? |
|
39 send_preview_email
|
|
40 message = :preview_email_sent |
|
41 else |
|
42 message = :petition_email_updated |
|
43 end |
|
45 redirect_to admin_archived_petition_url(@petition), notice: message |
|
46 else |
|
47 render 'admin/archived/petitions/show' |
|
48 end |
|
49 end |
|
51 def destroy |
|
52 if @email.destroy |
|
53 message = :petition_email_deleted |
|
54 else |
|
55 message = :petition_email_not_deleted |
|
56 end |
|
58 redirect_to admin_archived_petition_url(@petition), notice: message |
|
59 end |
|
61 private
|
|
63 def fetch_petition |
|
64 @petition = ::Archived::Petition.published.find(params[:petition_id]) |
|
65 end |
|
67 def build_email |
|
68 @email = @petition.emails.build |
|
69 end |
|
71 def fetch_email |
|
72 @email = @petition.emails.find(params[:id]) |
|
73 end |
|
75 def email_params |
|
76 params.require(:archived_petition_email).permit(:subject, :body).merge(sent_by: current_user.pretty_name) |
|
77 end |
|
79 def feedback_signature |
|
80 FeedbackSignature.new(@petition) |
|
81 end |
|
83 def send_email_to_petitioners? |
|
84 params.key?(:save_and_email) |
|
85 end |
|
87 def send_preview_email? |
|
88 params.key?(:save_and_preview) |
|
89 end |
|
91 def schedule_email_petitioners_job |
|
92 ::Archived::EmailPetitionersJob.run_later_tonight(petition: @petition, email: @email) |
|
93 end |
|
95 def send_preview_email |
|
96 ::Archived::PetitionMailer.email_signer(@petition, feedback_signature, @email).deliver_now |
|
97 end |
|
98end |