From 9aaeb23e7fc88becdacd56b3884d80b39672ff7a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 9 Mar 2016 16:47:10 +0100 Subject: [PATCH] sends an email for unfeasible spending proposals --- .../spending_proposals_controller.rb | 5 +++ app/mailers/mailer.rb | 9 ++++++ app/models/spending_proposal.rb | 8 +++++ .../unfeasible_spending_proposal.html.erb | 16 ++++++++++ config/locales/mailers.en.yml | 4 +++ config/locales/mailers.es.yml | 4 +++ spec/features/emails_spec.rb | 26 ++++++++++++++++ spec/models/spending_proposal_spec.rb | 31 +++++++++++++++++++ 8 files changed, 103 insertions(+) create mode 100644 app/views/mailer/unfeasible_spending_proposal.html.erb diff --git a/app/controllers/valuation/spending_proposals_controller.rb b/app/controllers/valuation/spending_proposals_controller.rb index f8c63c974..c0084a1c0 100644 --- a/app/controllers/valuation/spending_proposals_controller.rb +++ b/app/controllers/valuation/spending_proposals_controller.rb @@ -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 diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 356f2ea45..d0e6080bb 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -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) diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 237da73f7..df5cb17e4 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -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 diff --git a/app/views/mailer/unfeasible_spending_proposal.html.erb b/app/views/mailer/unfeasible_spending_proposal.html.erb new file mode 100644 index 000000000..0bb03ed6d --- /dev/null +++ b/app/views/mailer/unfeasible_spending_proposal.html.erb @@ -0,0 +1,16 @@ +

+ <%= @author.name %> +

+ +

+ <%= @spending_proposal.title %> +

+ +

+ <%= @spending_proposal.feasible_explanation %> +

+ +

+ <%= t("mailers.unfeasible_spending_proposal.responsible") %> + <%= @spending_proposal.administrator.id %> +

\ No newline at end of file diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index 8e337b42e..e7ff3f928 100755 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -20,3 +20,7 @@ en: new_reply_by_html: There is a new response from %{commenter} 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 \ No newline at end of file diff --git a/config/locales/mailers.es.yml b/config/locales/mailers.es.yml index fb0e7b2fb..b570dba46 100644 --- a/config/locales/mailers.es.yml +++ b/config/locales/mailers.es.yml @@ -20,3 +20,7 @@ es: new_reply_by_html: Hay una nueva respuesta de %{commenter} 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 \ No newline at end of file diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index fd3b219e6..f6fcbf901 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -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 diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index 2a10851b6..310a8064d 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -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