Class: PolylingoChat::Conversation

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/generators/polylingo_chat/install/templates/models/conversation.rb

Instance Method Summary collapse

Instance Method Details

#add_participant(record, role: nil) ⇒ Object

Add a participant to the conversation



30
31
32
33
34
# File 'lib/generators/polylingo_chat/install/templates/models/conversation.rb', line 30

def add_participant(record, role: nil)
  participants.find_or_create_by(participantable: record) do |p|
    p.role = role if role
  end
end

#includes_participant?(record) ⇒ Boolean

Check if a record is a participant in this conversation

Returns:

  • (Boolean)


25
26
27
# File 'lib/generators/polylingo_chat/install/templates/models/conversation.rb', line 25

def includes_participant?(record)
  participants.exists?(participantable: record)
end

#participantablesObject

Get all participantable objects (User, Vendor, Customer, etc.)



9
10
11
# File 'lib/generators/polylingo_chat/install/templates/models/conversation.rb', line 9

def participantables
  participants.map(&:participantable)
end

#participantables_of_type(klass) ⇒ Object

Get participants of a specific type Example: conversation.participantables_of_type(User)



15
16
17
# File 'lib/generators/polylingo_chat/install/templates/models/conversation.rb', line 15

def participantables_of_type(klass)
  participants.where(participantable_type: klass.name).map(&:participantable)
end

#usersObject

Backward compatibility - returns all User participants



20
21
22
# File 'lib/generators/polylingo_chat/install/templates/models/conversation.rb', line 20

def users
  participantables_of_type(User) if defined?(User)
end