2012-09-23 02:39:12 +00:00
|
|
|
class Item < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
has_many :itemitem_c, :foreign_key => 'parent_item_id', :class_name => 'Itemitem'
|
|
|
|
has_many :itemitem_p, :foreign_key => 'item_id', :class_name => 'Itemitem'
|
|
|
|
|
|
|
|
has_many :groupitems
|
|
|
|
has_many :personitems
|
|
|
|
|
|
|
|
has_many :groups, :through => :groupitems
|
|
|
|
has_many :people, :through => :personitems
|
|
|
|
|
2012-10-05 20:40:30 +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'}
|
|
|
|
has_many :items1, :through => :synapses2, :source => :item1
|
|
|
|
has_many :items2, :through => :synapses1, :source => :item2
|
|
|
|
|
2012-10-09 03:18:00 +00:00
|
|
|
def synapses
|
|
|
|
synapses1 + synapses2
|
|
|
|
end
|
|
|
|
|
|
|
|
def relatives
|
2012-10-05 20:40:30 +00:00
|
|
|
items1 + items2
|
|
|
|
end
|
|
|
|
|
2012-09-23 02:39:12 +00:00
|
|
|
belongs_to :item_category
|
|
|
|
|
|
|
|
has_many :child_items, :through => :itemitem_c, :source => :item
|
|
|
|
has_many :parent_items, :through => :itemitem_p, :source => :parent_item
|
|
|
|
|
2012-10-09 03:18:00 +00:00
|
|
|
def as_json
|
|
|
|
Jbuilder.encode do |json|
|
|
|
|
@data1 = {'$color'=> '#909291'}
|
|
|
|
@data2 = {'$color'=> '#70A35E', '$type'=> 'triangle', '$dim'=> 11 }
|
|
|
|
@single = Array.new
|
|
|
|
@single.push(self)
|
|
|
|
@items = @single + self.relatives
|
|
|
|
|
|
|
|
json.array!(@items) do |item|
|
2012-10-10 00:23:45 +00:00
|
|
|
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
|
|
|
|
json.data @data1
|
2012-10-09 03:18:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
json.data @data2
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|