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-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
|
|
|
|
@synapsedata['$category'] = self.category
|
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
|
|
|
|
@synapsedata['$category'] = synapse.category
|
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-05 20:40:30 +00:00
|
|
|
end
|