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/relations.yml

Constant Summary collapse

DEFAULT =

Default relations shipped with Social Stream

{}
CONFIG_FILE =

Default relations are re-defined in this configuration file

File.join(::Rails.root, 'config', 'relations.yml')

Constants inherited from Relation

Negative, Positive

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

allow, allow?, #follow?, ids_shared_with, #mode, negative_names, normalize, normalize_id, #positive?, positive_names

Class Method Details

.configObject

Relations configuration



26
27
28
# File 'app/models/relation/custom.rb', line 26

def config
  @config ||= build_config
end

.defaults_for(actor) ⇒ Object



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

def defaults_for(actor)
  cfg_rels = config[actor.subject_type.underscore]

  if cfg_rels.nil?
    raise "Undefined relations for subject type #{ actor.subject_type }. Please, add an entry to #{ CONFIG_FILE }"
  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

.strongestObject

A relation in the top of a strength hierarchy



65
66
67
# File 'app/models/relation/custom.rb', line 65

def strongest
  roots
end

Instance Method Details

#<=>(rel) ⇒ Object

Compare two relations



79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/relation/custom.rb', line 79

def <=> rel
  return -1 if rel.is_a?(Public)

  if ancestor_ids.include?(rel.id)
    1
  elsif rel.ancestor_ids.include?(id)
    -1
  else
    0
  end
end

#strongerObject

Other relations above in the same hierarchy that this relation



102
103
104
# File 'app/models/relation/custom.rb', line 102

def stronger
  ancestors
end

#stronger_or_equalObject

Relations above or at the same level of this relation



107
108
109
# File 'app/models/relation/custom.rb', line 107

def stronger_or_equal
  path
end

#to_cheesecake_hash(options = {}) ⇒ Object

JSON compatible with SocialCheesecake



112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/relation/custom.rb', line 112

def to_cheesecake_hash(options = {})
  {:id => id, :name => name}.tap do |hash|
    if options[:subsector]
      hash[:actors] = ties.map{ |t| [t.contact.receiver_id, t.contact.receiver.name, t.contact_id] }.uniq
    else
      hash[:subsectors] = ( weaker.present? ?
                            weaker.map{ |w| w.to_cheesecake_hash(:subsector => true) } :
                            Array.wrap(to_cheesecake_hash(:subsector => true)) )
    end
  end
end

#weakerObject

Other relations below in the same hierarchy that this relation



92
93
94
# File 'app/models/relation/custom.rb', line 92

def weaker
  descendants
end

#weaker_or_equalObject

Relations below or at the same level of this relation



97
98
99
# File 'app/models/relation/custom.rb', line 97

def weaker_or_equal
  subtree
end