123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
- module ApplicationModel::SearchIndexBase
- =begin
- collect data to index and send to backend
- ticket = Ticket.find(123)
- result = ticket.search_index_update_backend
- returns
- result = true # false
- =end
- def search_index_update_backend
- return if !self.class.search_index_support_config
- # default ignored attributes
- ignore_attributes = {
- :created_by_id => true,
- :updated_by_id => true,
- :active => true,
- }
- if self.class.search_index_support_config[:ignore_attributes]
- self.class.search_index_support_config[:ignore_attributes].each {|key, value|
- ignore_attributes[key] = value
- }
- end
- # remove ignored attributes
- attributes = self.attributes
- ignore_attributes.each {|key, value|
- next if value != true
- attributes.delete( key.to_s )
- }
- # fill up with search data
- attributes = search_index_attribute_lookup(attributes, self)
- return if !attributes
- # update backend
- if self.class.column_names.include? 'active'
- if self.active
- SearchIndexBackend.add( self.class.to_s, attributes )
- else
- SearchIndexBackend.remove( self.class.to_s, self.id )
- end
- else
- SearchIndexBackend.add( self.class.to_s, attributes )
- end
- end
- =begin
- get data to store in search index
- ticket = Ticket.find(123)
- result = ticket.search_index_data
- returns
- result = true # false
- =end
- def search_index_data
- attributes = {}
- ['name', 'note'].each { |key|
- if self[key] && !self[key].empty?
- attributes[key] = self[key]
- end
- }
- return if attributes.empty?
- attributes
- end
- private
- =begin
- lookup name of ref. objects
- attributes = search_index_attribute_lookup(attributes, Ticket)
- returns
- attributes # object with lookup data
- =end
- def search_index_attribute_lookup(attributes, ref_object)
- # default keep attributes
- keep_attributes = {}
- if self.class.search_index_support_config[:keep_attributes]
- self.class.search_index_support_config[:keep_attributes].each {|key, value|
- keep_attributes[key] = value
- }
- end
- attributes_new = {}
- attributes.each {|key, value|
- next if !value
- # get attribute name
- attribute_name_with_id = key.to_s
- 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
- next if !ref_object.respond_to?( attribute_name )
- # check if method has own class
- relation_class = ref_object.send(attribute_name).class
- next if !relation_class
- # lookup ref object
- relation_model = relation_class.lookup( :id => value )
- 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
- if !keep_attributes[ attribute_name_with_id.to_sym ]
- attributes.delete(key)
- end
- }
- attributes_new.merge(attributes)
- end
- end
|