diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5f89717c9000da20da0394782b532d88b4ab7697..cb34700f10161453028e91fe6281d2ce814eba44 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 cc1132c3eb25943602e51b34c579ea09ac3bd964..3cafaecfa7f491255b056d7ee28a033cb91673df 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 4a877822b918371d51503945f94c352c09ac3b1e..4385281b834caa30b7933a23420735174324de15 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 174b89579b7d68a319f7550c54f234eab4eb4687..2562e38b69a3bfd4a13cb48c07c2b51f6b16c00f 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 0000000000000000000000000000000000000000..38436efd5a22403f2d6cb020fdea4f13f3c26ace --- /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 db4c85d54742f3578aa799974984bc9398bdd2f0..4c3d091ac57103f184910208b979d299ff092207 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'