user_presenter.rb 1.3 KB
Newer Older
1
class UserPresenter
2
  attr_accessor :user, :aspects_ids
3

4 5 6
  def initialize(user, aspects_ids)
    self.user        = user
    self.aspects_ids = aspects_ids
7 8
  end

9 10 11 12 13 14 15 16 17 18
  def to_json(options={})
    user.person.as_api_response(:backbone).update(
      notifications_count:   notifications_count,
      unread_messages_count: unread_messages_count,
      admin:                 admin,
      moderator:             moderator,
      aspects:               aspects,
      services:              services,
      following_count:       user.contacts.receiving.count,
      configured_services:   configured_services
19 20 21
    ).to_json(options)
  end

22 23 24 25
  def services
    ServicePresenter.as_collection(user.services)
  end

26
  def configured_services
27
    user.services.map(&:provider)
28 29
  end

30
  def aspects
31 32
    @aspects ||= begin
                   aspects = AspectPresenter.as_collection(user.aspects)
33 34
                   no_aspects = aspects_ids.empty?
                   aspects.each {|a| a[:selected] = no_aspects || aspects_ids.include?(a[:id].to_s) }
35
                 end
36
  end
37 38

  def notifications_count
39
    @notification_count ||= user.unread_notifications.count
40 41 42 43 44 45 46 47 48
  end

  def unread_messages_count
    @unread_message_count ||= user.unread_message_count
  end

  def admin
    user.admin?
  end
49 50 51 52

  def moderator
    user.moderator?
  end
53
end