2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2014-01-29 23:54:34 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
class Organization
|
|
|
|
module SearchIndex
|
2014-01-29 23:54:34 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
lookup name of ref. objects
|
|
|
|
|
2016-03-08 06:32:58 +00:00
|
|
|
organization = Organization.find(123)
|
|
|
|
attributes = organization.search_index_attribute_lookup(attributes, Organization)
|
2014-01-29 23:54:34 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
attributes # object with lookup data
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def search_index_attribute_lookup(attributes, ref_object)
|
|
|
|
attributes_new = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
attributes.each { |key, value|
|
2015-04-27 21:44:41 +00:00
|
|
|
next if !value
|
|
|
|
|
|
|
|
# get attribute name
|
|
|
|
attribute_name = key.to_s
|
|
|
|
next if attribute_name[-3, 3] != '_id'
|
|
|
|
attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
|
|
|
|
|
|
|
|
# check if attribute method exists
|
2016-03-08 06:32:58 +00:00
|
|
|
next if !ref_object.respond_to?(attribute_name)
|
2015-04-27 21:44:41 +00:00
|
|
|
|
|
|
|
# check if method has own class
|
|
|
|
relation_class = ref_object.send(attribute_name).class
|
|
|
|
next if !relation_class
|
|
|
|
|
|
|
|
# lookup ref object
|
2016-03-08 06:32:58 +00:00
|
|
|
relation_model = relation_class.lookup(id: value)
|
2015-04-27 21:44:41 +00:00
|
|
|
next if !relation_model
|
|
|
|
|
|
|
|
# get name of ref object
|
|
|
|
value = nil
|
|
|
|
if relation_model.respond_to?('search_index_data')
|
|
|
|
value = relation_model.send('search_index_data')
|
|
|
|
end
|
|
|
|
next if !value
|
|
|
|
|
|
|
|
# save name of ref object
|
|
|
|
attributes_new[ attribute_name ] = value
|
|
|
|
attributes.delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
# add org member for search index data
|
|
|
|
attributes['member'] = []
|
2016-03-08 06:32:58 +00:00
|
|
|
users = User.where(organization_id: id)
|
2015-04-27 21:44:41 +00:00
|
|
|
users.each { |user|
|
|
|
|
attributes['member'].push user.search_index_data
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes_new.merge(attributes)
|
|
|
|
end
|
2014-01-29 23:54:34 +00:00
|
|
|
end
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|