search_index_base.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::SearchIndexBase
  3. =begin
  4. collect data to index and send to backend
  5. ticket = Ticket.find(123)
  6. result = ticket.search_index_update_backend
  7. returns
  8. result = true # false
  9. =end
  10. def search_index_update_backend
  11. return if !self.class.search_index_support_config
  12. # default ignored attributes
  13. ignore_attributes = {
  14. :created_by_id => true,
  15. :updated_by_id => true,
  16. :active => true,
  17. }
  18. if self.class.search_index_support_config[:ignore_attributes]
  19. self.class.search_index_support_config[:ignore_attributes].each {|key, value|
  20. ignore_attributes[key] = value
  21. }
  22. end
  23. # remove ignored attributes
  24. attributes = self.attributes
  25. ignore_attributes.each {|key, value|
  26. next if value != true
  27. attributes.delete( key.to_s )
  28. }
  29. # fill up with search data
  30. attributes = search_index_attribute_lookup(attributes, self)
  31. return if !attributes
  32. # update backend
  33. if self.class.column_names.include? 'active'
  34. if self.active
  35. SearchIndexBackend.add( self.class.to_s, attributes )
  36. else
  37. SearchIndexBackend.remove( self.class.to_s, self.id )
  38. end
  39. else
  40. SearchIndexBackend.add( self.class.to_s, attributes )
  41. end
  42. end
  43. =begin
  44. get data to store in search index
  45. ticket = Ticket.find(123)
  46. result = ticket.search_index_data
  47. returns
  48. result = true # false
  49. =end
  50. def search_index_data
  51. attributes = {}
  52. ['name', 'note'].each { |key|
  53. if self[key] && !self[key].empty?
  54. attributes[key] = self[key]
  55. end
  56. }
  57. return if attributes.empty?
  58. attributes
  59. end
  60. private
  61. =begin
  62. lookup name of ref. objects
  63. attributes = search_index_attribute_lookup(attributes, Ticket)
  64. returns
  65. attributes # object with lookup data
  66. =end
  67. def search_index_attribute_lookup(attributes, ref_object)
  68. # default keep attributes
  69. keep_attributes = {}
  70. if self.class.search_index_support_config[:keep_attributes]
  71. self.class.search_index_support_config[:keep_attributes].each {|key, value|
  72. keep_attributes[key] = value
  73. }
  74. end
  75. attributes_new = {}
  76. attributes.each {|key, value|
  77. next if !value
  78. # get attribute name
  79. attribute_name_with_id = key.to_s
  80. attribute_name = key.to_s
  81. next if attribute_name[-3,3] != '_id'
  82. attribute_name = attribute_name[ 0, attribute_name.length-3 ]
  83. # check if attribute method exists
  84. next if !ref_object.respond_to?( attribute_name )
  85. # check if method has own class
  86. relation_class = ref_object.send(attribute_name).class
  87. next if !relation_class
  88. # lookup ref object
  89. relation_model = relation_class.lookup( :id => value )
  90. next if !relation_model
  91. # get name of ref object
  92. value = nil
  93. if relation_model.respond_to?('search_index_data')
  94. value = relation_model.send('search_index_data')
  95. end
  96. next if !value
  97. # save name of ref object
  98. attributes_new[ attribute_name ] = value
  99. if !keep_attributes[ attribute_name_with_id.to_sym ]
  100. attributes.delete(key)
  101. end
  102. }
  103. attributes_new.merge(attributes)
  104. end
  105. end