cirandas.net

ref: master

app/presenters/activity_presenter.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class ActivityPresenter < Presenter
  def self.base_class
    ActionTracker::Record
  end

  def self.available?(instance)
    instance.kind_of?(ActionTracker::Record) || instance.kind_of?(ProfileActivity)
  end

  def self.target(instance)
    if instance.kind_of?(ProfileActivity)
      target(instance.activity)
    elsif instance.kind_of?(ActionTracker::Record)
       instance.target
    else
      instance
    end
  end

  def self.owner(instance)
    instance.kind_of?(ProfileActivity) ? instance.profile : instance.user
  end

  def target
    self.class.target(encapsulated_instance)
  end

  def owner
    self.class.owner(encapsulated_instance)
  end

  def hidden_for?(user)
    (target.respond_to?(:display_to?) &&
     !target.display_to?(user)) ||
    !AccessLevels.can_access?(target_profile.wall_access, user, target_profile) ||
    !target_profile.allow_followers?
  end

  def involved?(user)
    owner == user || target == user
  end

  private

  def target_profile
    if target.is_a? Profile
      target
    elsif target.is_a? Article
      target.profile
    elsif target.is_a? Scrap
      target.receiver
    else
      owner
    end
  end
end

# Preload ActivityPresenter subclasses to allow `Presenter.for()` to work
Dir.glob(File.join('app', 'presenters', 'activity', '*.rb')) do |file|
  load file
end