trabajo-afectivo/app/models/link.rb

179 lines
4.7 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Link < ApplicationModel
belongs_to :link_type, class_name: 'Link::Type'
belongs_to :link_object, class_name: 'Link::Object'
2012-04-10 14:06:46 +00:00
@map = {
'normal' => 'normal',
'parent' => 'child',
'child' => 'parent',
}
=begin
2012-08-20 13:21:15 +00:00
2012-11-07 23:47:05 +00:00
links = Link.list(
2012-08-21 10:28:41 +00:00
:link_object => 'Ticket',
:link_object_value => 1
2012-08-20 13:21:15 +00:00
)
=end
2012-08-21 10:28:41 +00:00
def self.list(data)
linkobject = self.link_object_get( name: data[:link_object] )
2012-08-21 10:28:41 +00:00
return if !linkobject
items = []
# get links for one site
2012-08-21 10:28:41 +00:00
list = Link.where(
'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
)
list.each { |item|
link = {}
link['link_type'] = @map[ Link::Type.find( item.link_type_id ).name ]
2012-08-21 10:28:41 +00:00
link['link_object'] = Link::Object.find( item.link_object_target_id ).name
link['link_object_value'] = item.link_object_target_value
items.push link
}
# get links for the other site
2012-08-21 10:28:41 +00:00
list = Link.where(
'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
)
list.each { |item|
link = {}
link['link_type'] = Link::Type.find( item.link_type_id ).name
link['link_object'] = Link::Object.find( item.link_object_source_id ).name
link['link_object_value'] = item.link_object_source_value
items.push link
}
items
2012-08-21 10:28:41 +00:00
end
=begin
Link.add(
:link_type => 'normal',
:link_object_source => 'Ticket',
:link_object_source_value => 6,
:link_object_target => 'Ticket',
:link_object_target_value => 31
)
Link.add(
:link_types_id => 12,
:link_object_source_id => 1,
:link_object_source_value => 1,
:link_object_target_id => 1,
:link_object_target_value => 1
)
=end
2012-08-20 13:21:15 +00:00
def self.add(data)
if data.key?(:link_type)
linktype = self.link_type_get( name: data[:link_type] )
2012-08-21 10:28:41 +00:00
data[:link_type_id] = linktype.id
data.delete( :link_type )
end
if data.key?(:link_object_source)
linkobject = self.link_object_get( name: data[:link_object_source] )
2012-08-21 10:28:41 +00:00
data[:link_object_source_id] = linkobject.id
2012-08-20 13:21:15 +00:00
data.delete( :link_object_source )
end
2012-08-21 10:28:41 +00:00
if data.key?(:link_object_target)
linkobject = self.link_object_get( name: data[:link_object_target] )
2012-08-21 10:28:41 +00:00
data[:link_object_target_id] = linkobject.id
2012-08-20 13:21:15 +00:00
data.delete( :link_object_target )
end
Link.create(data)
end
=begin
2012-08-20 13:21:15 +00:00
Link.remove(
:link_type => 'normal',
:link_object_source => 'Ticket',
:link_object_source_value => 6,
:link_object_target => 'Ticket',
:link_object_target_value => 31
)
=end
def self.remove(data)
if data.key?(:link_object_source)
linkobject = self.link_object_get( name: data[:link_object_source] )
data[:link_object_source_id] = linkobject.id
end
if data.key?(:link_object_target)
linkobject = self.link_object_get( name: data[:link_object_target] )
data[:link_object_target_id] = linkobject.id
end
2012-08-20 13:21:15 +00:00
# from one site
if data.key?(:link_type)
linktype = self.link_type_get( name: data[:link_type] )
data[:link_type_id] = linktype.id
end
links = Link.where(
link_type_id: data[:link_type_id],
link_object_source_id: data[:link_object_source_id],
link_object_source_value: data[:link_object_source_value],
link_object_target_id: data[:link_object_target_id],
link_object_target_value: data[:link_object_target_value]
)
links.each(&:destroy)
# from the other site
if data.key?(:link_type)
linktype = self.link_type_get( name: @map[ data[:link_type] ] )
data[:link_type_id] = linktype.id
end
links = Link.where(
link_type_id: data[:link_type_id],
link_object_target_id: data[:link_object_source_id],
link_object_target_value: data[:link_object_source_value],
link_object_source_id: data[:link_object_target_id],
link_object_source_value: data[:link_object_target_value]
)
links.each(&:destroy)
2012-08-20 13:21:15 +00:00
end
def self.link_type_get(data)
linktype = Link::Type.find_by( name: data[:name] )
if !linktype
linktype = Link::Type.create(
name: data[:name]
)
2012-08-21 10:28:41 +00:00
end
linktype
end
2012-08-21 10:28:41 +00:00
def self.link_object_get(data)
linkobject = Link::Object.find_by( name: data[:name] )
if !linkobject
linkobject = Link::Object.create(
name: data[:name]
)
2012-08-21 10:28:41 +00:00
end
linkobject
end
2012-08-21 10:28:41 +00:00
end
class Link::Type < ApplicationModel
validates :name, presence: true
2012-04-10 14:06:46 +00:00
end
class Link::Object < ApplicationModel
validates :name, presence: true
end