metamaps--metamaps/app/models/map.rb

138 lines
3.3 KiB
Ruby
Raw Normal View History

class Map < ActiveRecord::Base
2014-08-12 22:14:04 +00:00
belongs_to :user
has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy
has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy
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
# This method associates the attribute ":image" with a file attachment
has_attached_file :screenshot, :styles => {
:thumb => ['188x126#', :png]
#:full => ['940x630#', :png]
2014-10-08 04:07:54 +00:00
},
:default_url => 'https://s3.amazonaws.com/metamaps-assets/site/missing-map.png'
2015-12-22 18:16:03 +00:00
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :screenshot, :content_type => /\Aimage\/.*\Z/
2015-12-22 18:16:03 +00:00
def mappings
2014-08-12 22:14:04 +00:00
topicmappings + synapsemappings
end
2014-08-12 22:14:04 +00:00
def mk_permission
if self.permission == "commons"
"co"
elsif self.permission == "public"
"pu"
elsif self.permission == "private"
"pr"
end
end
#return an array of the contributors to the map
def contributors
contributors = []
2015-12-22 18:16:03 +00:00
self.mappings.each do |m|
contributors.push(m.user) if !contributors.include?(m.user)
end
2015-12-22 18:16:03 +00:00
return contributors
end
2014-08-12 15:09:53 +00:00
def topic_count
self.topics.length
end
def synapse_count
self.synapses.length
end
def user_name
self.user.name
end
def user_image
self.user.image.url
end
2015-12-22 18:16:03 +00:00
def contributor_count
self.contributors.length
end
2014-10-08 04:07:54 +00:00
def screenshot_url
self.screenshot.url(:thumb)
end
2014-08-27 02:51:50 +00:00
def created_at_str
self.created_at.strftime("%m/%d/%Y")
end
def updated_at_str
self.updated_at.strftime("%m/%d/%Y")
end
2014-08-12 15:09:53 +00:00
def as_json(options={})
json = super(:methods =>[:user_name, :user_image, :topic_count, :synapse_count, :contributor_count, :screenshot_url], :except => [:screenshot_content_type, :screenshot_file_size, :screenshot_file_name, :screenshot_updated_at])
json[:created_at_clean] = self.created_at_str
json[:updated_at_clean] = self.updated_at_str
2014-08-27 02:51:50 +00:00
json
2014-08-12 15:09:53 +00:00
end
##### PERMISSIONS ######
2015-12-22 18:16:03 +00:00
def authorize_to_delete(user)
if (self.user != user)
return false
end
return self
end
# returns false if user not allowed to 'show' Topic, Synapse, or Map
def authorize_to_show(user)
if (self.permission == "private" && self.user != user)
2014-08-12 22:14:04 +00:00
return false
end
return self
end
2015-12-22 18:16:03 +00:00
# returns false if user not allowed to 'edit' Topic, Synapse, or Map
2015-12-22 18:16:03 +00:00
def authorize_to_edit(user)
if !user
return false
elsif (self.permission == "private" && self.user != user)
2014-08-12 22:14:04 +00:00
return false
elsif (self.permission == "public" && self.user != user)
return false
end
return self
end
2015-12-22 18:16:03 +00:00
# returns Boolean if user allowed to view Topic, Synapse, or Map
2015-12-22 18:16:03 +00:00
def authorize_to_view(user)
2014-08-12 22:14:04 +00:00
if (self.permission == "private" && self.user != user)
return false
end
return true
end
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
data.content_type = "image/png"
data.original_filename = File.basename('map-' + self.id.to_s + '-screenshot.png')
2014-10-08 04:07:54 +00:00
self.screenshot = data
self.save
end
end