sends an email for unfeasible spending proposals

This commit is contained in:
rgarcia
2016-03-09 16:47:10 +01:00
parent 1bbc12dc52
commit 9aaeb23e7f
8 changed files with 103 additions and 0 deletions

View File

@@ -18,6 +18,11 @@ class Valuation::SpendingProposalsController < Valuation::BaseController
def valuate
if valid_price_params? && @spending_proposal.update(valuation_params)
if @spending_proposal.marked_as_unfeasible?
Mailer.unfeasible_spending_proposal(@spending_proposal).deliver_later
end
redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate')
else
render action: :edit

View File

@@ -33,6 +33,15 @@ class Mailer < ApplicationMailer
end
end
def unfeasible_spending_proposal(spending_proposal)
@spending_proposal = spending_proposal
@author = spending_proposal.author
with_user(@author) do
mail(to: @author.email, subject: t('mailers.unfeasible_spending_proposal.subject'))
end
end
private
def with_user(user, &block)

View File

@@ -68,4 +68,12 @@ class SpendingProposal < ActiveRecord::Base
end
end
def marked_as_unfeasible?
previous_changes.has_key?("feasible") && unfeasible?
end
def unfeasible?
not feasible?
end
end

View File

@@ -0,0 +1,16 @@
<p>
<%= @author.name %>
</p>
<p>
<%= @spending_proposal.title %>
</p>
<p>
<%= @spending_proposal.feasible_explanation %>
</p>
<p>
<%= t("mailers.unfeasible_spending_proposal.responsible") %>
<%= @spending_proposal.administrator.id %>
</p>

View File

@@ -20,3 +20,7 @@ en:
new_reply_by_html: There is a new response from <b>%{commenter}</b> to your comment on
subject: Someone has responded to your comment
title: New response to your comment
unfeasible_spending_proposal:
hi: Hi
subject: Your spending proposal has been marked as not feasible
responsible: The responsible for the evaluation of your spending proposal is admin

View File

@@ -20,3 +20,7 @@ es:
new_reply_by_html: Hay una nueva respuesta de <b>%{commenter}</b> a tu comentario en
subject: Alguien ha respondido a tu comentario
title: Nueva respuesta a tu comentario
unfeasible_spending_proposal:
hi: Hola
subject: Tu propuesta de inversión ha sido marcada como inviable
responsible: El responsable de la evaluación de tu respuesta es el administrator

View File

@@ -122,4 +122,30 @@ feature 'Emails' do
expect(email).to have_body_text(user_confirmation_path)
end
scenario "Email on unfeasible spending proposal", :focus do
spending_proposal = create(:spending_proposal)
administrator = create(:administrator)
valuator = create(:valuator)
spending_proposal.update(administrator: administrator)
spending_proposal.valuators << valuator
login_as(valuator.user)
visit edit_valuation_spending_proposal_path(spending_proposal)
choose 'spending_proposal_feasible_false'
fill_in 'spending_proposal_feasible_explanation', with: 'This is not legal as stated in Article 34.9'
click_button 'Save changes'
expect(page).to have_content "Dossier updated"
spending_proposal.reload
email = open_last_email
expect(email).to have_subject('Your spending proposal has been marked as not feasible')
expect(email).to deliver_to(spending_proposal.author.email)
expect(email).to have_body_text(spending_proposal.author.name)
expect(email).to have_body_text(spending_proposal.title)
expect(email).to have_body_text(spending_proposal.feasible_explanation)
expect(email).to have_body_text("admin #{spending_proposal.administrator.id}")
end
end

View File

@@ -59,6 +59,37 @@ describe SpendingProposal do
expect(spending_proposal.feasibility).to eq "undefined"
end
end
describe "#unfeasible?" do
it "returns true when not feasible" do
spending_proposal.feasible = false
expect(spending_proposal.unfeasible?).to eq true
end
it "returns false when feasible" do
spending_proposal.feasible = true
expect(spending_proposal.unfeasible?).to eq false
end
end
describe "#marked_as_unfeasible?" do
let(:spending_proposal) { create(:spending_proposal) }
it "returns true when feasibility has changed and it is false" do
spending_proposal.update(feasible: false)
expect(spending_proposal.marked_as_unfeasible?).to eq true
end
it "returns false when feasibility has not changed" do
spending_proposal.update(price: 1000000)
expect(spending_proposal.marked_as_unfeasible?).to eq false
end
it "returns false when it is feasible" do
spending_proposal.update(feasible: true)
expect(spending_proposal.marked_as_unfeasible?).to eq false
end
end
end
describe "by_admin" do