Skip to content

Commit

Permalink
Add admin page for emailing petitioners
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed Aug 7, 2015
1 parent a76d28c commit f716fce
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/controllers/admin/petition_emails_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Admin::PetitionEmailsController < Admin::AdminController
respond_to :html
before_action :fetch_petition
before_action :build_email

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

redirect_to [:admin, @petition], notice: 'Email will be sent overnight'
else
render 'admin/petitions/show'
end
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)
end

def email_params
params.require(:petition_email).permit(:subject, :body)
end

def feedback_signature
FeedbackSignature.new(@petition)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to 'Email petitioners', new_admin_petition_email_path(@petition), class: 'petition-action-heading' %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h2 class="petition-action-heading">Email petitioners</h2>
<%= form_for @email, url: admin_petition_emails_path(petition), method: :post 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_signatures_with_count_submit_button(f, petition) %>
<% end -%>

<%= javascript_include_tag 'character-counter' %>
4 changes: 4 additions & 0 deletions app/views/admin/petitions/_petition_actions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
</li>
<% end %>

<li class="petition-action">
<%= render 'petition_action_email_petitioners', petition: @petition %>
</li>

<% if current_user.can_take_petitions_down? && @petition.can_be_signed? %>
<li class="petition-action">
<%= render 'petition_action_take_down', petition: @petition %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
resources :admin_users
resources :petitions, :only => [:show, :index] do
resource 'debate-outcome', only: [:show, :update], as: :debate_outcome, controller: :debate_outcomes
resources :emails, only: [:new, :create], controller: :petition_emails
resource :petition_details, :only => [:show, :update]
resource :moderation, :only => [:update], controller: :moderation
resource :notes, :only => [:show, :update]
Expand Down
23 changes: 23 additions & 0 deletions features/admin/petition_email.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@admin
Feature: Emailing petitioner supporters
In order to keep petition supporters up-to-date on their petition
As an admin user
I want to send an email to all petition supporters

Scenario: Sending 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 "Email petitioners"
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 signatures"
Then the petition should not have any emails
And I should see an error
When I fill in the email details
And press "Email 6 signatures"
Then the petition should have the email details I provided
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
52 changes: 52 additions & 0 deletions features/step_definitions/petition_email_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
When(/^I fill in the email details$/) do
fill_in "Subject", :with => "Petition email subject"
fill_in "Body", :with => "Petition email body"
end

Then(/^the petition should not have any emails$/) do
@petition.reload
expect(@petition.emails).to be_empty
end

Then(/^the petition should have the email details I provided$/) do
@petition.reload
@email = @petition.emails.last
expect(@email.subject).to eq("Petition email subject")
expect(@email.body).to match(%r[Petition email body])
expect(@email.sent_by).to eq("Admin User")
end

Then(/^the petition creator should have been emailed with the update$/) do
@petition.reload
steps %Q(
Then "#{@petition.creator_signature.email}" should receive an email
When they open the email
Then they should see "Petition email body" in the email body
When they follow "#{petition_url(@petition)}" in the email
Then I should be on the petition page for "#{@petition.action}"
)
end

Then(/^all the signatories of the petition should have been emailed with the update$/) do
@petition.reload
@petition.signatures.notify_by_email.validated.where.not(id: @petition.creator_signature.id).each do |signatory|
steps %Q(
Then "#{signatory.email}" should receive an email
When they open the email
Then they should see "Petition email body" in the email body
When they follow "#{petition_url(@petition)}" in the email
Then I should be on the petition page for "#{@petition.action}"
)
end
end

Then(/^the feedback email address should have been emailed a copy$/) do
signatory = FeedbackSignature.new(@petition)
steps %Q(
Then "#{signatory.email}" should receive an email
When they open the email
Then they should see "Petition email body" in the email body
When they follow "#{petition_url(@petition)}" in the email
Then I should be on the petition page for "#{@petition.action}"
)
end
3 changes: 3 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def admin_url(admin_page)
when /^debate outcomes form page for "([^\"]*)"$/
admin_petition_debate_outcome_url(Petition.find_by(action: $1))

when /^email petitioners form page for "([^\"]*)"$/
new_admin_petition_email_url(Petition.find_by(action: $1))

when /^government response page for "([^\"]*)"$/
admin_petition_government_response_url(Petition.find_by(action: $1))

Expand Down
Loading

0 comments on commit f716fce

Please sign in to comment.