proof of concept for notifications overlay

This commit is contained in:
Michael Sofaer 2011-05-29 13:16:03 -07:00 committed by Michael Sofaer & Raphael Sofaer
parent ef7580a698
commit d6ba3c970c
8 changed files with 70 additions and 22 deletions

View file

@ -3,19 +3,20 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class NotificationsController < VannaController class NotificationsController < VannaController
include NotificationsHelper
def update def update(opts=params)
note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first note = Notification.where(:recipient_id => current_user.id, :id => opts[:id]).first
if note if note
note.update_attributes(:unread => false) note.update_attributes(:unread => false)
render :nothing => true {}
else else
render :nothing => true, :status => 404 Response.new :status => 404
end end
end end
def index def index(opts=params)
@aspect = :notification @aspect = :notification
conditions = {:recipient_id => current_user.id} conditions = {:recipient_id => current_user.id}
page = params[:page] || 1 page = params[:page] || 1
@ -31,12 +32,17 @@ class NotificationsController < VannaController
pager.replace(result) pager.replace(result)
end end
notifications.each{|n| n[:actors] = n.actors}
group_days = notifications.group_by{|note| I18n.l(note.created_at, :format => I18n.t('date.formats.fullmonth_day')) } group_days = notifications.group_by{|note| I18n.l(note.created_at, :format => I18n.t('date.formats.fullmonth_day')) }
{:group_days => group_days, :current_user => current_user, :notifications => notifications} {:group_days => group_days, :notifications => notifications}
end end
def read_all def read_all(opts=params)
Notification.where(:recipient_id => current_user.id).update_all(:unread => false) Notification.where(:recipient_id => current_user.id).update_all(:unread => false)
redirect_to aspects_path end
post_process :html do
def post_read_all
Response.new(:status => 302, :location => aspects_path)
end
end end
end end

View file

@ -53,7 +53,6 @@ module NotificationsHelper
number_of_actors = actors.count number_of_actors = actors.count
sentence_translations = {:two_words_connector => " #{t('notifications.index.and')} ", :last_word_connector => ", #{t('notifications.index.and')} " } sentence_translations = {:two_words_connector => " #{t('notifications.index.and')} ", :last_word_connector => ", #{t('notifications.index.and')} " }
actor_links = actors.collect{ |person| link_to("#{h(person.name.titlecase.strip)}", person_path(person))} actor_links = actors.collect{ |person| link_to("#{h(person.name.titlecase.strip)}", person_path(person))}
if number_of_actors < 4 if number_of_actors < 4
message = actor_links.to_sentence(sentence_translations) message = actor_links.to_sentence(sentence_translations)
else else

View file

@ -24,7 +24,7 @@
- if @notification_count - if @notification_count
#notification_badge #notification_badge
= link_to "", notifications_path, :title => new_notification_text(@notification_count) = link_to "", notifications_path, :title => new_notification_text(@notification_count)
= image_tag 'icons/monotone_flag.png', :height => 20, :width => 20 = image_tag 'icons/monotone_flag.png', :height => 20, :width => 20, :id => "notification-flag"
.badge_count{:class => ("hidden" if @notification_count == 0)} .badge_count{:class => ("hidden" if @notification_count == 0)}
= @notification_count = @notification_count
#message_inbox_badge #message_inbox_badge

View file

@ -95,6 +95,7 @@
= yield = yield
.clearfix .clearfix
=render :partial => 'notifications/overlay'
/=render :partial => 'layouts/debug.haml' /=render :partial => 'layouts/debug.haml'
%footer %footer

View file

@ -0,0 +1,15 @@
.span-24.last#notifications_overlay
.stream.notifications
.day_group.span-24.last
.span-3
.date
.day
.month
.span-8.notifications_for_day
.stream_element{:data=>{:guid => nil}}
.right
%span.from
= link_to("", "#", :class => "actor")
= link_to("", "#", :class => "object")
%br

View file

@ -27,7 +27,6 @@
%span.from %span.from
= notification_message_for(note) = notification_message_for(note)
%br %br
%time= timeago(note.created_at) %time= timeago(note.created_at)
.span-13.last .span-13.last

View file

@ -34,7 +34,32 @@
if(typeof this.collection[widgetId].start !== "undefined") { if(typeof this.collection[widgetId].start !== "undefined") {
this.collection[widgetId].start(); this.collection[widgetId].start();
} }
};
$("#notification_badge a").click(function(event){
event.preventDefault();
$.ajax({
"url":"/notifications",
"success":function(data){
$("#notifications_overlay").show();
var hash = eval("(" + data + ")");
$.each(hash["group_days"], function(day){
$("#notifications_overlay .month").text(day.split(" ")[0])
$("#notifications_overlay .day").text(day.split(" ")[1])
var notifications_for_day = hash["group_days"][day]
console.log(notifications_for_day);
$.each(notifications_for_day, function(i, notification_hash){
$.each(notification_hash, function(notification_type, notification){
console.log(notification_type);
console.log(notification);
$("#notifications_overlay .actor").
text(notification["actors"][0]["name"]).
attr("href", notification["actors"][0]["url"]);
});
});
})
} }
});
});
}; };
Diaspora.WidgetCollection.prototype.subscribe = function(id, callback, context) { Diaspora.WidgetCollection.prototype.subscribe = function(id, callback, context) {

View file

@ -5,16 +5,18 @@
require 'spec_helper' require 'spec_helper'
describe NotificationsController do describe NotificationsController do
render_views(false)
before do before do
@user = alice @user = alice
@aspect = @user.aspects.first @aspect = @user.aspects.first
sign_in :user, @user @controller = NotificationsController.new
@controller.stub!(:current_user).and_return(@user)
end end
describe '#update' do describe '#update' do
it 'marks a notification as read' do it 'marks a notification as read' do
note = Factory(:notification, :recipient => @user) note = Factory(:notification, :recipient => @user)
put :update, :id => note.id @controller.update :id => note.id
Notification.first.unread.should == false Notification.first.unread.should == false
end end
@ -24,7 +26,7 @@ describe NotificationsController do
Factory(:notification, :recipient => @user) Factory(:notification, :recipient => @user)
note = Factory(:notification, :recipient => user2) note = Factory(:notification, :recipient => user2)
put :update, :id => note.id @controller.update :id => note.id
Notification.find(note.id).unread.should == true Notification.find(note.id).unread.should == true
end end
@ -37,7 +39,7 @@ describe NotificationsController do
Factory(:notification, :recipient => @user) Factory(:notification, :recipient => @user)
Notification.where(:unread => true).count.should == 2 Notification.where(:unread => true).count.should == 2
get :read_all @controller.read_all({})
Notification.where(:unread => true).count.should == 0 Notification.where(:unread => true).count.should == 0
end end
end end
@ -50,11 +52,12 @@ describe NotificationsController do
end end
it 'paginates the notifications' do it 'paginates the notifications' do
get :index @controller.index({})[:notifications].count.should == 25
assigns[:notifications].count.should == 25 @controller.index(:page => 2)[:notifications].count.should == 1
end
get :index, :page => 2 it "includes the actors" do
assigns[:notifications].count.should == 1 notification = Factory(:notification, :recipient => @user)
@controller.index({})[:notifications].first[:actors].should == notification.actors
end end
it 'eager loads the target' do it 'eager loads the target' do