From ad697cd2c190b2079b77f769783ac7c1bb75cb8d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 6 Jun 2016 12:55:33 +0200 Subject: [PATCH] adds preference to receive proposal notifications --- app/controllers/account_controller.rb | 2 +- .../proposal_notifications_controller.rb | 4 +- app/views/account/show.html.erb | 25 +++++-- config/locales/en.yml | 1 + config/locales/es.yml | 1 + ...email_on_proposal_notification_to_users.rb | 5 ++ db/schema.rb | 35 +++++----- spec/features/account_spec.rb | 11 +-- spec/features/emails_spec.rb | 68 ++++++++++++------- spec/models/user_spec.rb | 6 ++ 10 files changed, 107 insertions(+), 51 deletions(-) create mode 100644 db/migrate/20160606102427_add_email_on_proposal_notification_to_users.rb diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 9a0f89bae..d1ac9c9c4 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -25,7 +25,7 @@ class AccountController < ApplicationController if @account.organization? params.require(:account).permit(:phone_number, :email_on_comment, :email_on_comment_reply, :newsletter, organization_attributes: [:name, :responsible_name]) else - params.require(:account).permit(:username, :public_activity, :email_on_comment, :email_on_comment_reply, :newsletter) + params.require(:account).permit(:username, :public_activity, :email_on_comment, :email_on_comment_reply, :email_on_proposal_notification, :newsletter) end end diff --git a/app/controllers/proposal_notifications_controller.rb b/app/controllers/proposal_notifications_controller.rb index fe6add7e2..f1249c707 100644 --- a/app/controllers/proposal_notifications_controller.rb +++ b/app/controllers/proposal_notifications_controller.rb @@ -11,7 +11,9 @@ class ProposalNotificationsController < ApplicationController @proposal = Proposal.find(notification_params[:proposal_id]) if @notification.save @proposal.voters.each do |voter| - Mailer.proposal_notification(@notification, voter).deliver_later + if voter.email_on_proposal_notification? + Mailer.proposal_notification(@notification, voter).deliver_later + end end redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification") else diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb index f0d2b6abc..b3a84dede 100644 --- a/app/views/account/show.html.erb +++ b/app/views/account/show.html.erb @@ -34,7 +34,9 @@
<%= f.label :public_activity do %> <%= f.check_box :public_activity, title: t('account.show.public_activity_label'), label: false %> - <%= t("account.show.public_activity_label") %> + + <%= t("account.show.public_activity_label") %> + <% end %>
@@ -43,21 +45,36 @@
<%= f.label :email_on_comment do %> <%= f.check_box :email_on_comment, title: t('account.show.email_on_comment_label'), label: false %> - <%= t("account.show.email_on_comment_label") %> + + <%= t("account.show.email_on_comment_label") %> + <% end %>
<%= f.label :email_on_comment_reply do %> <%= f.check_box :email_on_comment_reply, title: t('account.show.email_on_comment_reply_label'), label: false %> - <%= t("account.show.email_on_comment_reply_label") %> + + <%= t("account.show.email_on_comment_reply_label") %> + <% end %>
<%= f.label :email_newsletter_subscribed do %> <%= f.check_box :newsletter, title: t('account.show.subscription_to_website_newsletter_label'), label: false %> - <%= t("account.show.subscription_to_website_newsletter_label") %> + + <%= t("account.show.subscription_to_website_newsletter_label") %> + + <% end %> +
+ +
+ <%= f.label :email_on_proposal_notification do %> + <%= f.check_box :email_on_proposal_notification, title: t('account.show.email_on_proposal_notification_label'), label: false %> + + <%= t("account.show.email_on_proposal_notification_label") %> + <% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 40b32f712..5a0dec0c6 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,6 +15,7 @@ en: public_activity_label: Keep my list of activities public save_changes_submit: Save changes subscription_to_website_newsletter_label: Receive by email website relevant information + email_on_proposal_notification_label: Receive by email notifications about proposals title: My account user_permission_debates: Participate on debates user_permission_info: With your account you can... diff --git a/config/locales/es.yml b/config/locales/es.yml index 5dc388853..909e20bbc 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -15,6 +15,7 @@ es: public_activity_label: Mostrar públicamente mi lista de actividades save_changes_submit: Guardar cambios subscription_to_website_newsletter_label: Recibir emails con información interesante sobre la web + email_on_proposal_notification_label: Recibir emails con notificaciones sobre propuestas title: Mi cuenta user_permission_debates: Participar en debates user_permission_info: Con tu cuenta ya puedes... diff --git a/db/migrate/20160606102427_add_email_on_proposal_notification_to_users.rb b/db/migrate/20160606102427_add_email_on_proposal_notification_to_users.rb new file mode 100644 index 000000000..84753eb50 --- /dev/null +++ b/db/migrate/20160606102427_add_email_on_proposal_notification_to_users.rb @@ -0,0 +1,5 @@ +class AddEmailOnProposalNotificationToUsers < ActiveRecord::Migration + def change + add_column :users, :email_on_proposal_notification, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 91628725a..a8e09434e 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: 20160601103338) do +ActiveRecord::Schema.define(version: 20160606102427) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -402,30 +402,30 @@ ActiveRecord::Schema.define(version: 20160601103338) do add_index "tolk_translations", ["phrase_id", "locale_id"], name: "index_tolk_translations_on_phrase_id_and_locale_id", unique: true, using: :btree create_table "users", force: :cascade do |t| - t.string "email", default: "" - t.string "encrypted_password", default: "", null: false + t.string "email", default: "" + t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" - t.boolean "email_on_comment", default: false - t.boolean "email_on_comment_reply", default: false - t.string "phone_number", limit: 30 + t.boolean "email_on_comment", default: false + t.boolean "email_on_comment_reply", default: false + t.string "phone_number", limit: 30 t.string "official_position" - t.integer "official_level", default: 0 + t.integer "official_level", default: 0 t.datetime "hidden_at" t.string "sms_confirmation_code" - t.string "username", limit: 60 + t.string "username", limit: 60 t.string "document_number" t.string "document_type" t.datetime "residence_verified_at" @@ -436,20 +436,21 @@ ActiveRecord::Schema.define(version: 20160601103338) do t.datetime "letter_requested_at" t.datetime "confirmed_hide_at" t.string "letter_verification_code" - t.integer "failed_census_calls_count", default: 0 + t.integer "failed_census_calls_count", default: 0 t.datetime "level_two_verified_at" t.string "erase_reason" t.datetime "erased_at" - t.boolean "public_activity", default: true - t.boolean "newsletter", default: true - t.integer "notifications_count", default: 0 - t.boolean "registering_with_oauth", default: false + t.boolean "public_activity", default: true + t.boolean "newsletter", default: true + t.integer "notifications_count", default: 0 + t.boolean "registering_with_oauth", default: false t.string "locale" t.string "oauth_email" t.integer "geozone_id" t.string "redeemable_code" - t.string "gender", limit: 10 + t.string "gender", limit: 10 t.datetime "date_of_birth" + t.boolean "email_on_proposal_notification", default: true end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index bc9c38dae..f5ba747fb 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -35,6 +35,7 @@ feature 'Account' do fill_in 'account_username', with: 'Larry Bird' check 'account_email_on_comment' check 'account_email_on_comment_reply' + uncheck 'account_email_on_proposal_notification' click_button 'Save changes' expect(page).to have_content "Changes saved" @@ -42,8 +43,9 @@ feature 'Account' do visit account_path expect(page).to have_selector("input[value='Larry Bird']") - expect(page).to have_selector("input[id='account_email_on_comment'][value='1']") - expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']") + expect(find("#account_email_on_comment")).to be_checked + expect(find("#account_email_on_comment_reply")).to be_checked + expect(find("#account_email_on_proposal_notification")).to_not be_checked end scenario 'Edit Organization' do @@ -53,6 +55,7 @@ feature 'Account' do fill_in 'account_organization_attributes_name', with: 'Google' check 'account_email_on_comment' check 'account_email_on_comment_reply' + click_button 'Save changes' expect(page).to have_content "Changes saved" @@ -60,8 +63,8 @@ feature 'Account' do visit account_path expect(page).to have_selector("input[value='Google']") - expect(page).to have_selector("input[id='account_email_on_comment'][value='1']") - expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']") + expect(find("#account_email_on_comment")).to be_checked + expect(find("#account_email_on_comment_reply")).to be_checked end scenario "Errors on edit" do diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb index 81e6560ff..e61403635 100644 --- a/spec/features/emails_spec.rb +++ b/spec/features/emails_spec.rb @@ -148,40 +148,60 @@ feature 'Emails' do expect(email).to have_body_text(spending_proposal.feasible_explanation) end - scenario "Proposal notification" do - author = create(:user) + context "Proposal notifications" do - noelia = create(:user) - vega = create(:user) - cristina = create(:user) + scenario "Proposal notification" do + author = create(:user) - proposal = create(:proposal, author: author) + noelia = create(:user) + vega = create(:user) + cristina = create(:user) - create(:vote, voter: noelia, votable: proposal, vote_flag: true) - create(:vote, voter: vega, votable: proposal, vote_flag: true) + proposal = create(:proposal, author: author) - reset_mailer + create(:vote, voter: noelia, votable: proposal, vote_flag: true) + create(:vote, voter: vega, votable: proposal, vote_flag: true) - login_as(author) - visit root_path + reset_mailer - visit new_proposal_notification_path(proposal_id: proposal.id) + login_as(author) + visit root_path - fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal" - fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!" - click_button "Send" + visit new_proposal_notification_path(proposal_id: proposal.id) - expect(page).to have_content "Your message has been sent correctly." + fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal" + fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!" + click_button "Send message" - expect(unread_emails_for(noelia.email).size).to eql parse_email_count(1) - expect(unread_emails_for(vega.email).size).to eql parse_email_count(1) - expect(unread_emails_for(cristina.email).size).to eql parse_email_count(0) - expect(unread_emails_for(author.email).size).to eql parse_email_count(0) + expect(page).to have_content "Your message has been sent correctly." - email = open_last_email - expect(email).to have_subject("Thank you for supporting my proposal") - expect(email).to have_body_text("Please share it with others so we can make it happen!") - expect(email).to have_body_text(proposal.title) + expect(unread_emails_for(noelia.email).size).to eql parse_email_count(1) + expect(unread_emails_for(vega.email).size).to eql parse_email_count(1) + expect(unread_emails_for(cristina.email).size).to eql parse_email_count(0) + expect(unread_emails_for(author.email).size).to eql parse_email_count(0) + + email = open_last_email + expect(email).to have_subject("Thank you for supporting my proposal") + expect(email).to have_body_text("Please share it with others so we can make it happen!") + expect(email).to have_body_text(proposal.title) + end + + scenario "Do not send email about proposal notifications unless set in preferences", :js do + author = create(:user) + voter = create(:user, email_on_proposal_notification: false) + + proposal = create(:proposal) + create(:vote, voter: voter, votable: proposal, vote_flag: true) + + login_as(author) + visit new_proposal_notification_path(proposal_id: proposal.id) + + fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal" + fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!" + click_button "Send message" + + expect { open_last_email }.to raise_error "No email has been sent!" + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 054e043f9..ca46ff68f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -83,6 +83,12 @@ describe User do expect(subject.newsletter).to eq(true) end end + + describe 'email_on_proposal_notification' do + it 'should be true by default' do + expect(subject.email_on_proposal_notification).to eq(true) + end + end end describe "administrator?" do