tag.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. @@cache_item = {}
  6. @@cache_object = {}
  7. def self.tag_add(data)
  8. # lookups
  9. if data[:object]
  10. tag_object_id = self.tag_object_lookup( data[:object] )
  11. end
  12. if data[:item]
  13. tag_item_id = self.tag_item_lookup( data[:item] )
  14. end
  15. # create history
  16. Tag.create(
  17. :tag_object_id => tag_object_id,
  18. :tag_item_id => tag_item_id,
  19. :o_id => data[:o_id],
  20. :created_by_id => data[:created_by_id],
  21. )
  22. return true
  23. end
  24. def self.tag_remove(data)
  25. # lookups
  26. if data[:object]
  27. tag_object_id = self.tag_object_lookup( data[:object] )
  28. end
  29. if data[:item]
  30. tag_item_id = self.tag_item_lookup( data[:item] )
  31. end
  32. # create history
  33. result = Tag.where(
  34. :tag_object_id => tag_object_id,
  35. :tag_item_id => tag_item_id,
  36. :o_id => data[:o_id],
  37. )
  38. result.each { |item|
  39. item.destroy
  40. }
  41. return true
  42. end
  43. def self.tag_list( data )
  44. tag_object_id_requested = self.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 self.tag_item_lookup_id( tag.tag_item_id )
  52. }
  53. return tags
  54. end
  55. private
  56. def self.tag_item_lookup_id( id )
  57. # use cache
  58. return @@cache_item[ id ] if @@cache_item[ id ]
  59. # lookup
  60. tag_item = Tag::Item.find(id)
  61. @@cache_item[ id ] = tag_item.name
  62. return tag_item.name
  63. end
  64. def self.tag_item_lookup( name )
  65. name = name.downcase
  66. # use cache
  67. return @@cache_item[ name ] if @@cache_item[ name ]
  68. # lookup
  69. tag_item = Tag::Item.where( :name => name ).first
  70. if tag_item
  71. @@cache_item[ name ] = tag_item.id
  72. return tag_item.id
  73. end
  74. # create
  75. tag_item = Tag::Item.create(
  76. :name => name
  77. )
  78. @@cache_item[ name ] = tag_item.id
  79. return tag_item.id
  80. end
  81. def self.tag_object_lookup_id( id )
  82. # use cache
  83. return @@cache_object[ id ] if @@cache_object[ id ]
  84. # lookup
  85. tag_object = Tag::Object.find(id)
  86. @@cache_object[ id ] = tag_object.name
  87. return tag_object.name
  88. end
  89. def self.tag_object_lookup( name )
  90. # use cache
  91. return @@cache_object[ name ] if @@cache_object[ name ]
  92. # lookup
  93. tag_object = Tag::Object.where( :name => name ).first
  94. if tag_object
  95. @@cache_object[ name ] = tag_object.id
  96. return tag_object.id
  97. end
  98. # create
  99. tag_object = Tag::Object.create(
  100. :name => name
  101. )
  102. @@cache_object[ name ] = tag_object.id
  103. return tag_object.id
  104. end
  105. class Object < ActiveRecord::Base
  106. end
  107. class Item < ActiveRecord::Base
  108. end
  109. end