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
|
|
|
|
|
|
|
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
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|