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-26 10:04:52 +00:00
|
|
|
has_many :synapses1, :class_name => 'Synapse', :foreign_key => 'node1_id'
|
|
|
|
has_many :synapses2, :class_name => 'Synapse', :foreign_key => 'node2_id'
|
2012-10-10 15:52:50 +00:00
|
|
|
has_many :items1, :through => :synapses2, :source => :item1
|
|
|
|
has_many :items2, :through => :synapses1, :source => :item2
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
has_many :mappings
|
|
|
|
has_many :maps, :through => :mappings
|
2012-10-10 15:52:50 +00:00
|
|
|
|
|
|
|
def synapses
|
|
|
|
synapses1 + synapses2
|
|
|
|
end
|
|
|
|
|
|
|
|
def relatives
|
|
|
|
items1 + items2
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to :item_category
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
# has no viewable synapses helper function
|
|
|
|
def has_viewable_synapses(current)
|
|
|
|
result = false
|
|
|
|
self.synapses.each do |synapse|
|
|
|
|
if synapse.authorize_to_view(current)
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2012-12-01 23:05:17 +00:00
|
|
|
##### MAKE SEARCHABLE ######
|
|
|
|
def self.search(query)
|
|
|
|
self.where("(UPPER(name) LIKE UPPER(:query)", query: "%#{query}%").order(:name)
|
|
|
|
end
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
###### JSON ######
|
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-26 10:04:52 +00:00
|
|
|
@itemdata['$userid'] = self.user.id
|
|
|
|
@itemdata['$username'] = self.user.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-27 08:30:56 +00:00
|
|
|
def network_as_json(current)
|
2012-10-19 04:06:16 +00:00
|
|
|
Jbuilder.encode do |json|
|
2012-11-01 19:59:54 +00:00
|
|
|
@items = network(self,nil,4)
|
2012-10-19 04:06:16 +00:00
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
if @items.count > 1
|
|
|
|
json.array!(@items.delete_if{|item| (not item.authorize_to_view(current)) || (not item.has_viewable_synapses(current))}) do |item|
|
|
|
|
|
2012-11-25 01:48:26 +00:00
|
|
|
json.adjacencies item.synapses1.delete_if{|synapse| (not @items.include?(synapse.item2)) || (not synapse.authorize_to_view(current)) || (not synapse.item2.authorize_to_view(current)) } do |json, synapse|
|
|
|
|
json.nodeTo synapse.node2_id
|
|
|
|
json.nodeFrom synapse.node1_id
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = synapse.desc
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapsedata['$showDesc'] = false
|
2012-10-27 08:30:56 +00:00
|
|
|
@synapsedata['$category'] = synapse.category
|
2012-11-04 00:35:37 +00:00
|
|
|
@synapsedata['$id'] = synapse.id
|
2012-10-27 08:30:56 +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()]
|
2012-10-27 08:30:56 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
|
|
|
@itemdata['$userid'] = item.user.id
|
|
|
|
@itemdata['$username'] = item.user.name
|
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
2012-10-19 04:06:16 +00:00
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
elsif @items.count == 1
|
|
|
|
json.array!(@items) do |item|
|
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
|
|
|
@itemdata['$userid'] = item.user.id
|
|
|
|
@itemdata['$username'] = item.user.name
|
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
2012-10-19 04:06:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
##### PERMISSIONS ######
|
|
|
|
|
|
|
|
scope :visibleToUser, lambda { |current, user|
|
|
|
|
if user != nil
|
|
|
|
if user != current
|
|
|
|
Item.find_all_by_user_id_and_permission(user.id, "commons") | Item.find_all_by_user_id_and_permission(user.id, "public")
|
|
|
|
elsif user == current
|
|
|
|
Item.find_all_by_user_id_and_permission(user.id, "commons") | Item.find_all_by_user_id_and_permission(user.id, "public") | current.items.where(:permission => "private")
|
|
|
|
end
|
|
|
|
elsif (current != nil && user == nil)
|
|
|
|
Item.find_all_by_permission("commons") | Item.find_all_by_permission("public") | current.items.where(:permission => "private")
|
|
|
|
elsif (current == nil)
|
|
|
|
Item.find_all_by_permission("commons") | Item.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
|
2012-10-10 15:52:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|