search_index.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module Organization::SearchIndex
  3. =begin
  4. lookup name of ref. objects
  5. attributes = search_index_attribute_lookup(attributes, Ticket)
  6. returns
  7. attributes # object with lookup data
  8. =end
  9. def search_index_attribute_lookup(attributes, ref_object)
  10. attributes_new = {}
  11. attributes.each {|key, value|
  12. next if !value
  13. # get attribute name
  14. attribute_name = key.to_s
  15. next if attribute_name[-3,3] != '_id'
  16. attribute_name = attribute_name[ 0, attribute_name.length-3 ]
  17. # check if attribute method exists
  18. next if !ref_object.respond_to?( attribute_name )
  19. # check if method has own class
  20. relation_class = ref_object.send(attribute_name).class
  21. next if !relation_class
  22. # lookup ref object
  23. relation_model = relation_class.lookup( :id => value )
  24. next if !relation_model
  25. # get name of ref object
  26. value = nil
  27. if relation_model.respond_to?('search_index_data')
  28. value = relation_model.send('search_index_data')
  29. end
  30. next if !value
  31. # save name of ref object
  32. attributes_new[ attribute_name ] = value
  33. attributes.delete(key)
  34. }
  35. # add org member for search index data
  36. attributes['member'] = []
  37. users = User.where( :organization_id => self.id )
  38. users.each { |user|
  39. attributes['member'].push user.search_index_data
  40. }
  41. attributes_new.merge(attributes)
  42. end
  43. end