2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2016-09-21 17:22:40 +00:00
|
|
|
class Mapping < ApplicationRecord
|
2015-10-02 08:04:30 +00:00
|
|
|
scope :topicmapping, -> { where(mappable_type: :Topic) }
|
|
|
|
scope :synapsemapping, -> { where(mappable_type: :Synapse) }
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
validates :xloc, presence: true,
|
|
|
|
unless: proc { |m| m.mappable_type == 'Synapse' }
|
2016-02-21 09:25:39 +00:00
|
|
|
validates :yloc, presence: true,
|
2016-07-26 00:14:23 +00:00
|
|
|
unless: proc { |m| m.mappable_type == 'Synapse' }
|
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
|
|
|
|
after_update :after_updated
|
|
|
|
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 = {})
|
|
|
|
super(methods: [: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'
|
|
|
|
meta = {'x': xloc, 'y': yloc, 'mapping_id': id}
|
|
|
|
Events::TopicAddedToMap.publish!(mappable, map, user, meta)
|
2017-01-03 21:12:58 +00:00
|
|
|
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicAdded', topic: mappable.filtered, mapping_id: id
|
2016-12-16 21:51:52 +00:00
|
|
|
elsif mappable_type == 'Synapse'
|
|
|
|
Events::SynapseAddedToMap.publish!(mappable, map, user, meta)
|
2017-01-03 21:12:58 +00:00
|
|
|
ActionCable.server.broadcast(
|
|
|
|
'map_' + map.id.to_s,
|
|
|
|
type: 'synapseAdded',
|
|
|
|
synapse: mappable.filtered,
|
|
|
|
topic1: mappable.topic1.filtered,
|
|
|
|
topic2: mappable.topic2.filtered,
|
|
|
|
mapping_id: id)
|
2016-12-16 21:51:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_updated
|
|
|
|
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
|
|
|
|
meta = {'x': xloc, 'y': yloc, 'mapping_id': id}
|
|
|
|
Events::TopicMovedOnMap.publish!(mappable, map, updated_by, meta)
|
2017-01-03 21:12:58 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_destroyed
|
|
|
|
if mappable.defer_to_map
|
|
|
|
mappable.permission = mappable.defer_to_map.permission
|
|
|
|
mappable.defer_to_map_id = nil
|
|
|
|
mappable.save
|
|
|
|
end
|
|
|
|
|
|
|
|
meta = {'mapping_id': id}
|
|
|
|
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
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|