2012-10-10 15:52:50 +00:00
|
|
|
class Item < ActiveRecord::Base
|
2012-10-19 21:26:46 +00:00
|
|
|
include ItemsHelper
|
2012-10-10 15:52:50 +00:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
2012-10-18 00:51:54 +00:00
|
|
|
has_many :synapses1, :class_name => 'Synapse', :foreign_key => 'node1_id' #, :conditions => {:category => 'Item'}
|
|
|
|
has_many :synapses2, :class_name => 'Synapse', :foreign_key => 'node2_id' #, :conditions => {:category => 'Item'}
|
2012-10-10 15:52:50 +00:00
|
|
|
has_many :items1, :through => :synapses2, :source => :item1
|
|
|
|
has_many :items2, :through => :synapses1, :source => :item2
|
|
|
|
|
|
|
|
def synapses
|
|
|
|
synapses1 + synapses2
|
|
|
|
end
|
|
|
|
|
|
|
|
def relatives
|
|
|
|
items1 + items2
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to :item_category
|
|
|
|
|
2012-10-19 04:06:16 +00:00
|
|
|
def self_as_json
|
2012-10-10 15:52:50 +00:00
|
|
|
Jbuilder.encode do |json|
|
2012-10-19 04:06:16 +00:00
|
|
|
|
|
|
|
@itemdata = Hash.new
|
2012-10-21 17:19:59 +00:00
|
|
|
@itemdata['$desc'] = self.desc
|
|
|
|
@itemdata['$link'] = self.link
|
|
|
|
@itemdata['$itemcatname'] = self.item_category.name
|
2012-10-19 04:06:16 +00:00
|
|
|
json.data @itemdata
|
2012-10-21 17:19:59 +00:00
|
|
|
json.id self.id
|
|
|
|
json.name self.name
|
2012-10-19 04:06:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
#build a json object of everything connected to a specified node
|
2012-10-19 04:06:16 +00:00
|
|
|
def map_as_json
|
|
|
|
Jbuilder.encode do |json|
|
2012-10-19 21:26:46 +00:00
|
|
|
@items = network(self,nil)
|
2012-10-19 04:06:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_as_json
|
|
|
|
Jbuilder.encode do |json|
|
|
|
|
|
2012-10-18 00:51:54 +00:00
|
|
|
@items = Item.all
|
2012-10-10 15:52:50 +00:00
|
|
|
|
|
|
|
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
|
2012-10-18 00:51:54 +00:00
|
|
|
@synapsedata['$desc'] = synapse.desc
|
|
|
|
@synapsedata['$category'] = synapse.category
|
2012-10-10 15:52:50 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
|
|
|
@itemdata = Hash.new
|
2012-10-18 00:51:54 +00:00
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
2012-10-10 15:52:50 +00:00
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|