trabajo-afectivo/app/models/link.rb

122 lines
3.2 KiB
Ruby
Raw Normal View History

2012-04-10 14:06:46 +00:00
class Link < ActiveRecord::Base
2012-08-20 13:21:15 +00:00
has_many :link_types
2012-04-10 14:06:46 +00:00
#before_create :check_object
#after_create :after_create, :cache_delete
#after_update :cache_delete
#after_destroy :cache_delete
=begin
2012-08-20 13:21:15 +00:00
Link.add(
:link_types_id => 12,
:link_object_source => 'Ticket',
:link_object_source_value => 1,
:link_object_target => 'Ticket',
:link_object_target_value => 1
)
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,
)
2012-08-20 13:21:15 +00:00
Link.get_links_for_source_object(
:link_object => 'Ticket',
:link_object_value => 1
)
2012-08-20 13:21:15 +00:00
Link.get_links_for_target_object(
:link_object => 'Ticket',
:link_object_value => 1,
)
2012-08-20 13:21:15 +00:00
Link.delete_link_by_source( :source_value => 1 )
2012-08-20 13:21:15 +00:00
Link.delete_link_by_target( :target_value => 1 )
2012-08-20 13:21:15 +00:00
Link.delete_all_links_by_value( :object_value => 1 )
=end
2012-08-20 13:21:15 +00:00
def self.get_links_for_source_object(data)
linkobject_id = self.get_linktype_by_name( :name => data[:link_object] )
if linkobject_id
where( :link_object_source_id => linkobject_id, :link_object_source_value => data[:link_object_value] )
end
2012-08-20 13:21:15 +00:00
end
2012-08-20 13:21:15 +00:00
def self.get_links_for_target_object(data)
linkobject_id = self.get_linktype_by_name( :name => data[:link_object] )
if linkobject_id
where( :link_object_target_id => linkobject_id, :link_object_target_value => data[:link_object_value] )
end
2012-08-20 13:21:15 +00:00
end
def self.add(data)
if data.has_key?(:link_object_source)
2012-08-20 13:21:15 +00:00
# it exists we have to delete it
linkobject_id = self.get_linktype_by_name( :name => data[:link_object_source] )
data[:link_object_source_id] = linkobject_id
data.delete( :link_object_source )
end
2012-08-20 13:21:15 +00:00
if data.has_key?(:link_object_target)
# it exists we have to delete it
linkobject_id = self.get_linktype_by_name( :name => data[:link_object_target] )
data[:link_object_target_id] = linkobject_id
data.delete( :link_object_target )
end
Link.create(data)
end
def self.delete_link_by_source(data)
Link.where( :link_object_source_value => data[:source_value] ).destroy_all
end
def self.delete_link_by_target(data)
Link.where( :link_object_target_value => data[:target_value] ).destroy_all
end
def self.delete_all_links_by_value(data)
Link.where( ["link_object_source_value = ? or link_object_target_value = ?", data[:object_value], data[:object_value]] ).destroy_all
end
private
def self.get_linktype_by_name(data)
2012-08-20 13:21:15 +00:00
linkid = Link::Object.where(:name=>data[:name]).first
if linkid
return linkid.id
else
return nil
end
return linkid
end
#checks for a valid link type
def check_valid_link_type
2012-08-20 13:21:15 +00:00
Rails.logger.info "Logger Test"
puts "pre check link type"
end
def get_linkobject_by_key
2012-08-20 13:21:15 +00:00
puts "check for exisiting link"
end
2012-08-20 13:21:15 +00:00
#checks for an exisiting ling
def check_existing_link
puts "check for exisiting link"
end
end
class Link::Type < ActiveRecord::Base
2012-04-10 14:06:46 +00:00
end
class Link::Object < ActiveRecord::Base
end