2012-10-24 06:47:08 +00:00
|
|
|
class Map < ActiveRecord::Base
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
belongs_to :user
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
has_many :topicmappings, :class_name => 'Mapping', :conditions => {:category => 'Topic'}
|
|
|
|
has_many :synapsemappings, :class_name => 'Mapping', :conditions => {:category => 'Synapse'}
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
has_many :topics, :through => :topicmappings
|
|
|
|
has_many :synapses, :through => :synapsemappings
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2014-10-07 23:11:55 +00:00
|
|
|
after_touch :save_screenshot
|
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
|
|
|
has_attached_file :screenshot, :styles => {
|
|
|
|
:thumb => ['188x126#', :png],
|
|
|
|
:full => ['1880x1260#', :png]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
|
|
|
validates_attachment_content_type :screenshot, :content_type => /\Aimage\/.*\Z/
|
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
def mappings
|
|
|
|
topicmappings + synapsemappings
|
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
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
|
2013-01-18 22:08:06 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
|
|
|
|
#return an array of the contributors to the map
|
|
|
|
def contributors
|
|
|
|
contributors = []
|
|
|
|
|
|
|
|
self.mappings.each do |m|
|
|
|
|
contributors.push(m.user) if !contributors.include?(m.user)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-08-12 16:01:01 +00:00
|
|
|
def user_image
|
|
|
|
self.user.image.url
|
|
|
|
end
|
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
def contributor_count
|
|
|
|
self.contributors.length
|
|
|
|
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={})
|
2014-08-27 02:51:50 +00:00
|
|
|
json = super(:methods =>[:user_name, :user_image, :topic_count, :synapse_count, :contributor_count], :except => [:created_at, :updated_at])
|
|
|
|
json[:created_at] = self.created_at_str
|
|
|
|
json[:updated_at] = self.updated_at_str
|
|
|
|
json
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
2014-10-07 23:11:55 +00:00
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
##### PERMISSIONS ######
|
|
|
|
|
|
|
|
# returns false if user not allowed to 'show' Topic, Synapse, or Map
|
|
|
|
def authorize_to_show(user)
|
2014-08-12 22:14:04 +00:00
|
|
|
if (self.permission == "private" && self.user != user)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return self
|
2012-10-27 08:30:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns false if user not allowed to 'edit' Topic, Synapse, or Map
|
|
|
|
def authorize_to_edit(user)
|
2014-08-12 22:14:04 +00:00
|
|
|
if (self.permission == "private" && self.user != user)
|
|
|
|
return false
|
|
|
|
elsif (self.permission == "public" && self.user != user)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return self
|
2012-10-27 08:30:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns Boolean if user allowed to view Topic, Synapse, or Map
|
|
|
|
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
|
2012-10-27 08:30:56 +00:00
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2014-10-07 23:11:55 +00:00
|
|
|
def save_screenshot
|
|
|
|
# TODO - this will grab a map every single frickin' time a map is touched
|
|
|
|
# we need a system to throttle the amount to 1/hour or something like that
|
|
|
|
# maybe have a flag - last time this map was screenshotted
|
|
|
|
# don't update if it was less than an hour ago
|
|
|
|
# except this has the issue of a user updating map 7x, and it only screenshotting after
|
|
|
|
# the first time. We only want it to screenhsot the 7th time.
|
|
|
|
# We need to store a timestamp somewhere and do processing every hour, I think.
|
|
|
|
GrabMapScreenshotWorker.perform_async(self.id)
|
|
|
|
end
|
|
|
|
|
2012-10-24 06:47:08 +00:00
|
|
|
end
|