2012-10-24 06:47:08 +00:00
|
|
|
class Map < ActiveRecord::Base
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
has_many :topicmappings, :class_name => 'Mapping', :conditions => {:category => 'Topic'}
|
2012-10-26 10:04:52 +00:00
|
|
|
has_many :synapsemappings, :class_name => 'Mapping', :conditions => {:category => 'Synapse'}
|
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
has_many :topics, :through => :topicmappings
|
2012-10-26 10:04:52 +00:00
|
|
|
has_many :synapses, :through => :synapsemappings
|
|
|
|
|
|
|
|
def mappings
|
2013-01-01 22:45:35 +00:00
|
|
|
topicmappings + synapsemappings
|
2012-10-27 08:30:56 +00:00
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
|
|
|
def mk_permission
|
|
|
|
if self.permission == "commons"
|
|
|
|
"co"
|
|
|
|
elsif self.permission == "public"
|
|
|
|
"pu"
|
|
|
|
elsif self.permission == "private"
|
|
|
|
"pr"
|
|
|
|
end
|
|
|
|
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
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
###### JSON ######
|
2012-10-26 10:04:52 +00:00
|
|
|
#build a json object of a map
|
2012-10-27 08:30:56 +00:00
|
|
|
def self_as_json(current)
|
2012-10-26 10:04:52 +00:00
|
|
|
Jbuilder.encode do |json|
|
2013-01-01 22:45:35 +00:00
|
|
|
@topics = self.topics
|
2012-10-26 10:04:52 +00:00
|
|
|
@synapses = self.synapses
|
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
json.array!(@topics.delete_if{|topic| not topic.authorize_to_view(current)}) do |topic|
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
#json.adjacencies topic.synapses2.delete_if{|synapse| (not @topics.include?(synapse.topic1)) || (not @synapses.include?(synapse)) || (not synapse.authorize_to_view(current)) || (not synapse.topic1.authorize_to_view(current)) } do |json, synapse|
|
2012-10-30 16:15:45 +00:00
|
|
|
|
2013-01-06 04:42:22 +00:00
|
|
|
json.adjacencies topic.synapses1.delete_if{|synapse| (not @synapses.include?(synapse)) || (not @topics.include?(synapse.topic2)) || (not synapse.authorize_to_view(current)) || (not synapse.topic2.authorize_to_view(current)) } do |json, synapse|
|
2012-11-25 01:48:26 +00:00
|
|
|
json.nodeTo synapse.node2_id
|
|
|
|
json.nodeFrom synapse.node1_id
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = synapse.desc
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapsedata['$showDesc'] = false
|
2012-10-26 10:04:52 +00:00
|
|
|
@synapsedata['$category'] = synapse.category
|
2012-11-04 00:35:37 +00:00
|
|
|
@synapsedata['$id'] = synapse.id
|
2012-10-26 10:04:52 +00:00
|
|
|
@synapsedata['$userid'] = synapse.user.id
|
|
|
|
@synapsedata['$username'] = synapse.user.name
|
2012-11-25 01:48:26 +00:00
|
|
|
@synapsedata['$direction'] = [synapse.node1_id.to_s(), synapse.node2_id.to_s()]
|
2013-01-23 18:43:01 +00:00
|
|
|
@synapsedata['$permission'] = synapse.permission
|
2012-10-26 10:04:52 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
2012-12-22 08:32:12 +00:00
|
|
|
@inmaps = Array.new
|
2014-02-21 01:22:13 +00:00
|
|
|
@mapsString = ""
|
|
|
|
topic.maps.each_with_index do |map, index|
|
2012-12-22 08:32:12 +00:00
|
|
|
@inmaps.push(map.id)
|
2014-02-21 01:22:13 +00:00
|
|
|
@mapsString += map.name
|
|
|
|
@mapsString += (index+1) == topic.maps.count ? "" : ", "
|
2012-12-22 08:32:12 +00:00
|
|
|
end
|
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
@topicdata = Hash.new
|
|
|
|
@topicdata['$desc'] = topic.desc
|
|
|
|
@topicdata['$link'] = topic.link
|
|
|
|
@topicdata['$metacode'] = topic.metacode.name
|
|
|
|
@topicdata['$inmaps'] = @inmaps
|
2014-02-21 01:22:13 +00:00
|
|
|
@topicdata['$inmapsString'] = @mapsString
|
2014-02-01 08:57:19 +00:00
|
|
|
@topicdata['$synapseCount'] = topic.synapses.count
|
2013-01-01 22:45:35 +00:00
|
|
|
@topicdata['$userid'] = topic.user.id
|
|
|
|
@topicdata['$username'] = topic.user.name
|
|
|
|
@mapping = Mapping.find_by_topic_id_and_map_id(topic.id,self.id)
|
|
|
|
@topicdata['$xloc'] = @mapping.xloc
|
|
|
|
@topicdata['$yloc'] = @mapping.yloc
|
|
|
|
@topicdata['$mappingid'] = @mapping.id
|
2013-01-23 18:43:01 +00:00
|
|
|
@topicdata['$permission'] = topic.permission
|
2014-02-10 06:29:02 +00:00
|
|
|
@topicdata['$date'] = topic.created_at.strftime("%m/%d/%Y")
|
2013-01-01 22:45:35 +00:00
|
|
|
json.data @topicdata
|
|
|
|
json.id topic.id
|
|
|
|
json.name topic.name
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
##### PERMISSIONS ######
|
|
|
|
|
|
|
|
scope :visibleToUser, lambda { |current, user|
|
|
|
|
if user != nil
|
|
|
|
if user != current
|
|
|
|
Map.find_all_by_user_id_and_permission(user.id, "commons") | Map.find_all_by_user_id_and_permission(user.id, "public")
|
|
|
|
elsif user == current
|
|
|
|
Map.find_all_by_user_id_and_permission(user.id, "commons") | Map.find_all_by_user_id_and_permission(user.id, "public") | current.maps.where(:permission => "private")
|
|
|
|
end
|
|
|
|
elsif (current != nil && user == nil)
|
|
|
|
Map.find_all_by_permission("commons") | Map.find_all_by_permission("public") | current.maps.where(:permission => "private")
|
|
|
|
elsif (current == nil)
|
|
|
|
Map.find_all_by_permission("commons") | Map.find_all_by_permission("public")
|
|
|
|
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)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns false if user not allowed to 'edit' Topic, Synapse, or Map
|
|
|
|
def authorize_to_edit(user)
|
|
|
|
if (self.permission == "private" && self.user != user)
|
|
|
|
return false
|
|
|
|
elsif (self.permission == "public" && self.user != user)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns Boolean if user allowed to view Topic, Synapse, or Map
|
|
|
|
def authorize_to_view(user)
|
|
|
|
if (self.permission == "private" && self.user != user)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns Boolean based on whether user has permissions to edit or not
|
|
|
|
def authorize_linkto_edit(user)
|
|
|
|
if (self.user == user)
|
|
|
|
return true
|
|
|
|
elsif (self.permission == "commons")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-10-24 06:47:08 +00:00
|
|
|
end
|