Module: ActsAsMessageable::Model::InstanceMethods

Extended by:
T::Helpers, T::Sig
Defined in:
lib/acts_as_messageable/model.rb

Instance Method Summary collapse

Instance Method Details

#delete_message(message) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/acts_as_messageable/model.rb', line 194

def delete_message(message)
  current_user = self

  case current_user
  when message.to
    attribute = message.recipient_delete ? :recipient_permanent_delete : :recipient_delete
  when message.from
    attribute = message.sender_delete ? :sender_permanent_delete : :sender_delete
  else
    raise "#{current_user} can't delete this message"
  end

  ActsAsMessageable.rails_api.new(message).update_attributes!(attribute => true)
end

#deleted_messagesObject



116
117
118
# File 'lib/acts_as_messageable/model.rb', line 116

def deleted_messages
  messages(true)
end

#messages(trash = false) ⇒ Object



87
88
89
90
91
92
# File 'lib/acts_as_messageable/model.rb', line 87

def messages(trash = false)
  result = self.class.messages_class_name.connected_with(self, trash)
  result.relation_context = self

  result
end

#received_messagesObject



96
97
98
99
100
101
102
# File 'lib/acts_as_messageable/model.rb', line 96

def received_messages
  result = ActsAsMessageable.rails_api.new(received_messages_relation)
  result = T.unsafe(result).scoped.where(recipient_delete: false)
  result.relation_context = self

  result
end

#reply_to(message, *args) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/acts_as_messageable/model.rb', line 178

def reply_to(message, *args)
  current_user = T.cast(self, ActiveRecord::Base)

  return unless message.participant?(current_user)

  reply_message = T.unsafe(self).send_message(message.real_receiver(current_user), *args)
  reply_message.parent = message
  reply_message.save

  reply_message
end

#restore_message(message) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/acts_as_messageable/model.rb', line 213

def restore_message(message)
  current_user = self

  case current_user
  when message.to
    attribute = :recipient_delete
  when message.from
    attribute = :sender_delete
  else
    raise "#{current_user} can't restore this message"
  end

  ActsAsMessageable.rails_api.new(message).update_attributes!(attribute => false)
end

#send_message(to, *args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/acts_as_messageable/model.rb', line 130

def send_message(to, *args)
  message_attributes = {}

  case args.first
  when String
    self.class.messages_class_name.required.each_with_index do |attribute, index|
      message_attributes[attribute] = args[index]
    end
  when Hash
    message_attributes = args.first
  end

  message = self.class.messages_class_name.new message_attributes
  message.received_messageable = to
  message.sent_messageable = self
  message.save!

  message
end

#send_message!(to, *args) ⇒ Object



162
163
164
# File 'lib/acts_as_messageable/model.rb', line 162

def send_message!(to, *args)
  T.unsafe(self).send_message(to, *T.unsafe(args)).tap(&:save!)
end

#sent_messagesObject



106
107
108
109
110
111
112
# File 'lib/acts_as_messageable/model.rb', line 106

def sent_messages
  result = ActsAsMessageable.rails_api.new(sent_messages_relation)
  result = T.unsafe(result).scoped.where(sender_delete: false)
  result.relation_context = self

  result
end