Class: Relation::Custom

Inherits:
Relation
  • Object
show all
Defined in:
app/models/relation/custom.rb

Overview

When a new subject is created, a initial set of relations is created for him. Afterwards, the subject can customize them and adapt them to his own preferences.

Default relations are defined at config/initializers/social_stream.rb

Constant Summary

Constants inherited from Relation

Negative, Positive

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

#<=>, allow, allow?, create_activity?, #follow?, ids_shared_with, #mode, negative_names, normalize, normalize_id, #positive?, positive_names, system_list

Class Method Details

.defaults_for(actor) ⇒ Object



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
# File 'app/models/relation/custom.rb', line 25

def defaults_for(actor)
  subject_type = actor.subject.class.to_s.underscore

  cfg_rels = SocialStream.custom_relations.with_indifferent_access[subject_type]

  if cfg_rels.nil?
    raise "Undefined relations for subject type #{ subject_type }. Please, add an entry to config/initializers/social_stream.rb"
  end

  rels = {}

  cfg_rels.each_pair do |name, cfg_rel|
    rels[name] =
      create! :actor =>         actor,
              :name  =>         cfg_rel[:name],
              :receiver_type => cfg_rel[:receiver_type]

    if (ps = cfg_rel[:permissions]).present?
      ps.each do |p| 
        p.push(nil) if p.size == 1

        rels[name].permissions << 
          Permission.find_or_create_by_action_and_object(*p)
      end 
    end
  end

  # Parent, relations must be set after creation
  # FIXME: Can fix with ruby 1.9 and ordered hashes
  cfg_rels.each_pair do |name, cfg_rel|
    rels[name].update_attribute(:parent, rels[cfg_rel['parent']])
  end

  rels.values
end

Instance Method Details

#available_permissionsObject



67
68
69
# File 'app/models/relation/custom.rb', line 67

def available_permissions
  Permission.available(subject)
end

#subjectObject

The subject who defined of this relation



63
64
65
# File 'app/models/relation/custom.rb', line 63

def subject
  actor.subject
end