Commit dd111d3a

mo khan <mo@mokhan.ca>
2014-11-02 04:16:32
manually notifiy user when a new activity has been created and recreate all activities.
1 parent 90db48f
app/mailers/notification_mailer.rb
@@ -1,9 +1,8 @@
 class NotificationMailer < ActionMailer::Base
   default from: "noreply@cakeside.com"
 
-  def notification_email(activity)
-    @user = activity.user
-    @activity = activity
+  def notification_email(user)
+    @user = user
     mail(to: @user.email, subject: "New Activity on CakeSide")
   end
 end
app/models/activity.rb
@@ -1,9 +1,4 @@
 class Activity < ActiveRecord::Base
-  belongs_to :subject, polymorphic: true
-  belongs_to :user
-  after_create :send_notification_email
-
-  def send_notification_email
-    NotificationMailer.delay.notification_email(self)
-  end
+  belongs_to :subject, polymorphic: true # favorite, comment
+  belongs_to :user # user to notify
 end
app/models/comment.rb
@@ -7,6 +7,9 @@ class Comment < ActiveRecord::Base
   private
 
   def create_activity
-    Activity.create(user: user, subject: self)
+    transaction do
+      Activity.create(user: creation.author, subject: self)
+      creation.author.notify_of_activity
+    end
   end
 end
app/models/favorite.rb
@@ -4,6 +4,9 @@ class Favorite < ActiveRecord::Base
   after_create :create_activity
 
   def create_activity
-    Activity.create(user: creation.author, subject: self)
+    transaction do
+      Activity.create(user: creation.author, subject: self)
+      creation.author.notify_of_activity
+    end
   end
 end
app/models/user.rb
@@ -65,6 +65,10 @@ class User < ActiveRecord::Base
     creations.create(name: name, category_id: category.id)
   end
 
+  def notify_of_activity
+    NotificationMailer.delay.notification_email(self)
+  end
+
   class << self
     def login(username, password)
       user = User.find_by(email: username)
app/views/notification_mailer/notification_email.text.erb
@@ -1,4 +1,3 @@
-
 Hi <%= @user.name %>,
 
 You have new notification on Cakeside. Click <%= link_to "here", my_dashboard_url(protocol: 'https') %> to see.
db/migrate/20141102040612_recreate_all_activities.rb
@@ -0,0 +1,13 @@
+class RecreateAllActivities < ActiveRecord::Migration
+  def change
+    ActiveRecord::Base.transaction do
+      Activity.destroy_all
+      Favorite.find_each do |favorite|
+        Activity.create(user: favorite.creation.author, subject: favorite, created_at: favorite.created_at, updated_at: favorite.updated_at)
+      end
+      Comment.find_each do |comment|
+        Activity.create(user: comment.creation.author, subject: comment, created_at: comment.created_at, updated_at: comment.updated_at)
+      end
+    end
+  end
+end
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: 20141020045107) do
+ActiveRecord::Schema.define(version: 20141102040612) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -31,8 +31,8 @@ ActiveRecord::Schema.define(version: 20141020045107) do
 
   create_table "avatars", force: true do |t|
     t.integer  "user_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",        null: false
+    t.datetime "updated_at",        null: false
     t.string   "avatar"
     t.boolean  "avatar_processing"
     t.string   "avatar_tmp"
@@ -85,8 +85,8 @@ ActiveRecord::Schema.define(version: 20141020045107) do
     t.datetime "failed_at"
     t.string   "locked_by"
     t.string   "queue"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",             null: false
+    t.datetime "updated_at",             null: false
   end
 
   add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
@@ -103,8 +103,8 @@ ActiveRecord::Schema.define(version: 20141020045107) do
 
   create_table "interests", force: true do |t|
     t.string   "name"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
   end
 
   create_table "locations", id: :uuid, default: "uuid_generate_v4()", force: true do |t|
@@ -157,15 +157,13 @@ ActiveRecord::Schema.define(version: 20141020045107) do
     t.integer "taggings_count", default: 0
   end
 
-  add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
-
   create_table "tutorials", force: true do |t|
     t.string   "heading"
     t.text     "description"
     t.string   "url"
     t.integer  "user_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",  null: false
+    t.datetime "updated_at",  null: false
     t.string   "image_url"
     t.string   "author"
     t.string   "author_url"
@@ -190,8 +188,8 @@ ActiveRecord::Schema.define(version: 20141020045107) do
   add_index "user_sessions", ["user_id"], name: "index_user_sessions_on_user_id", using: :btree
 
   create_table "users", force: true do |t|
-    t.string   "email",                  default: "", null: false
-    t.string   "password_digest",        default: "", null: false
+    t.string   "email",                              default: "", null: false
+    t.string   "password_digest",        limit: 128, default: "", null: false
     t.string   "reset_password_token"
     t.datetime "reset_password_sent_at"
     t.datetime "created_at"
@@ -203,7 +201,7 @@ ActiveRecord::Schema.define(version: 20141020045107) do
     t.string   "city"
     t.string   "authentication_token"
     t.string   "full_address"
-    t.integer  "creations_count",        default: 0
+    t.integer  "creations_count",                    default: 0
     t.boolean  "admin"
   end
 
spec/mailers/previews/notification_mailer_preview.rb
@@ -1,5 +1,5 @@
 class NotificationMailerPreview < ActionMailer::Preview
   def notification_email
-    NotificationMailer.notification_email(Activity.last)
+    NotificationMailer.notification_email(User.last)
   end
 end
spec/mailers/notification_mailer_spec.rb
@@ -2,15 +2,15 @@ require "rails_helper"
 
 describe NotificationMailer do
   context "send welcome email" do
-    let(:activity) { build(:activity) }
-    let(:mail) { NotificationMailer.notification_email(activity) }
+    let(:user) { build(:user) }
+    let(:mail) { NotificationMailer.notification_email(user) }
 
     it "adds a subject" do
       mail.subject.should == "New Activity on CakeSide"
     end
 
     it "sends to the users email" do
-      mail.to.should include activity.user.email
+      mail.to.should include user.email
     end
 
     it "should send from the correct address" do
@@ -18,7 +18,7 @@ describe NotificationMailer do
     end
 
     it "includes their name" do
-      mail.body.encoded.should match(activity.user.name)
+      mail.body.encoded.should match(user.name)
     end
   end
 end
spec/models/favorite_spec.rb
@@ -7,7 +7,7 @@ describe Favorite do
     let(:user) { create(:user) }
 
     it "creates a new activity" do
-      creation.favorites.create(:user => user)
+      user.favorites.create(creation: creation)
       creation.author.activities.count.should == 1
     end
   end