From 9aaeb23e7fc88becdacd56b3884d80b39672ff7a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 9 Mar 2016 16:47:10 +0100 Subject: [PATCH 01/13] 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 From 2fe1e0d6247a0a63dc45dba83f5e940ddd2def39 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 9 Mar 2016 16:47:35 +0100 Subject: [PATCH 02/13] adds pending spec for edge case --- spec/models/spending_proposal_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index 310a8064d..bf5ee138f 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -89,6 +89,10 @@ describe SpendingProposal 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.." end end From 50b1571126db6f3ee4a18b8a0820a1a4c33f837e Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 12:14:33 +0200 Subject: [PATCH 03/13] adds code to spending proposals --- app/models/spending_proposal.rb | 4 ++++ spec/models/spending_proposal_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index df5cb17e4..81293b386 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -76,4 +76,8 @@ class SpendingProposal < ActiveRecord::Base not feasible? end + def code + "#{id}" + (administrator.present? ? "-#{administrator.id}" : "") + end + end diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index bf5ee138f..9ca2fae94 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -94,6 +94,19 @@ describe SpendingProposal do an exception occurs when sending the unfeasible email, because spending_proposal.administrator.id is nil.." end + + describe "#code" do + let(:spending_proposal) { create(:spending_proposal) } + + it "returns the proposal id" do + expect(spending_proposal.code).to eq("#{spending_proposal.id}") + end + + it "returns the administrator id when assigned" do + spending_proposal.administrator = create(:administrator) + expect(spending_proposal.code).to eq("#{spending_proposal.id}-#{spending_proposal.administrator.id}") + end + end end describe "by_admin" do From e31fe0060860100bdfa40ee3ebcd948d7668a0c2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 12:15:35 +0200 Subject: [PATCH 04/13] updates email text --- .../unfeasible_spending_proposal.html.erb | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/app/views/mailer/unfeasible_spending_proposal.html.erb b/app/views/mailer/unfeasible_spending_proposal.html.erb index 0bb03ed6d..958879493 100644 --- a/app/views/mailer/unfeasible_spending_proposal.html.erb +++ b/app/views/mailer/unfeasible_spending_proposal.html.erb @@ -1,16 +1,16 @@ -

- <%= @author.name %> -

+

Estimado usuario,

-

- <%= @spending_proposal.title %> -

+

Desde el Ayuntamiento de Madrid queremos agradecer tu participación en los Presupuestos Participativos de la ciudad de Madrid. +Lamentamos informarte de que tu propuesta '<%= @spending_proposal.title %>' quedará excluida de este proceso participativo por el siguiente motivo:

-

- <%= @spending_proposal.feasible_explanation %> -

+

<%= @spending_proposal.feasible_explanation %>

-

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

\ No newline at end of file +

Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: +<%= link_to "nueva propuesta de inversión", new_spending_proposal_url %>

+ +

Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, al correo preparticipativos@madrid.es, indicando necesariamente para su tramitación el código <%= @spending_proposal.code %> como asunto del correo, correspondiente a tu propuesta.

+ +

Sentimos las molestias ocasionadas y volvemos a darte las gracias por tu inestimable participación.

+ +

Atentamente

+

DIRECCIÓN GENERAL DE PARTICIPACIÓN CIUDADANA

\ No newline at end of file From d7996cd01bab68a634ab5186bc02a1bf8c7f0646 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 12:15:47 +0200 Subject: [PATCH 05/13] fixes specs --- spec/features/emails_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index f6fcbf901..849c5a7e2 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -142,10 +142,9 @@ feature 'Emails' do 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}") + expect(email).to have_body_text("#{spending_proposal.id}-#{spending_proposal.administrator.id}") end end From db5348938efd57d2b3ce737a20c6ae9bc0c29e8c Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 14:08:39 +0200 Subject: [PATCH 06/13] adds code to email subject --- app/mailers/mailer.rb | 2 +- spec/features/emails_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index d0e6080bb..fcc2a62b1 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -38,7 +38,7 @@ class Mailer < ApplicationMailer @author = spending_proposal.author with_user(@author) do - mail(to: @author.email, subject: t('mailers.unfeasible_spending_proposal.subject')) + mail(to: @author.email, subject: t('mailers.unfeasible_spending_proposal.subject', code: @spending_proposal.code)) end end diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 849c5a7e2..3ca35a14c 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -140,11 +140,10 @@ feature 'Emails' do spending_proposal.reload email = open_last_email - expect(email).to have_subject('Your spending proposal has been marked as not feasible') + expect(email).to have_subject("Your investment project '#{spending_proposal.id}-#{spending_proposal.administrator.id}' has been marked as unfeasible") expect(email).to deliver_to(spending_proposal.author.email) 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("#{spending_proposal.id}-#{spending_proposal.administrator.id}") end end From 034c3b527ed95d8ea69e30a7c0ac20ca76d65ede Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 14:08:57 +0200 Subject: [PATCH 07/13] adds translations for unfeasible email --- .../unfeasible_spending_proposal.html.erb | 19 ++++++++++--------- config/locales/mailers.en.yml | 12 +++++++++--- config/locales/mailers.es.yml | 12 +++++++++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/views/mailer/unfeasible_spending_proposal.html.erb b/app/views/mailer/unfeasible_spending_proposal.html.erb index 958879493..b89e83afa 100644 --- a/app/views/mailer/unfeasible_spending_proposal.html.erb +++ b/app/views/mailer/unfeasible_spending_proposal.html.erb @@ -1,16 +1,17 @@ -

