metamaps--metamaps/app/models/mapping.rb

62 lines
1.8 KiB
Ruby
Raw Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
class Mapping < ApplicationRecord
scope :topicmapping, -> { where(mappable_type: :Topic) }
scope :synapsemapping, -> { where(mappable_type: :Synapse) }
belongs_to :mappable, polymorphic: true
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
validates :xloc, presence: true,
unless: proc { |m| m.mappable_type == 'Synapse' }
validates :yloc, presence: true,
unless: proc { |m| m.mappable_type == 'Synapse' }
2016-01-26 09:58:33 +00:00
validates :map, presence: true
validates :mappable, presence: true
delegate :name, to: :user, prefix: true
2016-12-14 15:08:59 +00:00
after_create :after_created
after_update :after_updated
before_destroy :before_destroyed
def user_image
user.image.url
end
def as_json(_options = {})
super(methods: [:user_name, :user_image])
end
2016-12-14 15:08:59 +00:00
def after_created
if mappable_type == 'Topic'
2016-12-15 12:44:01 +00:00
meta = {'x': xloc, 'y': yloc, 'mapping_id': id}
Events::TopicAddedToMap.publish!(mappable, map, user, meta)
2016-12-14 15:08:59 +00:00
elsif mappable_type == 'Synapse'
2016-12-15 12:44:01 +00:00
Events::SynapseAddedToMap.publish!(mappable, map, user, meta)
2016-12-14 15:08:59 +00:00
end
end
def after_updated
if mappable_type == 'Topic' and (xloc_changed? or yloc_changed?)
2016-12-15 12:44:01 +00:00
meta = {'x': xloc, 'y': yloc, 'mapping_id': id}
Events::TopicMovedOnMap.publish!(mappable, map, user, meta)
2016-12-14 15:08:59 +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
2016-12-15 12:44:01 +00:00
meta = {'mapping_id': id}
2016-12-14 15:08:59 +00:00
if mappable_type == 'Topic'
2016-12-15 12:44:01 +00:00
Events::TopicRemovedFromMap.publish!(mappable, map, user, meta)
2016-12-14 15:08:59 +00:00
elsif mappable_type == 'Synapse'
2016-12-15 12:44:01 +00:00
Events::SynapseRemovedFromMap.publish!(mappable, map, user, meta)
2016-12-14 15:08:59 +00:00
end
end
end