From 467c06c3ca45d03266dba3b5a49b94b423877f0f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 18:06:43 +0200 Subject: [PATCH] allows unfeasible emails only to be send out once --- .../spending_proposals_controller.rb | 2 +- app/models/spending_proposal.rb | 11 +++++- ...ble_email_sent_at_to_spending_proposals.rb | 5 +++ db/schema.rb | 3 +- spec/features/emails_spec.rb | 1 + spec/models/spending_proposal_spec.rb | 37 +++++++++++++------ 6 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20160328152843_add_unfeasible_email_sent_at_to_spending_proposals.rb diff --git a/app/controllers/valuation/spending_proposals_controller.rb b/app/controllers/valuation/spending_proposals_controller.rb index c0084a1c0..deba1777d 100644 --- a/app/controllers/valuation/spending_proposals_controller.rb +++ b/app/controllers/valuation/spending_proposals_controller.rb @@ -20,7 +20,7 @@ class Valuation::SpendingProposalsController < Valuation::BaseController if valid_price_params? && @spending_proposal.update(valuation_params) if @spending_proposal.marked_as_unfeasible? - Mailer.unfeasible_spending_proposal(@spending_proposal).deliver_later + @spending_proposal.send_unfeasible_email end redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate') diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 8998fed6b..ea182b3fb 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -69,15 +69,24 @@ class SpendingProposal < ActiveRecord::Base end def marked_as_unfeasible? - previous_changes.has_key?("feasible") && unfeasible? + unfeasible_email_sent_at.blank? && unfeasible? && valuation_finished? end def unfeasible? not feasible? end + def valuation_finished? + valuation_finished + end + def code "#{id}" + (administrator.present? ? "-A#{administrator.id}" : "") end + def send_unfeasible_email + Mailer.unfeasible_spending_proposal(self).deliver_later + update(unfeasible_email_sent_at: Time.now) + end + end diff --git a/db/migrate/20160328152843_add_unfeasible_email_sent_at_to_spending_proposals.rb b/db/migrate/20160328152843_add_unfeasible_email_sent_at_to_spending_proposals.rb new file mode 100644 index 000000000..bbe1dc6ec --- /dev/null +++ b/db/migrate/20160328152843_add_unfeasible_email_sent_at_to_spending_proposals.rb @@ -0,0 +1,5 @@ +class AddUnfeasibleEmailSentAtToSpendingProposals < ActiveRecord::Migration + def change + add_column :spending_proposals, :unfeasible_email_sent_at, :datetime, default: nil + end +end diff --git a/db/schema.rb b/db/schema.rb index a406d3f4c..c6c5faa19 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160315092854) do +ActiveRecord::Schema.define(version: 20160328152843) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -311,6 +311,7 @@ ActiveRecord::Schema.define(version: 20160315092854) do t.integer "valuation_assignments_count", default: 0 t.integer "price_first_year", limit: 8 t.string "time_scope" + t.datetime "unfeasible_email_sent_at" end add_index "spending_proposals", ["author_id"], name: "index_spending_proposals_on_author_id", using: :btree diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 378098367..c6d675a73 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -134,6 +134,7 @@ feature 'Emails' do choose 'spending_proposal_feasible_false' fill_in 'spending_proposal_feasible_explanation', with: 'This is not legal as stated in Article 34.9' + check 'spending_proposal_valuation_finished' click_button 'Save changes' expect(page).to have_content "Dossier updated" diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index 340515f84..6d506d587 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -75,24 +75,39 @@ describe SpendingProposal do 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) + it "returns true when marked as unfeasibable and valuation_finished" do + spending_proposal.update(feasible: false, valuation_finished: true) 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 + it "returns false when marked as feasible" do spending_proposal.update(feasible: true) expect(spending_proposal.marked_as_unfeasible?).to eq false end - xit "when marked as unfeasible but there is no admin associated... - an exception occurs when sending the unfeasible email, - because spending_proposal.administrator.id is nil.." + it "returns false when marked as feasable and valuation_finished" do + spending_proposal.update(feasible: true, valuation_finished: true) + expect(spending_proposal.marked_as_unfeasible?).to eq false + end + + it "returns false when unfeasible email already sent" do + spending_proposal.update(unfeasible_email_sent_at: 1.day.ago) + expect(spending_proposal.marked_as_unfeasible?).to eq false + end + end + + describe "#send_unfeasible_email" do + let(:spending_proposal) { create(:spending_proposal) } + + it "sets the time when the unfeasible email was sent" do + expect(spending_proposal.unfeasible_email_sent_at).to_not be + spending_proposal.send_unfeasible_email + expect(spending_proposal.unfeasible_email_sent_at).to be + end + + it "send an email" do + expect {spending_proposal.send_unfeasible_email}.to change { ActionMailer::Base.deliveries.count }.by(1) + end end describe "#code" do