can_lookup_search_index_attributes.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanLookupSearchIndexAttributes
  3. extend ActiveSupport::Concern
  4. =begin
  5. lookup name of ref. objects
  6. ticket = Ticket.find(3)
  7. attributes = ticket.search_index_attribute_lookup
  8. returns
  9. attributes # object with lookup data
  10. =end
  11. def search_index_attribute_lookup
  12. attributes = self.attributes
  13. self.attributes.each do |key, value|
  14. next if !value
  15. # get attribute name
  16. attribute_name_with_id = key.to_s
  17. attribute_name = key.to_s
  18. next if attribute_name[-3, 3] != '_id'
  19. attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
  20. # check if attribute method exists
  21. next if !respond_to?(attribute_name)
  22. # check if method has own class
  23. relation_class = send(attribute_name).class
  24. next if !relation_class
  25. # lookup ref object
  26. relation_model = relation_class.lookup(id: value)
  27. next if !relation_model
  28. # get name of ref object
  29. value = nil
  30. if relation_model.respond_to?('search_index_data')
  31. value = relation_model.send('search_index_data')
  32. end
  33. if relation_model.respond_to?('name')
  34. value = relation_model.send('name')
  35. end
  36. next if !value
  37. # save name of ref object
  38. attributes[ attribute_name ] = value
  39. end
  40. ignored_attributes = self.class.instance_variable_get(:@search_index_attributes_ignored) || []
  41. return attributes if ignored_attributes.blank?
  42. ignored_attributes.each do |attribute|
  43. attributes.delete(attribute.to_s)
  44. end
  45. attributes
  46. end
  47. end