From 60c0a9839b55111982be7ab00da6e58c34c570bc Mon Sep 17 00:00:00 2001
From: Ilya Zhitomirskiy <ilya@laptop.(none)>
Date: Tue, 17 May 2011 18:19:05 -0700
Subject: [PATCH] added a podwide email preference

---
 app/mailers/notifier.rb        |  6 ++++--
 app/models/user_preference.rb  |  3 ++-
 app/views/users/edit.html.haml |  4 ++++
 config/locales/diaspora/en.yml |  1 +
 spec/mailers/notifier_spec.rb  | 14 ++++++++++++++
 5 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb
index c7b10bcf3c..43f51d159e 100644
--- a/app/mailers/notifier.rb
+++ b/app/mailers/notifier.rb
@@ -7,8 +7,10 @@ class Notifier < ActionMailer::Base
   def self.admin(string, recipients, opts = {})
     mails = []
     recipients.each do |rec|
-      mail = single_admin(string, rec)
-      mails << mail
+      if !rec.user_preferences.exists?(:email_type => "podwide")
+        mail = single_admin(string, rec)
+        mails << mail
+      end
     end
     mails
   end
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb
index 4689186212..fd54218ace 100644
--- a/app/models/user_preference.rb
+++ b/app/models/user_preference.rb
@@ -9,7 +9,8 @@ class UserPreference < ActiveRecord::Base
    "private_message",
    "started_sharing",
    "also_commented",
-   "liked"]
+   "liked",
+   "podwide"]
 
   def must_be_valid_email_type
     unless VALID_EMAIL_TYPES.include?(self.email_type)
diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml
index 54f9608173..2cdb6862ed 100644
--- a/app/views/users/edit.html.haml
+++ b/app/views/users/edit.html.haml
@@ -105,6 +105,10 @@
           = type.label t('.liked') 
           = type.check_box :liked, {:checked =>  @email_prefs['liked']}, false, true 
 
+        %br
+        %p.checkbox_select
+          = type.label t('.podwide') 
+          = type.check_box :podwide, {:checked =>  @email_prefs['podwide']}, false, true 
     %br
     = f.submit t('.change')
 
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index b11da4ab0c..f99f6df67c 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -627,6 +627,7 @@ en:
       started_sharing: "...someone starts sharing with you?"
       private_message: "...you receive a private message?"
       liked: "...someone likes your post?"
+      podwide: "...pod-wide news happen [new features, etc.]?"
       change: "Change"
     destroy: "Account successfully closed."
     getting_started:
diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb
index 40c062067e..54083e21f3 100644
--- a/spec/mailers/notifier_spec.rb
+++ b/spec/mailers/notifier_spec.rb
@@ -12,6 +12,7 @@ describe Notifier do
   before do
     Notifier.deliveries = []
   end
+
   describe '.administrative' do
     it 'mails a user' do
       mails = Notifier.admin("Welcome to bureaucracy!", [user])
@@ -21,6 +22,7 @@ describe Notifier do
       mail.body.encoded.should match /Welcome to bureaucracy!/
       mail.body.encoded.should match /#{user.username}/
     end
+
     it 'mails a bunch of users' do
       users = []
       5.times do
@@ -34,6 +36,18 @@ describe Notifier do
         mail.body.encoded.should match /#{this_user.username}/
       }
     end
+
+    it 'does not email the users who opted out of podwide emails' do
+      users = []
+      5.times do
+        users << Factory.create(:user)
+      end
+      2.times do |n|
+        users[n].user_preferences.create(:email_type => 'podwide')
+      end
+      mails = Notifier.admin("Welcome to bureaucracy!", users)
+      mails.length.should == 3
+    end
   end
 
   describe '.single_admin' do
-- 
GitLab