metamaps--metamaps/app/models/message.rb

31 lines
718 B
Ruby
Raw Permalink Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
class Message < ApplicationRecord
2015-12-11 19:23:41 +00:00
belongs_to :user
belongs_to :resource, polymorphic: true
delegate :name, to: :user, prefix: true
after_create :after_created
#after_create :after_created_async
2015-12-11 19:23:41 +00:00
def user_image
user.image.url
2015-12-11 19:23:41 +00:00
end
def as_json(_options = {})
json = super(methods: [:user_name, :user_image])
2015-12-11 19:23:41 +00:00
json
end
def after_created
ActionCable.server.broadcast 'map_' + resource.id.to_s, type: 'messageCreated', message: as_json
end
def after_created_async
FollowService.follow(resource, user, 'commented')
NotificationService.notify_followers(resource, 'map_message', self)
end
handle_asynchronously :after_created_async
2015-12-11 19:23:41 +00:00
end