Class: Tie

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tie.rb

Overview

A link between two actors in a relation.

The first Actor is the sender of the Tie. The second Actor is the receiver of the Tie.

Ties and Activities

Activities are attached to ties.

  • The sender actor is the author of the Activity. It is the user that uploads a resource to the website or the social entity that originates the activity.

  • The receiver is the target of the Activity. The wall-profile of an actor is composed by the resources assigned to the ties in which the actor is the receiver.

  • The Relation sets up the mode in which the Activity is shared. It sets the rules,

    or permissions, by which actors have access to the Activity.
    

Inverse ties

Relations can have its inverse. When a tie is establised, an inverse tie is establised as well.

Scopes

There are several scopes defined:

  • sent_by(actor), ties whose sender is actor

  • received_by(actor), ties whose receiver is actor

  • sent_or_received_by(actor), the union of the former

  • related_by(relation), ties with this relation. Accepts relation, relation_name, integer, array

  • pending, ties whose relation grant other relations, like friendship requests.

  • inverse(tie), the inverse of tie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_without_inverseObject

Avoids loops at create_inverse after save callback



32
33
34
# File 'app/models/tie.rb', line 32

def _without_inverse
  @_without_inverse
end

#relation_nameObject



81
82
83
# File 'app/models/tie.rb', line 81

def relation_name
  @relation_name || relation.try(:name)
end

Class Method Details

.Actor_id(a) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'app/models/tie.rb', line 216

def Actor_id(a)
  case a
  when Integer
    a
  when Actor
    a.id
  else
    a.actor.id
  end
end

.group_set_conditions(t) ⇒ Object



166
167
168
169
170
# File 'app/models/tie.rb', line 166

def group_set_conditions(t)
  arel_table[:receiver_id].eq(t.receiver_id).and(
    arel_table[:relation_id].eq(t.relation_id)).and(
    Permission.arel_table[:parameter].eq('group_set'))
end

.Relation(r, options = {}) ⇒ Object

Normalize a relation for ActiveRecord query from relation_name, id or Array

Options
mode

Relation mode



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/tie.rb', line 231

def Relation(r, options = {})
  case r
  when Relation
    r
  when String
    case options[:mode]
    when Array
      Relation.mode(*options[:mode]).find_by_name(r)
    when ActiveRecord::Relation
      options[:mode].find_by_name(r)
    else
      raise "Must provide a mode when looking up relations from name: #{ options[:mode] }"
    end
  when Integer
    r
  when Array
    r.map{ |e| Relation(e, options) }
  else
    raise "Unable to normalize relation #{ r.inspect }"
  end
end

.tie_conditions(t) ⇒ Object



152
153
154
155
156
157
# File 'app/models/tie.rb', line 152

def tie_conditions(t)
  arel_table[:sender_id].eq(t.sender_id).and(
    arel_table[:receiver_id].eq(t.receiver_id)).and(
    arel_table[:relation_id].eq(t.relation_id)).and(
    Permission.arel_table[:parameter].eq('tie'))
end

.weak_group_set_conditions(t) ⇒ Object



172
173
174
175
176
# File 'app/models/tie.rb', line 172

def weak_group_set_conditions(t)
  arel_table[:receiver_id].eq(t.receiver_id).and(
    arel_table[:relation_id].in(t.relation.stronger_or_equal)).and(
    Permission.arel_table[:parameter].eq('weak_group_set'))
end

.weak_set_conditions(t) ⇒ Object



159
160
161
162
163
164
# File 'app/models/tie.rb', line 159

def weak_set_conditions(t)
  arel_table[:sender_id].eq(t.sender_id).and(
    arel_table[:receiver_id].eq(t.receiver_id)).and(
    arel_table[:relation_id].in(t.relation.stronger_or_equal)).and(
    Permission.arel_table[:parameter].eq('weak_set'))
end

Instance Method Details

#permission?(user, action, object) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/tie.rb', line 147

def permission?(user, action, object)
  permissions(user, action, object).any?
end

#permissions(user, action, object) ⇒ Object



141
142
143
144
145
# File 'app/models/tie.rb', line 141

def permissions(user, action, object)
  self.class.
    sent_by(user).
    access_set(self, action, object)
end

#receiver_subjectObject



94
95
96
# File 'app/models/tie.rb', line 94

def receiver_subject
  receiver.try(:subject)
end

The tie with relation r inside this relation_set



116
117
118
# File 'app/models/tie.rb', line 116

def related(r)
  relation_set(:relations => r).first
end

#relation!Object



85
86
87
88
# File 'app/models/tie.rb', line 85

def relation!
  relation ||
    find_relation
end

#relation_set(options = {}) ⇒ Object

The set of ties between sender and receiver

Options
  • relations: Only ties with relations



102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/tie.rb', line 102

def relation_set(options = {})
  set = self.class.where(:sender_id => sender_id,
                         :receiver_id => receiver_id)

  if options.key?(:relations)
    set = 
      set.related_by self.class.Relation(options[:relations],
                                         :mode => relation.mode)
  end

  set
end

#sender_subjectObject



90
91
92
# File 'app/models/tie.rb', line 90

def sender_subject
  sender.try(:subject)
end