trabajo-afectivo/app/models/tag.rb

138 lines
2.8 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2012-11-18 11:06:55 +00:00
class Tag < ApplicationModel
belongs_to :tag_object, class_name: 'Tag::Object'
belongs_to :tag_item, class_name: 'Tag::Item'
2012-11-18 11:06:55 +00:00
@@cache_item = {}
@@cache_object = {}
def self.tag_add(data)
2012-11-18 11:06:55 +00:00
# lookups
if data[:object]
tag_object_id = self.tag_object_lookup( data[:object] )
end
if data[:item]
tag_item_id = self.tag_item_lookup( data[:item] )
end
# create history
Tag.create(
tag_object_id: tag_object_id,
tag_item_id: tag_item_id,
o_id: data[:o_id],
created_by_id: data[:created_by_id],
2012-11-18 11:06:55 +00:00
)
return true
end
def self.tag_remove(data)
2012-11-18 11:06:55 +00:00
# lookups
if data[:object]
tag_object_id = self.tag_object_lookup( data[:object] )
end
if data[:item]
tag_item_id = self.tag_item_lookup( data[:item] )
end
# create history
result = Tag.where(
tag_object_id: tag_object_id,
tag_item_id: tag_item_id,
o_id: data[:o_id],
2012-11-18 11:06:55 +00:00
)
result.each { |item|
item.destroy
}
return true
end
def self.tag_list( data )
tag_object_id_requested = self.tag_object_lookup( data[:object] )
tag_search = Tag.where(
tag_object_id: tag_object_id_requested,
o_id: data[:o_id],
2012-11-18 11:06:55 +00:00
)
tags = []
tag_search.each {|tag|
tags.push self.tag_item_lookup_id( tag.tag_item_id )
2012-11-18 11:06:55 +00:00
}
return tags
end
private
def self.tag_item_lookup_id( id )
2012-11-18 11:06:55 +00:00
# use cache
return @@cache_item[ id ] if @@cache_item[ id ]
2012-11-18 11:06:55 +00:00
# lookup
tag_item = Tag::Item.find(id)
@@cache_item[ id ] = tag_item.name
return tag_item.name
end
2012-11-18 11:06:55 +00:00
def self.tag_item_lookup( name )
2012-11-18 11:09:37 +00:00
name = name.downcase
2012-11-18 11:06:55 +00:00
# use cache
return @@cache_item[ name ] if @@cache_item[ name ]
2012-11-18 11:06:55 +00:00
# lookup
tag_item = Tag::Item.where( name: name ).first
if tag_item
2012-11-18 11:06:55 +00:00
@@cache_item[ name ] = tag_item.id
return tag_item.id
end
# create
tag_item = Tag::Item.create(
name: name
)
@@cache_item[ name ] = tag_item.id
return tag_item.id
end
2012-11-18 11:06:55 +00:00
def self.tag_object_lookup_id( id )
2012-11-18 11:06:55 +00:00
# use cache
return @@cache_object[ id ] if @@cache_object[ id ]
2012-11-18 11:06:55 +00:00
# lookup
tag_object = Tag::Object.find(id)
@@cache_object[ id ] = tag_object.name
return tag_object.name
end
2012-11-18 11:06:55 +00:00
def self.tag_object_lookup( name )
2012-11-18 11:06:55 +00:00
# use cache
return @@cache_object[ name ] if @@cache_object[ name ]
2012-11-18 11:06:55 +00:00
# lookup
tag_object = Tag::Object.where( name: name ).first
if tag_object
2012-11-18 11:06:55 +00:00
@@cache_object[ name ] = tag_object.id
return tag_object.id
end
# create
tag_object = Tag::Object.create(
name: name
)
@@cache_object[ name ] = tag_object.id
return tag_object.id
end
2012-11-18 11:06:55 +00:00
class Object < ActiveRecord::Base
end
class Item < ActiveRecord::Base
end
end