2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
class Tag < ApplicationModel
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :tag_object, class_name: 'Tag::Object'
|
|
|
|
belongs_to :tag_item, class_name: 'Tag::Item'
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2015-05-08 13:47:27 +00:00
|
|
|
# rubocop:disable Style/ClassVars
|
2012-11-18 11:06:55 +00:00
|
|
|
@@cache_item = {}
|
|
|
|
@@cache_object = {}
|
2015-05-08 13:47:27 +00:00
|
|
|
# rubocop:enable Style/ClassVars
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.tag_add(data)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_object_id = tag_object_lookup(data[:object])
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
if data[:item]
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_item_id = tag_item_lookup(data[:item].strip)
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
# return in duplicate
|
|
|
|
current_tags = tag_list(data)
|
2016-06-02 06:58:10 +00:00
|
|
|
return true if current_tags.include?(data[:item].strip)
|
2016-05-03 00:36:44 +00:00
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
# create history
|
|
|
|
Tag.create(
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
)
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.tag_remove(data)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_object_id = tag_object_lookup(data[:object])
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
if data[:item]
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_item_id = tag_item_lookup(data[:item].strip)
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
result = Tag.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
)
|
2015-05-07 10:25:16 +00:00
|
|
|
result.each(&:destroy)
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_list(data)
|
|
|
|
tag_object_id_requested = tag_object_lookup(data[:object])
|
2012-11-18 11:06:55 +00:00
|
|
|
tag_search = Tag.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
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|
|
2016-05-03 00:36:44 +00:00
|
|
|
tags.push tag_item_lookup_id(tag.tag_item_id)
|
2012-11-18 11:06:55 +00:00
|
|
|
}
|
2015-04-30 17:20:27 +00:00
|
|
|
tags
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_item_lookup_id(id)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
2016-05-03 00:36:44 +00:00
|
|
|
return @@cache_item[id] if @@cache_item[id]
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
tag_item = Tag::Item.find(id)
|
2016-05-03 00:36:44 +00:00
|
|
|
@@cache_item[id] = tag_item.name
|
2015-04-30 17:20:27 +00:00
|
|
|
tag_item.name
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_item_lookup(name)
|
2012-11-18 11:09:37 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
2016-05-03 00:36:44 +00:00
|
|
|
return @@cache_item[name] if @@cache_item[name]
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
2016-06-02 06:58:10 +00:00
|
|
|
tag_items = Tag::Item.where(name: name)
|
|
|
|
tag_items.each {|tag_item|
|
|
|
|
next if tag_item.name != name
|
2016-05-03 00:36:44 +00:00
|
|
|
@@cache_item[name] = tag_item.id
|
2012-11-18 11:06:55 +00:00
|
|
|
return tag_item.id
|
2016-06-02 06:58:10 +00:00
|
|
|
}
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_item = Tag::Item.create(name: name)
|
|
|
|
@@cache_item[name] = tag_item.id
|
2015-04-30 17:20:27 +00:00
|
|
|
tag_item.id
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_object_lookup_id(id)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
2016-05-03 00:36:44 +00:00
|
|
|
return @@cache_object[id] if @@cache_object[id]
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
tag_object = Tag::Object.find(id)
|
2016-05-03 00:36:44 +00:00
|
|
|
@@cache_object[id] = tag_object.name
|
2015-04-30 17:20:27 +00:00
|
|
|
tag_object.name
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_object_lookup(name)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
2016-05-03 00:36:44 +00:00
|
|
|
return @@cache_object[name] if @@cache_object[name]
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_object = Tag::Object.find_by(name: name)
|
2013-06-12 15:59:58 +00:00
|
|
|
if tag_object
|
2016-05-03 00:36:44 +00:00
|
|
|
@@cache_object[name] = tag_object.id
|
2012-11-18 11:06:55 +00:00
|
|
|
return tag_object.id
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
2016-05-03 00:36:44 +00:00
|
|
|
tag_object = Tag::Object.create(name: name)
|
|
|
|
@@cache_object[name] = tag_object.id
|
2015-04-30 17:20:27 +00:00
|
|
|
tag_object.id
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
class Object < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class Item < ActiveRecord::Base
|
2016-06-02 06:58:10 +00:00
|
|
|
before_save :fill_namedowncase
|
|
|
|
|
|
|
|
def fill_namedowncase
|
|
|
|
self.name_downcase = name.downcase
|
|
|
|
end
|
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|