Class: Fdchat

Inherits:
Object
  • Object
show all
Defined in:
lib/fdchat.rb,
lib/fdchat/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, owner: nil, owner_id: nil) ⇒ Fdchat

Returns a new instance of Fdchat.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fdchat.rb', line 9

def initialize(id: nil, owner: nil,owner_id: nil)
  if owner_id
    @sala = ChatSala.find_by(
        owner_id: BSON::ObjectId.from_string(owner_id)
    )
  else
  @sala = ChatSala.find_by(:$or => [
    { _id: id},
    { owner: owner}
  ])
  end
  unless @sala
   @sala = ChatSala.create!( owner: owner)
   if @sala
    add_participant_sala(owner)
   end
  end
end

Instance Attribute Details

#salaObject

Returns the value of attribute sala.



7
8
9
# File 'lib/fdchat.rb', line 7

def sala
  @sala
end

Instance Method Details

#add_participant_sala(participant) ⇒ Object



65
66
67
68
# File 'lib/fdchat.rb', line 65

def add_participant_sala(participant)
  @sala.chat_sala_alloweds
      .find_or_create_by!(participant: participant)
end

#all_chatsObject



27
28
29
# File 'lib/fdchat.rb', line 27

def all_chats
  @sala.chats.order_by(:created_at => 'desc')
end

#check_participantsObject



62
63
64
# File 'lib/fdchat.rb', line 62

def check_participants
  @sala.chat_sala_alloweds.only(:participant_id, :participant_type) if @sala.chat_sala_alloweds
end

#close(participant_id) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/fdchat.rb', line 43

def close(participant_id)
  if has_participant(participant_id)
    @sala.owner.update!(closed: true)
    @sala.update!(closed: true)
    true
  else
    false
  end
end

#create_mensaje(sender: nil, text: nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/fdchat.rb', line 30

def create_mensaje(sender:nil,text:nil)
  mensaje = @sala.chats.create!(
        sender:sender,
        text:text
    )
  self.add_participant_sala(sender)
  mensaje
end

#has_participant(participant_id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/fdchat.rb', line 52

def has_participant(participant_id)
  rtn = false
  check_participants.each do |part|
   if  part.participant_id.to_s == participant_id.to_s
     rtn =  true
     break
   end
  end
  rtn
end

#read(chat_id) ⇒ Object



38
39
40
41
42
# File 'lib/fdchat.rb', line 38

def read(chat_id)
  mensaje = @sala.chats.find(chat_id)
  mensaje.update!(read: true)
  mensaje
end

#remove_participant_sala(participant) ⇒ Object



69
70
71
# File 'lib/fdchat.rb', line 69

def remove_participant_sala(participant)
  @sala.chat_sala_alloweds.find_by(participant: participant)&.delete
end