Class: Contact

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

Overview

A Contact is an ordered pair of Actors, and therefore two Subjects.

Contacts are created at convenience (in the case of suggestions, for instance), and they do not mean that there is a real link between those two Subjects. Link existance is stored as a Tie. When Alice adds Bob as contact, a new Tie is created with the Contact from Alice to Bob and the Relation that Alice chose.

Inverse Contacts

Alice has a Contact to Bob. The inverse is the Contact from Bob to Alice. Inverse contacts are used to check if contacts are replied, for instance, if Bob added Alice as contact after she did so.

Again, the Contact from Bob to Alice must have positive ties to be active.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messageObject

Send a message when this contact is created or updated



20
21
22
# File 'app/models/contact.rb', line 20

def message
  @message
end

Instance Method Details

#actionObject

Is this Contact new or edit for subject ?

action is new when, despite of being created, it has not ties or it has a Tie with a reject relation.

The contact’s action is edit when it has any Tie with a custom relation or a public relation



170
171
172
# File 'app/models/contact.rb', line 170

def action
  sent? ? 'edit' : ( replied? ? 'reply' : 'new' )
end

#established?Boolean

Has this Contact any Tie?

Returns:

  • (Boolean)


119
120
121
# File 'app/models/contact.rb', line 119

def established?
  ties_count > 0
end

#inverse!Object

Find or create the inverse Contact



113
114
115
116
# File 'app/models/contact.rb', line 113

def inverse!
  inverse ||
    receiver.contact_to!(sender)
end

#options_for_selectObject

Return an object of choices suitable for the contact add button



185
186
187
# File 'app/models/contact.rb', line 185

def options_for_select
  sender.options_for_contact_select
end

#options_for_select_typeObject

Options for select are simple or multiple



191
192
193
# File 'app/models/contact.rb', line 191

def options_for_select_type
  sender.options_for_contact_select_type
end

#positive_replied?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'app/models/contact.rb', line 129

def positive_replied?
  inverse_id.present? &&
    self.class.positive.where(:id => inverse.id).any?
end

#receiver_subjectObject



103
104
105
# File 'app/models/contact.rb', line 103

def receiver_subject
  receiver.subject
end

#reflexive?Boolean

Does this Contact have the same sender and receiver?

Returns:

  • (Boolean)


108
109
110
# File 'app/models/contact.rb', line 108

def reflexive?
  sender_id == receiver_id
end

#replied?Boolean

The Contact in the other way is established

Returns:

  • (Boolean)


124
125
126
127
# File 'app/models/contact.rb', line 124

def replied?
  inverse_id.present? &&
    inverse.ties_count > 0
end

#sender_subjectObject



99
100
101
# File 'app/models/contact.rb', line 99

def sender_subject
  sender.subject
end

#sent?Boolean

The sender of this Contact has establised positive sent to the receiver

That means that there exists at least one Tie with a positive relation within them

Returns:

  • (Boolean)


158
159
160
# File 'app/models/contact.rb', line 158

def sent?
  ties_count > 0 && relations.where(:type => Relation.positive_names).any?
end

#statusObject



174
175
176
177
178
179
180
181
# File 'app/models/contact.rb', line 174

def status
  case action
  when 'edit'
    ties.includes(:relation).map(&:relation_name).join(", ")
  else
    I18n.t("contact.#{ action }.link")
  end
end

#user_authorObject

Record who creates ties in behalf of a group or organization

Defaults to the sender actor, if it is a user



145
146
147
148
# File 'app/models/contact.rb', line 145

def user_author
  @user_author ||
    build_user_author
end

#user_author=(subject) ⇒ Object

Set who creates ties in behalf of a group or organization



151
152
153
# File 'app/models/contact.rb', line 151

def user_author= subject
  @user_author = (subject.nil? ? nil : Actor.normalize(subject))
end

#verbObject

The ActivityVerb corresponding to this Contact. If this contact is pending, the other one was establised already, so this is going to “make-friend”. If it is not pending, the contact in the other way was not established, so this is following



138
139
140
# File 'app/models/contact.rb', line 138

def verb
  replied? ? "make-friend" : "follow"
end