Estimado usuario,

+

<%= t("mailers.unfeasible_spending_proposal.hi") %>

-

Desde el Ayuntamiento de Madrid queremos agradecer tu participación en los Presupuestos Participativos de la ciudad de Madrid. -Lamentamos informarte de que tu propuesta '<%= @spending_proposal.title %>' quedará excluida de este proceso participativo por el siguiente motivo:

+

<%= t("mailers.unfeasible_spending_proposal.unfeasible", + title: @spending_proposal.title) %>

<%= @spending_proposal.feasible_explanation %>

-

Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: -<%= link_to "nueva propuesta de inversión", new_spending_proposal_url %>

+

<%= t("mailers.unfeasible_spending_proposal.new_html", + url: link_to(t("mailers.unfeasible_spending_proposal.new_href"), + new_spending_proposal_url)) %>

-

Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, al correo preparticipativos@madrid.es, indicando necesariamente para su tramitación el código <%= @spending_proposal.code %> como asunto del correo, correspondiente a tu propuesta.

+

<%= t("mailers.unfeasible_spending_proposal.reconsider") %>

-

Sentimos las molestias ocasionadas y volvemos a darte las gracias por tu inestimable participación.

+

<%= t("mailers.unfeasible_spending_proposal.sorry") %>

-

Atentamente

-

DIRECCIÓN GENERAL DE PARTICIPACIÓN CIUDADANA

\ No newline at end of file +

<%= t("mailers.unfeasible_spending_proposal.sincerely") %>

+

<%= t("mailers.unfeasible_spending_proposal.signatory") %>

