Class: Relation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Relation
- Defined in:
- app/models/relation.rb
Overview
A relation defines a type of tie. Relations are affective (friendship, liking, respect), formal or biological (authority, kinship), transfer of material resources (transactions, lending and borrowing), messages or conversations, physical connection and affiliation to same organizations.
Strength hierarchies
Relations are arranged in strength hierarchies, denoting that some ties between two actors are stronger than others. For example, a “friend” relation is stronger than an “acquaintance” relation.
When a strong tie is established, ties with weaker relations are establised as well
Permissions
Subjects assign permissions to relations. This way, when establishing ties, they are granting permissions to their contacts.
See the documentation of Permission for more details on permission definition.
Activities and relations
Each Activity can be attached to one or more relations. The Relation sets up the mode in which the Activity is shared. It sets the Audience that has access to it, and the Permissions that rule that access.
Defined Under Namespace
Classes: Custom, CustomsController, Public, Reject, Single
Constant Summary collapse
- Positive =
%w{ custom public }
- Negative =
%w{ reject }
Class Method Summary collapse
-
.allow(subject, action, object, options = {}) ⇒ Object
All the relations that allow subject to perform action.
- .allow?(*args) ⇒ Boolean
-
.negative_names ⇒ Object
Negative relations: [ ‘Relation::Reject’ ].
-
.normalize(r, options = {}) ⇒ Object
Get relation from object, if possible.
- .normalize_id(r, options = {}) ⇒ Object
-
.positive_names ⇒ Object
Positive relation names: [ ‘Relation::Custom’, ‘Relation::Public’ ].
Instance Method Summary collapse
-
#mode ⇒ Object
Relation class scoped in the same mode that this relation.
-
#positive? ⇒ Boolean
Is this Relation a Positive one?.
Class Method Details
.allow(subject, action, object, options = {}) ⇒ Object
All the relations that allow subject to perform action
Options:
in:: Limit possible relations to a set
public_relations:: include also {Relation::Public} whose activities can always be read
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/relation.rb', line 103 def allow(subject, action, object, = {}) q = select("DISTINCT relations.*"). joins(:contacts). joins(:permissions) conds = Permission.arel_table[:action].eq(action).and(Permission.arel_table[:object].eq(object)) # Relation::Public permissions cannot be customized yet if action == 'read' && object == 'activity' && ([:public].nil? || [:public]) conds = conds.or(Relation.arel_table[:type].eq('Relation::Public')) end # Add in condition if ! [:in].nil? conds = conds.and(Relation.arel_table[:id].in(Relation.normalize_id(Array([:in])))) end # subject conditions conds = conds.and(Contact.arel_table[:receiver_id].eq(Actor.normalize_id(subject))) q.where(conds) end |
.allow?(*args) ⇒ Boolean
128 129 130 |
# File 'app/models/relation.rb', line 128 def allow?(*args) allow(*args).to_a.any? end |
.negative_names ⇒ Object
Negative relations: [ ‘Relation::Reject’ ]
94 95 96 |
# File 'app/models/relation.rb', line 94 def negative_names Negative.map{ |r| "Relation::#{ r.classify }" } end |
.normalize(r, options = {}) ⇒ Object
Get relation from object, if possible
- Options
- sender
-
The sender of the tie
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/relation.rb', line 58 def normalize(r, = {}) case r when Relation r when String if [:sender] [:sender].relation_custom(r) else raise "Must provide a sender when looking up relations from name: #{ [:sender] }" end when Integer Relation.find r when Array r.map{ |e| Relation.normalize(e, ) } else raise "Unable to normalize relation #{ r.class }: #{ r.inspect }" end end |
.normalize_id(r, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/relation.rb', line 77 def normalize_id(r, = {}) case r when Integer r when Array r.map{ |e| Relation.normalize_id(e, ) } else normalize(r, ).id end end |
.positive_names ⇒ Object
Positive relation names: [ ‘Relation::Custom’, ‘Relation::Public’ ]
89 90 91 |
# File 'app/models/relation.rb', line 89 def positive_names Positive.map{ |r| "Relation::#{ r.classify }" } end |
Instance Method Details
#mode ⇒ Object
Relation class scoped in the same mode that this relation
134 135 136 |
# File 'app/models/relation.rb', line 134 def mode Relation.mode(sender_type, receiver_type) end |
#positive? ⇒ Boolean
Is this Relation a Positive one?
139 140 141 |
# File 'app/models/relation.rb', line 139 def positive? self.class.positive_names.include?(self.class.to_s) end |