sends notification email to each voter
This commit is contained in:
@@ -10,6 +10,9 @@ class ProposalNotificationsController < ApplicationController
|
||||
@notification = ProposalNotification.new(notification_params)
|
||||
@proposal = Proposal.find(notification_params[:proposal_id])
|
||||
if @notification.save
|
||||
@proposal.voters.each do |voter|
|
||||
Mailer.proposal_notification(@notification, voter).deliver_later
|
||||
end
|
||||
redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification")
|
||||
else
|
||||
render :new
|
||||
|
||||
@@ -42,6 +42,14 @@ class Mailer < ApplicationMailer
|
||||
end
|
||||
end
|
||||
|
||||
def proposal_notification(notification, voter)
|
||||
@notification = notification
|
||||
|
||||
with_user(voter) do
|
||||
mail(to: voter.email, subject: @notification.title)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_user(user, &block)
|
||||
|
||||
@@ -97,6 +97,10 @@ class Proposal < ActiveRecord::Base
|
||||
cached_votes_up + physical_votes
|
||||
end
|
||||
|
||||
def voters
|
||||
votes_for.voters
|
||||
end
|
||||
|
||||
def editable?
|
||||
total_votes <= Setting["max_votes_for_proposal_edit"].to_i
|
||||
end
|
||||
|
||||
2
app/views/mailer/proposal_notification.html.erb
Normal file
2
app/views/mailer/proposal_notification.html.erb
Normal file
@@ -0,0 +1,2 @@
|
||||
<div><%= @notification.proposal.title %></div>
|
||||
<div><%= @notification.body %></div>
|
||||
@@ -148,4 +148,40 @@ feature 'Emails' do
|
||||
expect(email).to have_body_text(spending_proposal.feasible_explanation)
|
||||
end
|
||||
|
||||
scenario "Proposal notification" do
|
||||
author = create(:user)
|
||||
|
||||
noelia = create(:user)
|
||||
vega = create(:user)
|
||||
cristina = create(:user)
|
||||
|
||||
proposal = create(:proposal, author: author)
|
||||
|
||||
create(:vote, voter: noelia, votable: proposal, vote_flag: true)
|
||||
create(:vote, voter: vega, votable: proposal, vote_flag: true)
|
||||
|
||||
reset_mailer
|
||||
|
||||
login_as(author)
|
||||
visit root_path
|
||||
|
||||
visit new_proposal_notification_path(proposal_id: proposal.id)
|
||||
|
||||
fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal"
|
||||
fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!"
|
||||
click_button "Send"
|
||||
|
||||
expect(page).to have_content "Your message has been sent correctly."
|
||||
|
||||
expect(unread_emails_for(noelia.email).size).to eql parse_email_count(1)
|
||||
expect(unread_emails_for(vega.email).size).to eql parse_email_count(1)
|
||||
expect(unread_emails_for(cristina.email).size).to eql parse_email_count(0)
|
||||
expect(unread_emails_for(author.email).size).to eql parse_email_count(0)
|
||||
|
||||
email = open_last_email
|
||||
expect(email).to have_subject("Thank you for supporting my proposal")
|
||||
expect(email).to have_body_text("Please share it with others so we can make it happen!")
|
||||
expect(email).to have_body_text(proposal.title)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,9 +3,6 @@ require 'rails_helper'
|
||||
feature 'Proposal Notifications' do
|
||||
|
||||
scenario "Send a notification" do
|
||||
noelia = create(:user)
|
||||
vega = create(:user)
|
||||
|
||||
author = create(:user)
|
||||
proposal = create(:proposal, author: author)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user