From 063912287ce7af7cc7b20ecddfbca9f2553bb1b6 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg <maxwell@joindiaspora.com> Date: Thu, 16 Feb 2012 13:19:14 -0800 Subject: [PATCH] use the user presenter, luke --- app/controllers/application_controller.rb | 13 +-------- app/controllers/notifications_controller.rb | 2 ++ app/helpers/layout_helper.rb | 11 +++----- app/models/user.rb | 8 ++++++ app/presenters/user_presenter.rb | 30 +++++++++++++++++++++ app/views/notifications/index.html.haml | 4 +-- 6 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 app/presenters/user_presenter.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5f89717c90..cb34700f10 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,9 +13,7 @@ class ApplicationController < ActionController::Base inflection_method :grammatical_gender => :gender - helper_method :notification_count, - :unread_message_count, - :all_aspects, + helper_method :all_aspects, :all_contacts_count, :my_contacts_count, :only_sharing_count, @@ -38,15 +36,6 @@ class ApplicationController < ActionController::Base 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 @all_aspects ||= current_user.aspects end diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index cc1132c3eb..3cafaecfa7 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -41,6 +41,8 @@ class NotificationsController < ApplicationController end @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| format.html format.xml { render :xml => @notifications.to_xml } diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 4a877822b9..4385281b83 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -36,16 +36,11 @@ module LayoutHelper def set_current_user_in_javascript return unless current_user - + current_user_presenter = UserPresenter.new(current_user) + content_tag(:script) do <<-JS.html_safe - app.user( - _.extend(#{current_user.person.as_api_response(:backbone).to_json}, { - notifications_count : #{notification_count}, - unread_messages_count : #{unread_message_count}, - admin : #{current_user.admin?} - }) - ); + app.user(#{current_user_presenter.to_json}); JS end end diff --git a/app/models/user.rb b/app/models/user.rb index 174b89579b..2562e38b69 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -98,6 +98,14 @@ class User < ActiveRecord::Base where('last_sign_in_at > ?', time) 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] def self.find_by_invitation(invitation) service = invitation.service diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb new file mode 100644 index 0000000000..38436efd5a --- /dev/null +++ b/app/presenters/user_presenter.rb @@ -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 \ No newline at end of file diff --git a/app/views/notifications/index.html.haml b/app/views/notifications/index.html.haml index db4c85d547..4c3d091ac5 100644 --- a/app/views/notifications/index.html.haml +++ b/app/views/notifications/index.html.haml @@ -1,8 +1,8 @@ #notifications_content .span-13 %h2 - %span.notification_count{:class => ('unread' if notification_count > 0)} - = notification_count + %span.notification_count{:class => ('unread' if @unread_notification_count >0 )} + = @unread_notification_count = t('.notifications') .span-8.last = link_to t('.mark_all_as_read'), notifications_read_all_path, :class => 'button' -- GitLab