From 68bf119c18471cc6f11114dfa93adfd2e5ddd082 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 14 Jan 2017 17:02:33 +0100 Subject: [PATCH] sends unfeasible budget investment --- .../budget_investments_controller.rb | 5 +++ app/mailers/mailer.rb | 9 +++++ app/models/budget/investment.rb | 11 +++++- .../budget_investment_unfeasible.html.erb | 35 +++++++++++++++++++ ...ble_email_sent_at_to_budget_investments.rb | 5 +++ db/schema.rb | 3 +- 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 app/views/mailer/budget_investment_unfeasible.html.erb create mode 100644 db/migrate/20170114154421_add_unfeasible_email_sent_at_to_budget_investments.rb diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index 03facadb3..666fac74a 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -21,6 +21,11 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController def valuate if valid_price_params? && @investment.update(valuation_params) + + if @investment.unfeasible_email_pending? + @investment.send_unfeasible_email + end + redirect_to valuation_budget_budget_investment_path(@budget, @investment), notice: t('valuation.budget_investments.notice.valuate') else render action: :edit diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index c5c4ae603..188de029b 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -82,6 +82,15 @@ class Mailer < ApplicationMailer end end + def budget_investment_unfeasible(investment) + @investment = investment + @author = investment.author + + with_user(@author) do + mail(to: @author.email, subject: t('mailers.budget_investment_unfeasible.subject', code: @investment.code)) + end + end + private def with_user(user, &block) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index dd98e472e..4c8816abc 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -136,12 +136,21 @@ class Budget unfeasible? && valuation_finished? end + def unfeasible_email_pending? + unfeasible_email_sent_at.blank? && unfeasible? && valuation_finished? + end + def total_votes cached_votes_up + physical_votes end def code - "B#{budget.id}I#{id}" + "#{created_at.strftime('%Y')}-#{id}" + (administrator.present? ? "-A#{administrator.id}" : "") + end + + def send_unfeasible_email + Mailer.budget_investment_unfeasible(self).deliver_later + update(unfeasible_email_sent_at: Time.current) end def reason_for_not_being_selectable_by(user) diff --git a/app/views/mailer/budget_investment_unfeasible.html.erb b/app/views/mailer/budget_investment_unfeasible.html.erb new file mode 100644 index 000000000..3bed46cec --- /dev/null +++ b/app/views/mailer/budget_investment_unfeasible.html.erb @@ -0,0 +1,35 @@ + + +

+ <%= t("mailers.budget_investment_unfeasible.hi") %> +

+ +

+ <%= t("mailers.budget_investment_unfeasible.unfeasible_html", + title: @investment.title) %> +

+ +

+ <%= @investment.unfeasibility_explanation %> +

+ +

+ <%= t("mailers.budget_investment_unfeasible.new_html", + url: link_to(t("mailers.budget_investment_unfeasible.new_href"), + new_budget_investment_url(@investment.budget), style: "color: #2895F1; text-decoration: underline;")) %> +

+ +

+ <%= t("mailers.budget_investment_unfeasible.reconsider_html", code: @investment.code) %> +

+ +

+ <%= t("mailers.budget_investment_unfeasible.sorry") %> +

+ +

+ <%= t("mailers.budget_investment_unfeasible.sincerely") %>
+ + <%= t("mailers.budget_investment_unfeasible.signatory") %> +

+ \ No newline at end of file diff --git a/db/migrate/20170114154421_add_unfeasible_email_sent_at_to_budget_investments.rb b/db/migrate/20170114154421_add_unfeasible_email_sent_at_to_budget_investments.rb new file mode 100644 index 000000000..dbd1a203b --- /dev/null +++ b/db/migrate/20170114154421_add_unfeasible_email_sent_at_to_budget_investments.rb @@ -0,0 +1,5 @@ +class AddUnfeasibleEmailSentAtToBudgetInvestments < ActiveRecord::Migration + def change + add_column :budget_investments, :unfeasible_email_sent_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a9498399..fb563c8db 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: 20170106130838) do +ActiveRecord::Schema.define(version: 20170114154421) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -143,6 +143,7 @@ ActiveRecord::Schema.define(version: 20170106130838) do t.boolean "selected", default: false t.string "location" t.string "organization_name" + t.datetime "unfeasible_email_sent_at" end add_index "budget_investments", ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree