can_lookup_search_index_attributes.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. attribute_name = key.to_s
  15. # ignore standard attribute if needed
  16. if self.class.search_index_attribute_ignored?(attribute_name)
  17. attributes.delete(attribute_name)
  18. next
  19. end
  20. # need value for reference data
  21. next if !value
  22. # check if we have a referenced object which we could include here
  23. next if !search_index_attribute_method(attribute_name)
  24. # get referenced attribute name
  25. attribute_ref_name = self.class.search_index_attribute_ref_name(attribute_name)
  26. next if !attribute_ref_name
  27. # ignore referenced attributes if needed
  28. next if self.class.search_index_attribute_ignored?(attribute_ref_name)
  29. # get referenced attribute value
  30. value = search_index_value_by_attribute(attribute_name)
  31. next if !value
  32. # save name of ref object
  33. attributes[ attribute_ref_name ] = value
  34. end
  35. attributes
  36. end
  37. =begin
  38. This function returns the relational search index value based on the attribute name.
  39. organization = Organization.find(1)
  40. value = organization.search_index_value_by_attribute('organization_id')
  41. returns
  42. value = {"name"=>"Zammad Foundation"}
  43. =end
  44. def search_index_value_by_attribute(attribute_name = '')
  45. # get attribute name
  46. relation_class = search_index_attribute_method(attribute_name)
  47. return if !relation_class
  48. # lookup ref object
  49. relation_model = relation_class.lookup(id: attributes[attribute_name])
  50. return if !relation_model
  51. relation_model.search_index_value
  52. end
  53. =begin
  54. This function returns the relational search value.
  55. organization = Organization.find(1)
  56. value = organization.search_index_value
  57. returns
  58. value = {"name"=>"Zammad Foundation"}
  59. =end
  60. def search_index_value
  61. # get name of ref object
  62. value = nil
  63. if respond_to?('name')
  64. value = send('name')
  65. elsif respond_to?('search_index_data')
  66. value = send('search_index_data')
  67. return if value == true
  68. end
  69. value
  70. end
  71. =begin
  72. This function returns the method for the relational search index attribute.
  73. method = Ticket.new.search_index_attribute_method('organization_id')
  74. returns
  75. method = Organization (class)
  76. =end
  77. def search_index_attribute_method(attribute_name = '')
  78. return if attribute_name[-3, 3] != '_id'
  79. attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
  80. return if !respond_to?(attribute_name)
  81. send(attribute_name).class
  82. end
  83. class_methods do
  84. =begin
  85. This function returns the relational search index attribute name for the given class.
  86. attribute_ref_name = Organization.search_index_attribute_ref_name('user_id')
  87. returns
  88. attribute_ref_name = 'user'
  89. =end
  90. def search_index_attribute_ref_name(attribute_name)
  91. attribute_name[ 0, attribute_name.length - 3 ]
  92. end
  93. =begin
  94. This function returns if a search index attribute should be ignored.
  95. ignored = Ticket.search_index_attribute_ignored?('organization_id')
  96. returns
  97. ignored = false
  98. =end
  99. def search_index_attribute_ignored?(attribute_name = '')
  100. ignored_attributes = instance_variable_get(:@search_index_attributes_ignored) || []
  101. return if ignored_attributes.blank?
  102. ignored_attributes.include?(attribute_name.to_sym)
  103. end
  104. end
  105. end