\ No newline at end of file diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index e7ff3f928..53d2b7c60 100755 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -21,6 +21,12 @@ en: 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 + hi: "Dear user," + new_html: "For all these, we invite you to elaborate a new proposal that ajusts to the conditions of this process. You can do it following this link: %{url}" + new_href: "new investment project" + reconsider: "If you believe that the rejected proposal meets the requirements to be an investment proposal, you can communicate this, within 48 hours, responding to this email. Please include all relevant information to reconsider the proposal as valid." + sincerely: "Sincerely" + signatory: "DEPARTMENT OF PUBLIC PARTICIPATION" + sorry: "Sorry for the inconvenience and we again thank you for your invaluable participation." + subject: "Your investment project '%{code}' has been marked as unfeasible" + unfeasible: "From the Madrid City Council we want to thank you for your participation in the participatory budgets of the city of Madrid. We regret to inform you that your proposal '%{title}' will be excluded from this participatory process for the following reason:" \ No newline at end of file diff --git a/config/locales/mailers.es.yml b/config/locales/mailers.es.yml index b570dba46..0624fa410 100644 --- a/config/locales/mailers.es.yml +++ b/config/locales/mailers.es.yml @@ -21,6 +21,12 @@ es: 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 + hi: "Estimado usuario," + new_html: "Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: %{url}" + new_href: "nueva propuesta de inversión" + reconsider: "Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, respondiendo a este email. Por favor incluye toda la información relevante para reconsiderar la propuesta como válida." + sincerely: "Atentamente" + signatory: "DIRECCIÓN GENERAL DE PARTICIPACIÓN CIUDADANA" + sorry: "Sentimos las molestias ocasionadas y volvemos a darte las gracias por tu inestimable participación." + subject: "Tu propuesta de inversión '%{code}' ha sido marcada como inviable" + unfeasible: "Desde el Ayuntamiento de Madrid queremos agradecer tu participación en los Presupuestos Participativos de la ciudad de Madrid. Lamentamos informarte de que tu propuesta '%{title}' quedará excluida de este proceso participativo por el siguiente motivo:" From 8a8f062398c4e9db249d9dc3e634a0b741236fad Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 16:37:16 +0200 Subject: [PATCH 08/13] updates investment project code --- app/models/spending_proposal.rb | 2 +- spec/features/emails_spec.rb | 4 ++-- spec/models/spending_proposal_spec.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 81293b386..8998fed6b 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -77,7 +77,7 @@ class SpendingProposal < ActiveRecord::Base end def code - "#{id}" + (administrator.present? ? "-#{administrator.id}" : "") + "#{id}" + (administrator.present? ? "-A#{administrator.id}" : "") end end diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 3ca35a14c..378098367 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -122,7 +122,7 @@ feature 'Emails' do expect(email).to have_body_text(user_confirmation_path) end - scenario "Email on unfeasible spending proposal", :focus do + scenario "Email on unfeasible spending proposal" do spending_proposal = create(:spending_proposal) administrator = create(:administrator) valuator = create(:valuator) @@ -140,7 +140,7 @@ feature 'Emails' do spending_proposal.reload email = open_last_email - expect(email).to have_subject("Your investment project '#{spending_proposal.id}-#{spending_proposal.administrator.id}' has been marked as unfeasible") + expect(email).to have_subject("Your investment project '#{spending_proposal.id}-A#{spending_proposal.administrator.id}' has been marked as unfeasible") expect(email).to deliver_to(spending_proposal.author.email) expect(email).to have_body_text(spending_proposal.title) expect(email).to have_body_text(spending_proposal.feasible_explanation) diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index 9ca2fae94..340515f84 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -104,7 +104,7 @@ describe SpendingProposal do it "returns the administrator id when assigned" do spending_proposal.administrator = create(:administrator) - expect(spending_proposal.code).to eq("#{spending_proposal.id}-#{spending_proposal.administrator.id}") + expect(spending_proposal.code).to eq("#{spending_proposal.id}-A#{spending_proposal.administrator.id}") end end end From 467c06c3ca45d03266dba3b5a49b94b423877f0f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 18:06:43 +0200 Subject: [PATCH 09/13] 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 From 26039d490697ee539eb5bd4ca35e9b05b73d07e7 Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Mon, 28 Mar 2016 15:20:17 +0200 Subject: [PATCH 10/13] Improves styles for email --- .../unfeasible_spending_proposal.html.erb | 40 ++++++++++++++----- config/locales/mailers.en.yml | 6 +-- config/locales/mailers.es.yml | 6 +-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/views/mailer/unfeasible_spending_proposal.html.erb b/app/views/mailer/unfeasible_spending_proposal.html.erb index b89e83afa..fa4e3cf96 100644 --- a/app/views/mailer/unfeasible_spending_proposal.html.erb +++ b/app/views/mailer/unfeasible_spending_proposal.html.erb @@ -1,17 +1,35 @@ -

<%= t("mailers.unfeasible_spending_proposal.hi") %>

+ -

<%= t("mailers.unfeasible_spending_proposal.unfeasible", - title: @spending_proposal.title) %>

+

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

-

<%= @spending_proposal.feasible_explanation %>

+

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

-

<%= t("mailers.unfeasible_spending_proposal.new_html", - url: link_to(t("mailers.unfeasible_spending_proposal.new_href"), - new_spending_proposal_url)) %>

+

+ <%= @spending_proposal.feasible_explanation %> +

-

<%= t("mailers.unfeasible_spending_proposal.reconsider") %>

+

+ <%= t("mailers.unfeasible_spending_proposal.new_html", + url: link_to(t("mailers.unfeasible_spending_proposal.new_href"), + new_spending_proposal_url, style: "color: #2895F1; text-decoration: underline;")) %> +

-

<%= t("mailers.unfeasible_spending_proposal.sorry") %>

+

+ <%= t("mailers.unfeasible_spending_proposal.reconsider_html") %> +

-

<%= t("mailers.unfeasible_spending_proposal.sincerely") %>

-

<%= t("mailers.unfeasible_spending_proposal.signatory") %>

\ No newline at end of file +

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

+ +

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

