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