Module: TentD::Model::Permissible

Included in:
Follower, Following, Post, PostVersion, ProfileInfo
Defined in:
lib/tentd/model/permissible.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/tentd/model/permissible.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#assign_permissions(permissions) ⇒ Object



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
# File 'lib/tentd/model/permissible.rb', line 29

def assign_permissions(permissions)
  return unless permissions.kind_of?(Hash)

  if permissions.groups && permissions.groups.kind_of?(Array)
    permissions.groups.each do |g|
      next unless g.id
      group = Model::Group.first(:public_id => g.id, :fields => [:id])
      self.permissions.create(:group => group) if group
    end
  end

  if permissions.entities && permissions.entities.kind_of?(Hash)
    permissions.entities.each do |entity,visible|
      next unless visible
      followers = Model::Follower.all(:entity => entity, :fields => [:id])
      followers.each do |follower|
        self.permissions.create(:follower_access => follower)
      end
      followings = Model::Following.all(:entity => entity, :fields => [:id])
      followings.each do |following|
        self.permissions.create(:following => following)
      end
    end
  end

  self.public = permissions.public unless permissions.public.nil?
end

#permissions_json(extended = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tentd/model/permissible.rb', line 10

def permissions_json(extended = false)
  if extended
    groups = []
    entities = []
    send(respond_to?(:permissions) ? :permissions : :visibility_permissions).each do |permission|
      groups << permission.group.public_id if permission.group
      entities << permission.follower_access.entity if permission.follower_access
    end

    {
      :groups => groups.uniq,
      :entities => Hash[entities.uniq.map { |e| [e, true] }],
      :public => self.public
    }
  else
    { :public => self.public }
  end
end