From c749d81fe9a8ac8e8f27839b159c57c81a8763f3 Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Fri, 17 Dec 2010 15:24:49 -0800
Subject: [PATCH] putting a link to notifications, make them unread in the
 controller when you view the index

---
 app/controllers/application_controller.rb   | 2 +-
 app/controllers/notifications_controller.rb | 4 ++++
 app/models/notification.rb                  | 5 +++--
 app/views/layouts/_header.html.haml         | 1 +
 public/stylesheets/sass/application.sass    | 3 ++-
 5 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 6ead92dec8..dcf04921c2 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base
       @aspect = nil
       @aspects = current_user.aspects.fields(:name)
       @aspects_dropdown_array = @aspects.collect{|x| [x.to_s, x.id]}
-      @notifications = Notification.for(current_user).all
+      @notification_count = Notification.for(current_user, :unread =>true).all.count
     end
   end
 
diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index 0db3bdbf54..c792930477 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -4,6 +4,7 @@
 
 class NotificationsController < ApplicationController
   before_filter :authenticate_user!
+  respond_to :html
 
   def destroy
     note = Notification.find_by_user_id_and_id(current_user.id, params[:id])
@@ -16,6 +17,9 @@ class NotificationsController < ApplicationController
   end
 
   def index
+    @notifications = Notification.for(current_user)
     @group_days = @notifications.group_by{|note| note.created_at.strftime("%B %d") } 
+    respond_with @notifications
+    @notifications.find_all{|x| x.unread ==true}.each{|x| x.update_attributes(:unread => false)}
   end
 end
diff --git a/app/models/notification.rb b/app/models/notification.rb
index ab77b24b86..4068da5dd7 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -7,16 +7,17 @@ class Notification
 
   key :object_id, ObjectId
   key :kind, String
+  key :unread, Boolean, :default => true
 
   belongs_to :user
   belongs_to :person
 
   timestamps!
 
-  attr_accessible :object_id, :kind, :user_id, :person_id
+  attr_accessible :object_id, :kind, :user_id, :person_id, :unread
 
   def self.for(user, opts={})
-    self.where(opts.merge(:user_id => user.id)).order('created_at desc')
+    self.where(opts.merge!(:user_id => user.id)).order('created_at desc')
   end
 
   def self.notify(user, object, person)
diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml
index 5055c4013a..e9722107bb 100644
--- a/app/views/layouts/_header.html.haml
+++ b/app/views/layouts/_header.html.haml
@@ -44,6 +44,7 @@
               = link_for_aspect aspect
           %li
             = link_to '+', '#add_aspect_pane', :class => "add_aspect_button", :title => t('aspects.manage.add_a_new_aspect')
+            = link_to "notifications (#{@notification_count})", notifications_path
 
         .fancybox_content
           #add_aspect_pane
diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass
index 9c678e2004..1dc27f2403 100644
--- a/public/stylesheets/sass/application.sass
+++ b/public/stylesheets/sass/application.sass
@@ -139,7 +139,8 @@ header
     .right
       :top 10px
     :height 45px
-
+    
+  ul#notifications,
   ul#user_menu
     :overflow hidden
     :white-space nowrap
-- 
GitLab