Module: SocialStream::Ability::Base

Includes:
CanCan::Ability
Included in:
SocialStream::Ability
Defined in:
lib/social_stream/ability/base.rb

Instance Method Summary collapse

Instance Method Details

#initialize(subject) ⇒ Object

Create a new ability for this user, who is currently representing subject



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/social_stream/ability/base.rb', line 7

def initialize(subject)
  
  #Download alias action
  alias_action :download, :to => :show
  
  # Activity Objects
  (SocialStream.objects - [ :actor, :comment ]).map{ |obj|
    obj.to_s.classify.constantize
  }.each do |klass|
    can :create, klass do |k| # can :create, Post do |post|
      k.build_post_activity.allow?(subject, 'create')
    end

    can :read, klass do |k| # can :read, Post do |post|
      k.post_activity.allow?(subject, 'read')
    end

    can :update, klass do |k| # can :update, Post do |post|
      k.post_activity.allow?(subject, 'update')
    end

    can :destroy, klass do |k| # can :destroy, Post do |post|
      k.post_activity.allow?(subject, 'destroy')
    end
  end

  can :create, Comment do |c|
    c._activity_parent.allow?(subject, 'read')
  end

  can :read, Comment do |c|
    c.post_activity.allow?(subject, 'read')
  end

  can :update, Comment do |c|
    c.post_activity.allow?(subject, 'update')
  end

  can :destroy, Comment do |c|
    c.post_activity.allow?(subject, 'destroy')
  end

  # Activities
  can :create, Activity do |a|
    a.allow?(subject, 'create')
  end

  can :read, Activity do |a|
    a.allow?(subject, 'read')
  end

  can :update, Activity do |a|
    a.allow?(subject, 'update')
  end

  can :destroy, Activity do |a|
    a.allow?(subject, 'destroy')
  end

  # Users
  can :read, User

  can :update, User do |u|
    u.represented_by?(subject)
  end

  # Groups
  can :read, Group

  can :create, Group do |g|
    subject.present? &&
      g.author_id == Actor.normalize_id(subject)
  end

  can :update, Group do |g|
    g.represented_by?(subject)
  end

  can :destroy, Group do |g|
    g.represented_by?(subject)
  end

  can :read, Profile

  # Profile
  can :update, Profile do |p|
    p.subject.represented_by?(subject)
  end

  # Privacy
  can [:create, :read, :update, :destroy], Relation::Custom, :actor_id => subject.try(:actor_id)
end