2016-09-21 17:22:40 +00:00
|
|
|
class Map < ApplicationRecord
|
2014-08-12 22:14:04 +00:00
|
|
|
belongs_to :user
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2015-10-01 03:02:39 +00:00
|
|
|
has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy
|
|
|
|
has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy
|
2016-07-26 00:14:23 +00:00
|
|
|
has_many :topics, through: :topicmappings, source: :mappable, source_type: 'Topic'
|
|
|
|
has_many :synapses, through: :synapsemappings, source: :mappable, source_type: 'Synapse'
|
2015-12-11 19:23:41 +00:00
|
|
|
has_many :messages, as: :resource, dependent: :destroy
|
2016-08-31 20:58:49 +00:00
|
|
|
has_many :stars
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
has_many :user_maps, dependent: :destroy
|
|
|
|
has_many :collaborators, through: :user_maps, source: :user
|
|
|
|
|
2016-03-12 23:36:38 +00:00
|
|
|
has_many :webhooks, as: :hookable
|
|
|
|
has_many :events, -> { includes :user }, as: :eventable, dependent: :destroy
|
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
2016-07-26 00:14:23 +00:00
|
|
|
has_attached_file :screenshot, styles: {
|
|
|
|
thumb: ['188x126#', :png]
|
|
|
|
#:full => ['940x630#', :png]
|
2014-10-08 04:07:54 +00:00
|
|
|
},
|
2016-08-31 22:57:19 +00:00
|
|
|
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/missing-map-white.png'
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2015-12-17 01:10:52 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :arranged, inclusion: { in: [true, false] }
|
|
|
|
validates :permission, presence: true
|
2015-12-17 01:16:02 +00:00
|
|
|
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
2016-01-22 01:59:09 +00:00
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
2016-07-26 00:14:23 +00:00
|
|
|
validates_attachment_content_type :screenshot, content_type: /\Aimage\/.*\Z/
|
2014-08-15 22:04:22 +00:00
|
|
|
|
2015-12-22 18:16:03 +00:00
|
|
|
def mappings
|
2016-07-26 00:14:23 +00:00
|
|
|
topicmappings + synapsemappings
|
2014-08-12 22:14:04 +00:00
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
def mk_permission
|
2015-12-17 01:03:51 +00:00
|
|
|
Perm.short(permission)
|
2013-01-18 22:08:06 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
# return an array of the contributors to the map
|
2014-01-29 03:46:58 +00:00
|
|
|
def contributors
|
|
|
|
contributors = []
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
mappings.each do |m|
|
|
|
|
contributors.push(m.user) unless contributors.include?(m.user)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
contributors
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-08-12 15:09:53 +00:00
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
def editors
|
2016-07-26 00:14:23 +00:00
|
|
|
collaborators + [user]
|
2016-04-24 15:50:35 +00:00
|
|
|
end
|
|
|
|
|
2014-08-12 15:09:53 +00:00
|
|
|
def topic_count
|
2016-02-09 03:48:07 +00:00
|
|
|
topics.length
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def synapse_count
|
2016-02-09 03:48:07 +00:00
|
|
|
synapses.length
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
delegate :name, to: :user, prefix: true
|
2014-08-12 15:09:53 +00:00
|
|
|
|
2014-08-12 16:01:01 +00:00
|
|
|
def user_image
|
2016-02-09 03:48:07 +00:00
|
|
|
user.image.url
|
2014-08-12 16:01:01 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def contributor_count
|
2016-02-09 03:48:07 +00:00
|
|
|
contributors.length
|
2014-08-15 22:04:22 +00:00
|
|
|
end
|
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
def collaborator_ids
|
|
|
|
collaborators.map(&:id)
|
|
|
|
end
|
|
|
|
|
2014-10-08 04:07:54 +00:00
|
|
|
def screenshot_url
|
2016-02-09 03:48:07 +00:00
|
|
|
screenshot.url(:thumb)
|
2014-10-08 04:07:54 +00:00
|
|
|
end
|
|
|
|
|
2014-08-27 02:51:50 +00:00
|
|
|
def created_at_str
|
2016-07-26 00:14:23 +00:00
|
|
|
created_at.strftime('%m/%d/%Y')
|
2014-08-27 02:51:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at_str
|
2016-07-26 00:14:23 +00:00
|
|
|
updated_at.strftime('%m/%d/%Y')
|
2014-08-27 02:51:50 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
|
|
|
json = super(methods: [:user_name, :user_image, :topic_count, :synapse_count, :contributor_count, :collaborator_ids, :screenshot_url], except: [:screenshot_content_type, :screenshot_file_size, :screenshot_file_name, :screenshot_updated_at])
|
2016-02-09 03:48:07 +00:00
|
|
|
json[:created_at_clean] = created_at_str
|
|
|
|
json[:updated_at_clean] = updated_at_str
|
2014-08-27 02:51:50 +00:00
|
|
|
json
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
2014-10-07 23:11:55 +00:00
|
|
|
|
2014-10-08 04:07:54 +00:00
|
|
|
def decode_base64(imgBase64)
|
|
|
|
decoded_data = Base64.decode64(imgBase64)
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2014-10-08 04:07:54 +00:00
|
|
|
data = StringIO.new(decoded_data)
|
|
|
|
data.class_eval do
|
|
|
|
attr_accessor :content_type, :original_filename
|
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
data.content_type = 'image/png'
|
|
|
|
data.original_filename = File.basename('map-' + id.to_s + '-screenshot.png')
|
2014-10-08 04:07:54 +00:00
|
|
|
|
|
|
|
self.screenshot = data
|
2016-07-26 00:14:23 +00:00
|
|
|
save
|
2014-10-08 04:07:54 +00:00
|
|
|
end
|
2012-10-24 06:47:08 +00:00
|
|
|
end
|