search_index.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class User
  3. module SearchIndex
  4. =begin
  5. lookup name of ref. objects
  6. user = User.find(123)
  7. attributes = user.search_index_attribute_lookup
  8. returns
  9. attributes # object with lookup data
  10. =end
  11. def search_index_attribute_lookup
  12. attributes = super
  13. attributes['permissions'] = []
  14. permissions_with_child_ids.each do |permission_id|
  15. permission = ::Permission.lookup(id: permission_id)
  16. next if !permission
  17. attributes['permissions'].push permission.name
  18. end
  19. attributes['role_ids'] = role_ids
  20. attributes
  21. end
  22. =begin
  23. get data to store in search index
  24. user = User.find(2)
  25. result = user.search_index_data
  26. returns
  27. result = {
  28. attribute1: 'some value',
  29. attribute2: ['value 1', 'value 2'],
  30. ...
  31. }
  32. =end
  33. def search_index_data
  34. attributes = {}
  35. self.attributes.each do |key, value|
  36. next if key == 'password'
  37. next if !value
  38. next if value.respond_to?('blank?') && value.blank?
  39. attributes[key] = value
  40. end
  41. return if attributes.blank?
  42. if attributes['organization_id'].present?
  43. organization = Organization.lookup(id: attributes['organization_id'])
  44. if organization
  45. attributes['organization'] = organization.name
  46. attributes['organization_ref'] = organization.search_index_data
  47. end
  48. end
  49. attributes
  50. end
  51. end
  52. end