allows unfeasible emails only to be send out once
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddUnfeasibleEmailSentAtToSpendingProposals < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :spending_proposals, :unfeasible_email_sent_at, :datetime, default: nil
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user