2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
module ApplicationModel::CanLookupSearchIndexAttributes
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
lookup name of ref. objects
|
|
|
|
|
|
|
|
ticket = Ticket.find(3)
|
|
|
|
attributes = ticket.search_index_attribute_lookup
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
attributes # object with lookup data
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_attribute_lookup
|
|
|
|
|
|
|
|
attributes = self.attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
self.attributes.each do |key, value|
|
2019-06-28 11:38:49 +00:00
|
|
|
attribute_name = key.to_s
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# ignore standard attribute if needed
|
|
|
|
if self.class.search_index_attribute_ignored?(attribute_name)
|
|
|
|
attributes.delete(attribute_name)
|
|
|
|
next
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# need value for reference data
|
|
|
|
next if !value
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# check if we have a referenced object which we could include here
|
|
|
|
next if !search_index_attribute_method(attribute_name)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# get referenced attribute name
|
|
|
|
attribute_ref_name = self.class.search_index_attribute_ref_name(attribute_name)
|
|
|
|
next if !attribute_ref_name
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# ignore referenced attributes if needed
|
|
|
|
next if self.class.search_index_attribute_ignored?(attribute_ref_name)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
# get referenced attribute value
|
|
|
|
value = search_index_value_by_attribute(attribute_name)
|
2017-01-31 17:13:45 +00:00
|
|
|
next if !value
|
|
|
|
|
|
|
|
# save name of ref object
|
2020-02-18 14:49:52 +00:00
|
|
|
attributes[ attribute_ref_name ] = value
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function returns the relational search index value based on the attribute name.
|
|
|
|
|
|
|
|
organization = Organization.find(1)
|
|
|
|
value = organization.search_index_value_by_attribute('organization_id')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
value = {"name"=>"Zammad Foundation"}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_value_by_attribute(attribute_name = '')
|
|
|
|
|
|
|
|
# get attribute name
|
|
|
|
relation_class = search_index_attribute_method(attribute_name)
|
|
|
|
return if !relation_class
|
|
|
|
|
|
|
|
# lookup ref object
|
|
|
|
relation_model = relation_class.lookup(id: attributes[attribute_name])
|
|
|
|
return if !relation_model
|
|
|
|
|
|
|
|
relation_model.search_index_value
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function returns the relational search value.
|
|
|
|
|
|
|
|
organization = Organization.find(1)
|
|
|
|
value = organization.search_index_value
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
returns
|
|
|
|
|
|
|
|
value = {"name"=>"Zammad Foundation"}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_value
|
|
|
|
|
|
|
|
# get name of ref object
|
|
|
|
value = nil
|
|
|
|
if respond_to?('search_index_data')
|
|
|
|
value = send('search_index_data')
|
|
|
|
return if value == true
|
|
|
|
elsif respond_to?('name')
|
|
|
|
value = send('name')
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-02-18 14:49:52 +00:00
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function returns the method for the relational search index attribute.
|
|
|
|
|
|
|
|
method = Ticket.new.search_index_attribute_method('organization_id')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
method = Organization (class)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_attribute_method(attribute_name = '')
|
|
|
|
return if attribute_name[-3, 3] != '_id'
|
|
|
|
|
|
|
|
attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
|
|
|
|
return if !respond_to?(attribute_name)
|
|
|
|
|
|
|
|
send(attribute_name).class
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function returns the relational search index attribute name for the given class.
|
|
|
|
|
|
|
|
attribute_ref_name = Organization.search_index_attribute_ref_name('user_id')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
attribute_ref_name = 'user'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_attribute_ref_name(attribute_name)
|
|
|
|
attribute_name[ 0, attribute_name.length - 3 ]
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
This function returns if a search index attribute should be ignored.
|
|
|
|
|
|
|
|
ignored = Ticket.search_index_attribute_ignored?('organization_id')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
ignored = false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_attribute_ignored?(attribute_name = '')
|
|
|
|
ignored_attributes = instance_variable_get(:@search_index_attributes_ignored) || []
|
|
|
|
return if ignored_attributes.blank?
|
|
|
|
|
|
|
|
ignored_attributes.include?(attribute_name.to_sym)
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
end
|