From 91acf9f878cef1fc25c9f157c5b477fbca79644d Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 27 Mar 2013 12:17:46 +0000 Subject: [PATCH] Only send email notifications to members who want them. --- app/models/notification.rb | 4 ++- ...20130327120024_add_send_email_to_member.rb | 5 +++ db/schema.rb | 35 ++++++++----------- spec/factories/member.rb | 4 +++ spec/factories/notifications.rb | 4 +++ spec/models/notification_spec.rb | 6 ++++ 6 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20130327120024_add_send_email_to_member.rb diff --git a/app/models/notification.rb b/app/models/notification.rb index 9e61cd1ca..1b53e36dc 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -16,7 +16,9 @@ class Notification < ActiveRecord::Base end def send_email - Notifier.notify(self).deliver + if self.recipient.send_notification_email + Notifier.notify(self).deliver + end end end diff --git a/db/migrate/20130327120024_add_send_email_to_member.rb b/db/migrate/20130327120024_add_send_email_to_member.rb new file mode 100644 index 000000000..b52c5fdb2 --- /dev/null +++ b/db/migrate/20130327120024_add_send_email_to_member.rb @@ -0,0 +1,5 @@ +class AddSendEmailToMember < ActiveRecord::Migration + def change + add_column :members, :send_notification_email, :boolean, :default => true + end +end diff --git a/db/schema.rb b/db/schema.rb index e4c42cd30..ee8cd192a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130326092227) do +ActiveRecord::Schema.define(:version => 20130327120024) do create_table "comments", :force => true do |t| t.integer "post_id", :null => false @@ -45,7 +45,7 @@ ActiveRecord::Schema.define(:version => 20130326092227) do create_table "gardens", :force => true do |t| t.string "name", :null => false - t.integer "owner_id" + t.integer "owner_id", :null => false t.string "slug", :null => false t.datetime "created_at", :null => false t.datetime "updated_at", :null => false @@ -56,12 +56,12 @@ ActiveRecord::Schema.define(:version => 20130326092227) do add_index "gardens", ["slug"], :name => "index_gardens_on_slug", :unique => true create_table "members", :force => true do |t| - t.string "email", :default => "", :null => false - t.string "encrypted_password", :default => "", :null => false + t.string "email", :default => "", :null => false + 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 + t.integer "sign_in_count", :default => 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" @@ -70,11 +70,11 @@ ActiveRecord::Schema.define(:version => 20130326092227) do t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" - t.integer "failed_attempts", :default => 0 + t.integer "failed_attempts", :default => 0 t.string "unlock_token" t.datetime "locked_at" - 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 "login_name" t.string "slug" t.boolean "tos_agreement" @@ -82,6 +82,7 @@ ActiveRecord::Schema.define(:version => 20130326092227) do t.string "location" t.float "latitude" t.float "longitude" + t.boolean "send_notification_email", :default => true end add_index "members", ["confirmation_token"], :name => "index_users_on_confirmation_token", :unique => true @@ -97,22 +98,14 @@ ActiveRecord::Schema.define(:version => 20130326092227) do create_table "notifications", :force => true do |t| t.integer "sender_id" - t.integer "recipient_id", :null => false + t.integer "recipient_id", :null => false t.string "subject" t.text "body" - t.boolean "read", :default => false + t.boolean "read", :default => false + t.integer "notification_type" t.integer "post_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - - create_table "payments", :force => true do |t| - t.integer "payer_id" - t.decimal "amount" - t.date "paid_period_begins" - t.date "paid_period_ends" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "plantings", :force => true do |t| diff --git a/spec/factories/member.rb b/spec/factories/member.rb index 829bf0b7c..762a7f631 100644 --- a/spec/factories/member.rb +++ b/spec/factories/member.rb @@ -72,6 +72,10 @@ FactoryGirl.define do login_name 'abc_123' end + factory :no_email_notifications_member do + send_notification_email false + end + end end diff --git a/spec/factories/notifications.rb b/spec/factories/notifications.rb index aa26b4145..e0e4fc788 100644 --- a/spec/factories/notifications.rb +++ b/spec/factories/notifications.rb @@ -8,5 +8,9 @@ FactoryGirl.define do body "MyText" read false post + + factory :no_email_notification do + recipient { FactoryGirl.create(:no_email_notifications_member) } + end end end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 720f905d3..8ebaba010 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -37,6 +37,12 @@ describe Notification do ActionMailer::Base.deliveries.last.to.should == [@notification2.recipient.email] end + it "doesn't send email to people who don't want it" do + @notification = FactoryGirl.create(:no_email_notification) + @notification.send_email + ActionMailer::Base.deliveries.last.to.should_not == [@notification.recipient.email] + end + it "sends email on creation" do @notification2 = FactoryGirl.create(:notification) ActionMailer::Base.deliveries.last.to.should == [@notification2.recipient.email]