tag.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Tag < ApplicationModel
  3. belongs_to :tag_object, class_name: 'Tag::Object'
  4. belongs_to :tag_item, class_name: 'Tag::Item'
  5. # rubocop:disable Style/ClassVars
  6. @@cache_item = {}
  7. @@cache_object = {}
  8. # rubocop:enable Style/ClassVars
  9. def self.tag_add(data)
  10. # lookups
  11. if data[:object]
  12. tag_object_id = tag_object_lookup( data[:object] )
  13. end
  14. if data[:item]
  15. tag_item_id = tag_item_lookup( data[:item] )
  16. end
  17. # create history
  18. Tag.create(
  19. tag_object_id: tag_object_id,
  20. tag_item_id: tag_item_id,
  21. o_id: data[:o_id],
  22. created_by_id: data[:created_by_id],
  23. )
  24. true
  25. end
  26. def self.tag_remove(data)
  27. # lookups
  28. if data[:object]
  29. tag_object_id = tag_object_lookup( data[:object] )
  30. end
  31. if data[:item]
  32. tag_item_id = tag_item_lookup( data[:item] )
  33. end
  34. # create history
  35. result = Tag.where(
  36. tag_object_id: tag_object_id,
  37. tag_item_id: tag_item_id,
  38. o_id: data[:o_id],
  39. )
  40. result.each(&:destroy)
  41. true
  42. end
  43. def self.tag_list( data )
  44. tag_object_id_requested = tag_object_lookup( data[:object] )
  45. tag_search = Tag.where(
  46. tag_object_id: tag_object_id_requested,
  47. o_id: data[:o_id],
  48. )
  49. tags = []
  50. tag_search.each {|tag|
  51. tags.push tag_item_lookup_id( tag.tag_item_id )
  52. }
  53. tags
  54. end
  55. def self.tag_item_lookup_id( id )
  56. # use cache
  57. return @@cache_item[ id ] if @@cache_item[ id ]
  58. # lookup
  59. tag_item = Tag::Item.find(id)
  60. @@cache_item[ id ] = tag_item.name
  61. tag_item.name
  62. end
  63. def self.tag_item_lookup( name )
  64. name = name.downcase
  65. # use cache
  66. return @@cache_item[ name ] if @@cache_item[ name ]
  67. # lookup
  68. tag_item = Tag::Item.find_by( name: name )
  69. if tag_item
  70. @@cache_item[ name ] = tag_item.id
  71. return tag_item.id
  72. end
  73. # create
  74. tag_item = Tag::Item.create(
  75. name: name
  76. )
  77. @@cache_item[ name ] = tag_item.id
  78. tag_item.id
  79. end
  80. def self.tag_object_lookup_id( id )
  81. # use cache
  82. return @@cache_object[ id ] if @@cache_object[ id ]
  83. # lookup
  84. tag_object = Tag::Object.find(id)
  85. @@cache_object[ id ] = tag_object.name
  86. tag_object.name
  87. end
  88. def self.tag_object_lookup( name )
  89. # use cache
  90. return @@cache_object[ name ] if @@cache_object[ name ]
  91. # lookup
  92. tag_object = Tag::Object.find_by( name: name )
  93. if tag_object
  94. @@cache_object[ name ] = tag_object.id
  95. return tag_object.id
  96. end
  97. # create
  98. tag_object = Tag::Object.create(
  99. name: name
  100. )
  101. @@cache_object[ name ] = tag_object.id
  102. tag_object.id
  103. end
  104. class Object < ActiveRecord::Base
  105. end
  106. class Item < ActiveRecord::Base
  107. end
  108. end