2012-10-05 20:40:30 +00:00
|
|
|
class Synapse < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
belongs_to :item1, :class_name => "Item", :foreign_key => "node1_id"
|
|
|
|
belongs_to :item2, :class_name => "Item", :foreign_key => "node2_id"
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
has_many :mappings
|
|
|
|
has_many :maps, :through => :mappings
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
##### JSON ######
|
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
def self_as_json
|
2012-10-24 06:47:08 +00:00
|
|
|
Jbuilder.encode do |json|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = self.desc
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapsedata['$showDesc'] = false
|
2012-10-24 06:47:08 +00:00
|
|
|
@synapsedata['$category'] = self.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-10-24 06:47:08 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def selfplusnodes_as_json
|
2012-10-19 21:26:46 +00:00
|
|
|
Jbuilder.encode do |json|
|
|
|
|
@items = Array.new
|
|
|
|
@items.push(self.item1)
|
|
|
|
@items.push(self.item2)
|
|
|
|
|
|
|
|
json.array!(@items) do |item|
|
|
|
|
json.adjacencies item.synapses2.delete_if{|synapse| not @items.include?(Item.find_by_id(synapse.node1_id))} do |json, synapse|
|
|
|
|
json.nodeTo synapse.node1_id
|
|
|
|
json.nodeFrom synapse.node2_id
|
|
|
|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = synapse.desc
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapsedata['$showDesc'] = false
|
2012-10-19 21:26:46 +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-10-19 21:26:46 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
2012-10-26 10:04:52 +00:00
|
|
|
@itemdata['$userid'] = self.user.id
|
|
|
|
@itemdata['$username'] = self.user.name
|
2012-10-19 21:26:46 +00:00
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
##### PERMISSIONS ######
|
|
|
|
|
|
|
|
scope :visibleToUser, lambda { |current, user|
|
|
|
|
if user != nil
|
|
|
|
if user != current
|
|
|
|
Synapse.find_all_by_user_id_and_permission(user.id, "commons") | Synapse.find_all_by_user_id_and_permission(user.id, "public")
|
|
|
|
elsif user == current
|
|
|
|
Synapse.find_all_by_user_id_and_permission(user.id, "commons") | Synapse.find_all_by_user_id_and_permission(user.id, "public") | current.synapses.where(:permission => "private")
|
|
|
|
end
|
|
|
|
elsif (current != nil && user == nil)
|
|
|
|
Synapse.find_all_by_permission("commons") | Synapse.find_all_by_permission("public") | current.synapses.where(:permission => "private")
|
|
|
|
elsif (current == nil)
|
|
|
|
Synapse.find_all_by_permission("commons") | Synapse.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-19 21:26:46 +00:00
|
|
|
|
2012-10-05 20:40:30 +00:00
|
|
|
end
|