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-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-14 15:08:59 +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-14 15:08:59 +00:00
|
|
|
|
|
|
|
def after_created
|
|
|
|
if mappable_type == 'Topic'
|
|
|
|
meta = {'x': xloc, 'y': yloc}
|
|
|
|
Events::TopicAddedToMap.publish!(self, user, meta)
|
|
|
|
elsif mappable_type == 'Synapse'
|
|
|
|
Events::SynapseAddedToMap.publish!(self, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_updated
|
|
|
|
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
|
|
|
|
meta = {'x': xloc, 'y': yloc}
|
|
|
|
Events::TopicMovedOnMap.publish!(self, user, meta)
|
|
|
|
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
|
|
|
|
|
|
|
|
if mappable_type == 'Topic'
|
|
|
|
Events::TopicRemovedFromMap.publish!(self, user)
|
|
|
|
elsif mappable_type == 'Synapse'
|
|
|
|
Events::SynapseRemovedFromMap.publish!(self, user)
|
|
|
|
end
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|