use the user presenter, luke

This commit is contained in:
Maxwell Salzberg 2012-02-16 13:19:14 -08:00
parent 40413a6bc3
commit 063912287c
6 changed files with 46 additions and 22 deletions

View file

@ -13,9 +13,7 @@ class ApplicationController < ActionController::Base
inflection_method :grammatical_gender => :gender inflection_method :grammatical_gender => :gender
helper_method :notification_count, helper_method :all_aspects,
:unread_message_count,
:all_aspects,
:all_contacts_count, :all_contacts_count,
:my_contacts_count, :my_contacts_count,
:only_sharing_count, :only_sharing_count,
@ -38,15 +36,6 @@ class ApplicationController < ActionController::Base
end end
end end
##helpers
def notification_count
@notification_count ||= Notification.for(current_user, :unread =>true).size
end
def unread_message_count
@unread_message_count ||= ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}")
end
def all_aspects def all_aspects
@all_aspects ||= current_user.aspects @all_aspects ||= current_user.aspects
end end

View file

@ -41,6 +41,8 @@ class NotificationsController < ApplicationController
end end
@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')) }
@unread_notification_count = current_user.unread_notifications.count
respond_to do |format| respond_to do |format|
format.html format.html
format.xml { render :xml => @notifications.to_xml } format.xml { render :xml => @notifications.to_xml }

View file

@ -36,16 +36,11 @@ module LayoutHelper
def set_current_user_in_javascript def set_current_user_in_javascript
return unless current_user return unless current_user
current_user_presenter = UserPresenter.new(current_user)
content_tag(:script) do content_tag(:script) do
<<-JS.html_safe <<-JS.html_safe
app.user( app.user(#{current_user_presenter.to_json});
_.extend(#{current_user.person.as_api_response(:backbone).to_json}, {
notifications_count : #{notification_count},
unread_messages_count : #{unread_message_count},
admin : #{current_user.admin?}
})
);
JS JS
end end
end end

View file

@ -98,6 +98,14 @@ class User < ActiveRecord::Base
where('last_sign_in_at > ?', time) where('last_sign_in_at > ?', time)
end end
def unread_notifications
notifications.where(:unread => true)
end
def unread_message_count
ConversationVisibility.sum(:unread, :conditions => "person_id = #{self.person.id}")
end
# @return [User] # @return [User]
def self.find_by_invitation(invitation) def self.find_by_invitation(invitation)
service = invitation.service service = invitation.service

View file

@ -0,0 +1,30 @@
class UserPresenter
attr_accessor :user
def initialize(user)
self.user = user
end
def to_json(options = {})
self.user.person.as_api_response(:backbone).update(
{ :notifications_count => notifications_count,
:unread_messages_count => unread_messages_count,
:admin => admin
}
).to_json(options)
end
protected
def notifications_count
@notification_count ||= user.unread_notifications.count
end
def unread_messages_count
@unread_message_count ||= user.unread_message_count
end
def admin
user.admin?
end
end

View file

@ -1,8 +1,8 @@
#notifications_content #notifications_content
.span-13 .span-13
%h2 %h2
%span.notification_count{:class => ('unread' if notification_count > 0)} %span.notification_count{:class => ('unread' if @unread_notification_count >0 )}
= notification_count = @unread_notification_count
= t('.notifications') = t('.notifications')
.span-8.last .span-8.last
= link_to t('.mark_all_as_read'), notifications_read_all_path, :class => 'button' = link_to t('.mark_all_as_read'), notifications_read_all_path, :class => 'button'