search_index_base.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2013 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_at => true,
  15. :updated_at => true,
  16. :created_by_id => true,
  17. :updated_by_id => true,
  18. :active => true,
  19. }
  20. if self.class.search_index_support_config[:ignore_attributes]
  21. self.class.search_index_support_config[:ignore_attributes].each {|key, value|
  22. ignore_attributes[key] = value
  23. }
  24. end
  25. # remove ignored attributes
  26. attributes = self.attributes
  27. ignore_attributes.each {|key, value|
  28. next if value != true
  29. attributes.delete( key.to_s )
  30. }
  31. # fill up with search data
  32. attributes = search_index_attribute_lookup(attributes, self)
  33. return if !attributes
  34. # update backend
  35. if self.class.column_names.include? 'active'
  36. if self.active
  37. SearchIndexBackend.add( self.class.to_s, attributes )
  38. else
  39. SearchIndexBackend.remove( self.class.to_s, self.id )
  40. end
  41. else
  42. SearchIndexBackend.add( self.class.to_s, attributes )
  43. end
  44. end
  45. =begin
  46. get data to store in search index
  47. ticket = Ticket.find(123)
  48. result = ticket.search_index_data
  49. returns
  50. result = true # false
  51. =end
  52. def search_index_data
  53. puts 'aaaaa'
  54. attributes = {}
  55. ['name', 'note'].each { |key|
  56. if self[key] && !self[key].empty?
  57. attributes[key] = self[key]
  58. end
  59. }
  60. return if attributes.empty?
  61. attributes
  62. end
  63. private
  64. =begin
  65. lookup name of ref. objects
  66. attributes = search_index_attribute_lookup(attributes, Ticket)
  67. returns
  68. attributes # object with lookup data
  69. =end
  70. def search_index_attribute_lookup(attributes, ref_object)
  71. attributes_new = {}
  72. attributes.each {|key, value|
  73. next if !value
  74. # get attribute name
  75. attribute_name = key.to_s
  76. next if attribute_name[-3,3] != '_id'
  77. attribute_name = attribute_name[ 0, attribute_name.length-3 ]
  78. # check if attribute method exists
  79. next if !ref_object.respond_to?( attribute_name )
  80. # check if method has own class
  81. relation_class = ref_object.send(attribute_name).class
  82. next if !relation_class
  83. # lookup ref object
  84. relation_model = relation_class.lookup( :id => value )
  85. next if !relation_model
  86. # get name of ref object
  87. value = nil
  88. if relation_model.respond_to?('search_index_data')
  89. value = relation_model.send('search_index_data')
  90. end
  91. next if !value
  92. # save name of ref object
  93. attributes_new[ attribute_name ] = value
  94. attributes.delete(key)
  95. }
  96. attributes_new.merge(attributes)
  97. end
  98. end