Class: Stream::Aspect

Inherits:
Base
  • Object
show all
Defined in:
lib/stream/aspect.rb

Overview

Copyright © 2010-2011, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Constant Summary

Constant Summary

Constants inherited from Base

Base::TYPES_OF_POST_IN_STREAM

Instance Attribute Summary

Attributes inherited from Base

#max_time, #order, #publisher, #user

Instance Method Summary (collapse)

Methods inherited from Base

#post_from_group, #stream_posts

Constructor Details

- (void) initialize(user, inputted_aspect_ids, opts = {})

Parameters:

  • user (User)
  • inputted_aspect_ids (Array<Integer>)

    Ids of aspects for given stream

  • aspect_ids (Array<Integer>)

    Aspects this stream is responsible for



13
14
15
16
# File 'lib/stream/aspect.rb', line 13

def initialize(user, inputted_aspect_ids, opts={})
  super(user, opts)
  @inputted_aspect_ids = inputted_aspect_ids
end

Instance Method Details

- (Aspect, Symbol) aspect

Note:

aspects.first is used for mobile. NOTE(this is a hack and should be fixed)

The first aspect in #aspects, given the stream is not for all aspects, or #aspects size is 1

Returns:



62
63
64
65
66
# File 'lib/stream/aspect.rb', line 62

def aspect
  if !for_all_aspects? || aspects.size == 1
    aspects.first
  end
end

- (Array<Integer>) aspect_ids

Maps ids into an array from #aspects

Returns:

  • (Array<Integer>)

    Aspect ids



34
35
36
# File 'lib/stream/aspect.rb', line 34

def aspect_ids
  @aspect_ids ||= aspects.map { |a| a.id }
end

- (ActiveRecord::Association<Aspect>) aspects

Filters aspects given the stream's aspect ids on initialization and the user. Will disclude aspects from inputted aspect ids if user is not associated with their target aspects.

Returns:

  • (ActiveRecord::Association<Aspect>)

    Filtered aspects given the stream's user



23
24
25
26
27
28
29
# File 'lib/stream/aspect.rb', line 23

def aspects
  @aspects ||= lambda do
    a = user.aspects
    a = a.where(:id => @inputted_aspect_ids) if @inputted_aspect_ids.any?
    a
  end.call
end

- (Boolean) can_comment?(post)

This is perfomance optimization, as everyone in your aspect stream you have a contact.

Parameters:

Returns:

  • (Boolean)


116
117
118
# File 'lib/stream/aspect.rb', line 116

def can_comment?(post)
  true
end

Provides a link to the user to the contacts page that corresponds with the stream's active aspects.

Returns:

  • (String)

    Link to contacts



103
104
105
106
107
108
109
# File 'lib/stream/aspect.rb', line 103

def contacts_link
  if for_all_aspects? || aspect_ids.size > 1
    Rails.application.routes.url_helpers.contacts_path
  else
    Rails.application.routes.url_helpers.contacts_path(:a_id => aspect.id)
  end
end

- (String) contacts_title

Provides a translated title for contacts box on the right pane.

Returns:



91
92
93
94
95
96
97
# File 'lib/stream/aspect.rb', line 91

def contacts_title
  if self.for_all_aspects? || self.aspect_ids.size > 1
    I18n.t('_contacts')
  else
   "#{self.aspect.name} (#{self.people.size})"
  end
end

- (Boolean) for_all_aspects?

Determine whether or not the stream is displaying across all of the user's aspects.

Returns:

  • (Boolean)


84
85
86
# File 'lib/stream/aspect.rb', line 84

def for_all_aspects?
  @all_aspects ||= aspect_ids.length == user.aspects.size
end

Returns URL

Returns:



55
56
57
# File 'lib/stream/aspect.rb', line 55

def link(opts={})
  Rails.application.routes.url_helpers.aspects_path(opts)
end

- (ActiveRecord::Association<Person>) people

Returns AR association of people within stream's given aspects

Returns:

  • (ActiveRecord::Association<Person>)

    AR association of people within stream's given aspects



50
51
52
# File 'lib/stream/aspect.rb', line 50

def people
  @people ||= Person.unique_from_aspects(aspect_ids, user).includes(:profile)
end

- (ActiveRecord::Association<Post>) posts

Returns AR association of posts

Returns:

  • (ActiveRecord::Association<Post>)

    AR association of posts



39
40
41
42
43
44
45
46
47
# File 'lib/stream/aspect.rb', line 39

def posts
  # NOTE(this should be something like Post.all_for_stream(@user, aspect_ids, {}) that calls visible_shareables
  @posts ||= user.visible_shareables(Post, :all_aspects? => for_all_aspects?,
                                           :by_members_of => aspect_ids,
                                           :type => TYPES_OF_POST_IN_STREAM,
                                           :order => "#{order} DESC",
                                           :max_time => max_time
                 )
end

- (String) title

The title that will display at the top of the stream's publisher box.

Returns:



72
73
74
75
76
77
78
# File 'lib/stream/aspect.rb', line 72

def title
  if self.for_all_aspects?
    I18n.t('streams.aspects.title')
  else
    self.aspects.to_sentence
  end
end