+ \ No newline at end of file diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index 53d2b7c60..b13c8b194 100755 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -22,11 +22,11 @@ en: title: New response to your comment unfeasible_spending_proposal: hi: "Dear user," - new_html: "For all these, we invite you to elaborate a new proposal that ajusts to the conditions of this process. You can do it following this link: %{url}" + new_html: "For all these, we invite you to elaborate a new proposal that ajusts to the conditions of this process. You can do it following this link: %{url}." new_href: "new investment project" - reconsider: "If you believe that the rejected proposal meets the requirements to be an investment proposal, you can communicate this, within 48 hours, responding to this email. Please include all relevant information to reconsider the proposal as valid." + reconsider_html: "If you believe that the rejected proposal meets the requirements to be an investment proposal, you can communicate this, within 48 hours, responding to this email. Please include all relevant information to reconsider the proposal as valid." sincerely: "Sincerely" signatory: "DEPARTMENT OF PUBLIC PARTICIPATION" sorry: "Sorry for the inconvenience and we again thank you for your invaluable participation." subject: "Your investment project '%{code}' has been marked as unfeasible" - unfeasible: "From the Madrid City Council we want to thank you for your participation in the participatory budgets of the city of Madrid. We regret to inform you that your proposal '%{title}' will be excluded from this participatory process for the following reason:" \ No newline at end of file + unfeasible_html: "From the Madrid City Council we want to thank you for your participation in the participatory budgets of the city of Madrid. We regret to inform you that your proposal '%{title}' will be excluded from this participatory process for the following reason:" \ No newline at end of file diff --git a/config/locales/mailers.es.yml b/config/locales/mailers.es.yml index 0624fa410..cfc099643 100644 --- a/config/locales/mailers.es.yml +++ b/config/locales/mailers.es.yml @@ -22,11 +22,11 @@ es: title: Nueva respuesta a tu comentario unfeasible_spending_proposal: hi: "Estimado usuario," - new_html: "Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: %{url}" + new_html: "Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: %{url}." new_href: "nueva propuesta de inversión" - reconsider: "Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, respondiendo a este email. Por favor incluye toda la información relevante para reconsiderar la propuesta como válida." + reconsider_html: "Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, respondiendo a este email. Por favor incluye toda la información relevante para reconsiderar la propuesta como válida." sincerely: "Atentamente" signatory: "DIRECCIÓN GENERAL DE PARTICIPACIÓN CIUDADANA" sorry: "Sentimos las molestias ocasionadas y volvemos a darte las gracias por tu inestimable participación." subject: "Tu propuesta de inversión '%{code}' ha sido marcada como inviable" - unfeasible: "Desde el Ayuntamiento de Madrid queremos agradecer tu participación en los Presupuestos Participativos de la ciudad de Madrid. Lamentamos informarte de que tu propuesta '%{title}' quedará excluida de este proceso participativo por el siguiente motivo:" + unfeasible_html: "Desde el Ayuntamiento de Madrid queremos agradecer tu participación en los Presupuestos Participativos de la ciudad de Madrid. Lamentamos informarte de que tu propuesta '%{title}' quedará excluida de este proceso participativo por el siguiente motivo:" From 57299ec968399a696fffe1fe19439ccc07ae28f3 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 18:26:09 +0200 Subject: [PATCH 11/13] adds rake task to send current unfeasible spending proposals --- app/models/spending_proposal.rb | 2 +- lib/tasks/spending_proposals.rake | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/spending_proposals.rake diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index ea182b3fb..4dc776ae2 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -73,7 +73,7 @@ class SpendingProposal < ActiveRecord::Base end def unfeasible? - not feasible? + feasible == false end def valuation_finished? diff --git a/lib/tasks/spending_proposals.rake b/lib/tasks/spending_proposals.rake new file mode 100644 index 000000000..eb33eed59 --- /dev/null +++ b/lib/tasks/spending_proposals.rake @@ -0,0 +1,13 @@ +namespace :spending_proposals do + desc "Sends an email to the authors of unfeasible spending proposals" + task send_unfeasible_emails: :environment do + SpendingProposal.find_each do |spending_propsal| + if spending_propsal.marked_as_unfeasible? + spending_propsal.send_unfeasible_email + puts "email sent for proposal #{spending_propsal.title}" + else + puts "this proposal is feasible: #{spending_propsal.title}" + end + end + end +end \ No newline at end of file From 381b66abdbd635df802a825ae29e080ea988989a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 18:29:59 +0200 Subject: [PATCH 12/13] updates reply email address --- config/locales/mailers.en.yml | 2 +- config/locales/mailers.es.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index b13c8b194..9e18435dd 100755 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -24,7 +24,7 @@ en: hi: "Dear user," new_html: "For all these, we invite you to elaborate a new proposal that ajusts to the conditions of this process. You can do it following this link: %{url}." new_href: "new investment project" - reconsider_html: "If you believe that the rejected proposal meets the requirements to be an investment proposal, you can communicate this, within 48 hours, responding to this email. Please include all relevant information to reconsider the proposal as valid." + reconsider_html: "If you believe that the rejected proposal meets the requirements to be an investment proposal, you can communicate this, within 48 hours, responding to the email address preparticipativos@madrid.es. Please include all relevant information to reconsider the proposal as valid." sincerely: "Sincerely" signatory: "DEPARTMENT OF PUBLIC PARTICIPATION" sorry: "Sorry for the inconvenience and we again thank you for your invaluable participation." diff --git a/config/locales/mailers.es.yml b/config/locales/mailers.es.yml index cfc099643..2e11604a3 100644 --- a/config/locales/mailers.es.yml +++ b/config/locales/mailers.es.yml @@ -24,7 +24,7 @@ es: hi: "Estimado usuario," new_html: "Por todo ello, te invitamos a que elabores una nueva propuesta que se ajuste a las condiciones de este proceso. Esto lo puedes hacer en este enlace: %{url}." new_href: "nueva propuesta de inversión" - reconsider_html: "Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, respondiendo a este email. Por favor incluye toda la información relevante para reconsiderar la propuesta como válida." + reconsider_html: "Si consideras que la propuesta rechazada cumple los requisitos para mantenerla como propuesta de inversión, podrás comunicarlo, en el plazo de 48 horas, al correo preparticipativos@madrid.es. Por favor incluye toda la información relevante para reconsiderar la propuesta como válida." sincerely: "Atentamente" signatory: "DIRECCIÓN GENERAL DE PARTICIPACIÓN CIUDADANA" sorry: "Sentimos las molestias ocasionadas y volvemos a darte las gracias por tu inestimable participación." From cfe75932882215f742acf1e049c08f56022efaf1 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Mar 2016 18:43:56 +0200 Subject: [PATCH 13/13] updates method name to be more accurate --- .../valuation/spending_proposals_controller.rb | 2 +- app/models/spending_proposal.rb | 2 +- lib/tasks/spending_proposals.rake | 2 +- spec/models/spending_proposal_spec.rb | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/valuation/spending_proposals_controller.rb b/app/controllers/valuation/spending_proposals_controller.rb index deba1777d..364671d09 100644 --- a/app/controllers/valuation/spending_proposals_controller.rb +++ b/app/controllers/valuation/spending_proposals_controller.rb @@ -19,7 +19,7 @@ class Valuation::SpendingProposalsController < Valuation::BaseController def valuate if valid_price_params? && @spending_proposal.update(valuation_params) - if @spending_proposal.marked_as_unfeasible? + if @spending_proposal.unfeasible_email_pending? @spending_proposal.send_unfeasible_email end diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 4dc776ae2..a084a800e 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -68,7 +68,7 @@ class SpendingProposal < ActiveRecord::Base end end - def marked_as_unfeasible? + def unfeasible_email_pending? unfeasible_email_sent_at.blank? && unfeasible? && valuation_finished? end diff --git a/lib/tasks/spending_proposals.rake b/lib/tasks/spending_proposals.rake index eb33eed59..b49f2acf5 100644 --- a/lib/tasks/spending_proposals.rake +++ b/lib/tasks/spending_proposals.rake @@ -2,7 +2,7 @@ namespace :spending_proposals do desc "Sends an email to the authors of unfeasible spending proposals" task send_unfeasible_emails: :environment do SpendingProposal.find_each do |spending_propsal| - if spending_propsal.marked_as_unfeasible? + if spending_propsal.unfeasible_email_pending? spending_propsal.send_unfeasible_email puts "email sent for proposal #{spending_propsal.title}" else diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb index 6d506d587..4d320a8d7 100644 --- a/spec/models/spending_proposal_spec.rb +++ b/spec/models/spending_proposal_spec.rb @@ -72,27 +72,27 @@ describe SpendingProposal do end end - describe "#marked_as_unfeasible?" do + describe "#unfeasible_email_pending?" do let(:spending_proposal) { create(:spending_proposal) } 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 + expect(spending_proposal.unfeasible_email_pending?).to eq true end it "returns false when marked as feasible" do spending_proposal.update(feasible: true) - expect(spending_proposal.marked_as_unfeasible?).to eq false + expect(spending_proposal.unfeasible_email_pending?).to eq false end 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 + expect(spending_proposal.unfeasible_email_pending?).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 + expect(spending_proposal.unfeasible_email_pending?).to eq false end end