2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2016-09-21 17:22:40 +00:00
|
|
|
class Message < ApplicationRecord
|
2015-12-11 19:23:41 +00:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :resource, polymorphic: true
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
delegate :name, to: :user, prefix: true
|
2017-01-03 21:12:58 +00:00
|
|
|
|
|
|
|
after_create :after_created
|
2015-12-11 19:23:41 +00:00
|
|
|
|
|
|
|
def user_image
|
2016-07-26 00:14:23 +00:00
|
|
|
user.image.url
|
2015-12-11 19:23:41 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
|
|
|
json = super(methods: [:user_name, :user_image])
|
2015-12-11 19:23:41 +00:00
|
|
|
json
|
|
|
|
end
|
2017-01-03 21:12:58 +00:00
|
|
|
|
|
|
|
def after_created
|
|
|
|
ActionCable.server.broadcast 'map_' + resource.id.to_s, type: 'messageCreated', message: self.as_json
|
|
|
|
end
|
2015-12-11 19:23:41 +00:00
|
|
|
end
|