2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
class Mapping < ApplicationRecord
|
2018-01-21 22:21:00 +00:00
|
|
|
scope :topicmapping, (-> { where(mappable_type: :Topic) })
|
|
|
|
scope :synapsemapping, (-> { where(mappable_type: :Synapse) })
|
2015-10-02 08:04:30 +00:00
|
|
|
|
|
|
|
belongs_to :mappable, polymorphic: true
|
2016-07-26 00:14:23 +00:00
|
|
|
belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true
|
2014-08-12 22:14:04 +00:00
|
|
|
belongs_to :user
|
2016-12-16 21:51:52 +00:00
|
|
|
belongs_to :updated_by, class_name: 'User'
|
2016-01-26 09:58:33 +00:00
|
|
|
|
|
|
|
validates :map, presence: true
|
|
|
|
validates :mappable, presence: true
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
delegate :name, to: :user, prefix: true
|
2014-08-12 16:01:01 +00:00
|
|
|
|
2016-12-16 21:51:52 +00:00
|
|
|
after_create :after_created
|
2017-02-11 05:20:42 +00:00
|
|
|
after_create :after_created_async
|
2016-12-16 21:51:52 +00:00
|
|
|
after_update :after_updated
|
2017-02-11 05:20:42 +00:00
|
|
|
after_update :after_updated_async
|
2016-12-16 21:51:52 +00:00
|
|
|
before_destroy :before_destroyed
|
|
|
|
|
2014-08-12 16:01:01 +00:00
|
|
|
def user_image
|
2016-07-26 00:14:23 +00:00
|
|
|
user.image.url
|
2014-08-12 16:01:01 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
2018-01-20 22:10:26 +00:00
|
|
|
super(methods: %i[user_name user_image])
|
2014-08-12 16:01:01 +00:00
|
|
|
end
|
2016-12-16 21:51:52 +00:00
|
|
|
|
|
|
|
def after_created
|
|
|
|
if mappable_type == 'Topic'
|
2017-02-11 05:20:42 +00:00
|
|
|
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicAdded', topic: mappable.filtered, mapping_id: id
|
2017-01-24 00:30:13 +00:00
|
|
|
meta = { 'x': xloc, 'y': yloc, 'mapping_id': id }
|
2016-12-16 21:51:52 +00:00
|
|
|
Events::TopicAddedToMap.publish!(mappable, map, user, meta)
|
|
|
|
elsif mappable_type == 'Synapse'
|
2017-01-03 21:12:58 +00:00
|
|
|
ActionCable.server.broadcast(
|
|
|
|
'map_' + map.id.to_s,
|
|
|
|
type: 'synapseAdded',
|
|
|
|
synapse: mappable.filtered,
|
2017-05-20 17:57:52 +00:00
|
|
|
topic1: mappable.topic1&.filtered,
|
|
|
|
topic2: mappable.topic2&.filtered,
|
2017-01-24 00:30:13 +00:00
|
|
|
mapping_id: id
|
|
|
|
)
|
2017-03-07 03:49:46 +00:00
|
|
|
meta = { 'mapping_id': id }
|
|
|
|
Events::SynapseAddedToMap.publish!(mappable, map, user, meta)
|
2016-12-16 21:51:52 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-07 03:49:46 +00:00
|
|
|
|
2017-02-11 05:20:42 +00:00
|
|
|
def after_created_async
|
|
|
|
FollowService.follow(map, user, 'contributed')
|
|
|
|
end
|
|
|
|
handle_asynchronously :after_created_async
|
2016-12-16 21:51:52 +00:00
|
|
|
|
|
|
|
def after_updated
|
2018-01-21 22:21:00 +00:00
|
|
|
return unless (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?)
|
|
|
|
meta = { 'x': xloc, 'y': yloc, 'mapping_id': id }
|
|
|
|
Events::TopicMovedOnMap.publish!(mappable, map, updated_by, meta)
|
|
|
|
ActionCable.server.broadcast('map_' + map.id.to_s, type: 'topicMoved',
|
|
|
|
id: mappable.id, mapping_id: id,
|
|
|
|
x: xloc, y: yloc)
|
2016-12-16 21:51:52 +00:00
|
|
|
end
|
2017-03-07 03:49:46 +00:00
|
|
|
|
2017-02-11 05:20:42 +00:00
|
|
|
def after_updated_async
|
2018-01-21 22:21:00 +00:00
|
|
|
return unless (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?)
|
|
|
|
FollowService.follow(map, updated_by, 'contributed')
|
2017-02-11 05:20:42 +00:00
|
|
|
end
|
|
|
|
handle_asynchronously :after_updated_async
|
2016-12-16 21:51:52 +00:00
|
|
|
|
|
|
|
def before_destroyed
|
|
|
|
if mappable.defer_to_map
|
|
|
|
mappable.permission = mappable.defer_to_map.permission
|
|
|
|
mappable.defer_to_map_id = nil
|
|
|
|
mappable.save
|
|
|
|
end
|
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
meta = { 'mapping_id': id }
|
2016-12-16 21:51:52 +00:00
|
|
|
if mappable_type == 'Topic'
|
|
|
|
Events::TopicRemovedFromMap.publish!(mappable, map, updated_by, meta)
|
2017-01-03 21:12:58 +00:00
|
|
|
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicRemoved', id: mappable.id, mapping_id: id
|
2016-12-16 21:51:52 +00:00
|
|
|
elsif mappable_type == 'Synapse'
|
|
|
|
Events::SynapseRemovedFromMap.publish!(mappable, map, updated_by, meta)
|
2017-01-03 21:12:58 +00:00
|
|
|
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'synapseRemoved', id: mappable.id, mapping_id: id
|
2016-12-16 21:51:52 +00:00
|
|
|
end
|
2017-02-11 05:20:42 +00:00
|
|
|
FollowService.follow(map, updated_by, 'contributed')
|
2016-12-16 21:51:52 +00:00
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|