-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from alphagov/add-petition-email-crud
Add petition email crud
- Loading branch information
Showing
12 changed files
with
990 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,84 @@ | ||
class Admin::PetitionEmailsController < Admin::AdminController | ||
respond_to :html | ||
before_action :fetch_petition | ||
before_action :build_email | ||
before_action :build_email, only: [:new, :create] | ||
before_action :fetch_email, only: [:edit, :update, :destroy] | ||
|
||
def new | ||
render 'admin/petitions/show' | ||
end | ||
|
||
def create | ||
if @email.update(email_params) | ||
EmailPetitionersJob.run_later_tonight(petition: @petition, email: @email) | ||
PetitionMailer.email_signer(@petition, feedback_signature, @email).deliver_now | ||
if send_email_to_petitioners? | ||
schedule_email_petitioners_job | ||
message = 'Email will be sent overnight' | ||
else | ||
message = 'Created other parliamentary business successfully' | ||
end | ||
|
||
redirect_to [:admin, @petition], notice: 'Email will be sent overnight' | ||
redirect_to [:admin, @petition], notice: message | ||
else | ||
render 'admin/petitions/show' | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @email.update(email_params) | ||
if send_email_to_petitioners? | ||
schedule_email_petitioners_job | ||
message = 'Email will be sent overnight' | ||
else | ||
message = 'Updated other parliamentary business successfully' | ||
end | ||
|
||
redirect_to [:admin, @petition], notice: message | ||
else | ||
render 'admin/petitions/show' | ||
end | ||
end | ||
|
||
def destroy | ||
if @email.destroy | ||
message = 'Deleted other parliamentary business successfully' | ||
else | ||
message = 'Unable to delete other parliamentary business - please contact support' | ||
end | ||
|
||
redirect_to [:admin, @petition], notice: message | ||
end | ||
|
||
private | ||
|
||
def fetch_petition | ||
@petition = Petition.moderated.find(params[:petition_id]) | ||
end | ||
|
||
def build_email | ||
@email = @petition.emails.build(sent_by: current_user.pretty_name) | ||
@email = @petition.emails.build | ||
end | ||
|
||
def fetch_email | ||
@email = @petition.emails.find(params[:id]) | ||
end | ||
|
||
def email_params | ||
params.require(:petition_email).permit(:subject, :body) | ||
params.require(:petition_email).permit(:subject, :body).merge(sent_by: current_user.pretty_name) | ||
end | ||
|
||
def feedback_signature | ||
FeedbackSignature.new(@petition) | ||
end | ||
|
||
def send_email_to_petitioners? | ||
params.key?(:save_and_email) | ||
end | ||
|
||
def schedule_email_petitioners_job | ||
EmailPetitionersJob.run_later_tonight(petition: @petition, email: @email) | ||
PetitionMailer.email_signer(@petition, feedback_signature, @email).deliver_now | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/views/admin/admin/_petition_action_email_petitioners.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<%= link_to 'Add an item of parliamentary business', new_admin_petition_email_path(@petition), class: 'petition-action-heading' %> | ||
<%= link_to 'Other parliamentary business', new_admin_petition_email_path(@petition), class: 'petition-action-heading' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<div class="grid-row"> | ||
<div class="column-two-thirds extra-gutter"> | ||
<h1>Edit other parliamentary business</h1> | ||
|
||
<%= form_for @email, url: admin_petition_email_path(@petition, @email), method: :patch do |f| -%> | ||
<%= form_row :for => [f.object, :subject] do %> | ||
<%= f.label :subject, 'Subject', class: 'form-label' %> | ||
<%= error_messages_for_field f.object, :subject %> | ||
<%= f.text_area :subject, rows: 2, cols: 70, tabindex: increment, data: { max_length: 100 }, class: 'form-control' %> | ||
<p class="character-count">100 characters max</p> | ||
<% end %> | ||
|
||
<%= form_row :for => [f.object, :body] do %> | ||
<%= f.label :body, 'Body', class: 'form-label' %> | ||
<%= error_messages_for_field f.object, :body %> | ||
<%= f.text_area :body, rows: 8, cols: 70, tabindex: increment, data: { max_length: 5000 }, class: 'form-control' %> | ||
<p class="character-count">5000 characters max</p> | ||
<% end %> | ||
|
||
<%= email_petitioners_with_count_submit_button(f, @petition) %> | ||
<%= f.submit "Save without emailing", name: 'save', class: 'button-secondary' %> | ||
<% end -%> | ||
</div> | ||
<div class="petition-meta column-third"> | ||
<%= render 'admin/petitions/petition_details' %> | ||
</div> | ||
</div> | ||
|
||
<%= javascript_include_tag 'character-counter' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ Feature: Emailing petitioner supporters | |
And I am logged in as a sysadmin with the email "[email protected]", first_name "Admin", last_name "User" | ||
When I am on the admin all petitions page | ||
And I follow "Ban Badger Baiting" | ||
And I follow "Add an item of parliamentary business" | ||
And I follow "Other parliamentary business" | ||
Then I should be on the admin email petitioners form page for "Ban Badger Baiting" | ||
And the markup should be valid | ||
When I press "Email 6 petitioners" | ||
|
@@ -21,3 +21,54 @@ Feature: Emailing petitioner supporters | |
And the petition creator should have been emailed with the update | ||
And all the signatories of the petition should have been emailed with the update | ||
And the feedback email address should have been emailed a copy | ||
|
||
Scenario: Saving an email to all petitioners | ||
Given an open petition "Ban Badger Baiting" with some signatures | ||
And I am logged in as a sysadmin with the email "[email protected]", first_name "Admin", last_name "User" | ||
When I am on the admin all petitions page | ||
And I follow "Ban Badger Baiting" | ||
And I follow "Other parliamentary business" | ||
Then I should be on the admin email petitioners form page for "Ban Badger Baiting" | ||
And the markup should be valid | ||
When I press "Save without emailing" | ||
Then the petition should not have any emails | ||
And I should see an error | ||
When I fill in the email details | ||
And press "Save without emailing" | ||
Then the petition should have the email details I provided | ||
And the petition creator should not have been emailed with the update | ||
And all the signatories of the petition should not have been emailed with the update | ||
And the feedback email address should not have been emailed a copy | ||
|
||
Scenario: Updating an email to all petitioners | ||
Given an open petition "Ban Badger Baiting" with some signatures | ||
And it has an existing petition email "This will be debated" | ||
And I am logged in as a sysadmin with the email "[email protected]", first_name "Admin", last_name "User" | ||
When I am on the admin all petitions page | ||
And I follow "Ban Badger Baiting" | ||
And I follow "Other parliamentary business" | ||
Then I should be on the admin email petitioners form page for "Ban Badger Baiting" | ||
And the markup should be valid | ||
And I should see "This will be debated" | ||
When I press "Edit" | ||
Then I should see "Edit other parliamentary business" | ||
When I fill in "Subject" with "This will not be debated" | ||
And I press "Save without emailing" | ||
Then I should see "Updated other parliamentary business successfully" | ||
When I follow "Other parliamentary business" | ||
Then I should see "This will not be debated" | ||
|
||
Scenario: Deleting an email to all petitioners | ||
Given an open petition "Ban Badger Baiting" with some signatures | ||
And it has an existing petition email "This will be debated" | ||
And I am logged in as a sysadmin with the email "[email protected]", first_name "Admin", last_name "User" | ||
When I am on the admin all petitions page | ||
And I follow "Ban Badger Baiting" | ||
And I follow "Other parliamentary business" | ||
Then I should be on the admin email petitioners form page for "Ban Badger Baiting" | ||
And the markup should be valid | ||
And I should see "This will be debated" | ||
When I press "Delete" | ||
Then I should see "Deleted other parliamentary business successfully" | ||
When I follow "Other parliamentary business" | ||
Then I should not see "This will be debated" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.