Table name: messages
id :integer not null, primary key message :text created_at :datetime updated_at :datetime offline :boolean default(FALSE) chat_conversation_id :integer university_id :integer
Set pagination limit for page.
# File app/models/message.rb, line 69 def self.paginate(page = 1, per_page = 10, offset_delta = 0) limit(per_page.to_i).offset((per_page.to_i * (page.to_i - 1)) + offset_delta.to_i) end
Returns message json object.
# File app/models/message.rb, line 43 def as_json(options = {}) json = { id: self.id, conversation_id: chat_conversation.id, from: { id: sender.id, name: sender.full_name, image: (ActionController::Base.helpers.image_path sender.image_url(:thumb)) }, message: message, created_at: created_at, chat_box_data: chat_conversation.chat_data_for(User.current), sent: sender == User.current } json.merge!( attachment: { image: attachment.image?, file_name: attachment.attachment_file_name, file_type: attachment.attachment_content_type.split('/').last, file_size: ApplicationController.helpers.number_to_human_size(attachment.attachment_file_size), url: attachment.attachment.url } ) if attachment json end
Create receipts for message.
# File app/models/message.rb, line 32 def create_receipts self.receipts.create!(user: User.current, read: true, sent: true, chat_conversation_id: self.chat_conversation.id) CreateReceipts.perform_async(id) end
Returns whether the message is delivered to user or not.
# File app/models/message.rb, line 38 def read?(user) received_receipts.where(user: user, read: true).exists? end