trabajo-afectivo/app/models/ticket/search_index.rb

104 lines
3.1 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Ticket::SearchIndex
=begin
2014-01-30 00:06:42 +00:00
build and send data for search index 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
2015-04-05 23:28:52 +00:00
# for performance reasons, Model.search_index_reload will only collect if of object
# get whole data here
ticket = self.class.find(self.id)
# remove ignored attributes
attributes = ticket.attributes
ignore_attributes.each {|key, value|
next if value != true
attributes.delete( key.to_s )
}
2014-02-05 20:53:21 +00:00
# add tags
tags = Tag.tag_list( :object=> 'Ticket', :o_id => self.id )
if tags && !tags.empty?
attributes[:tag] = tags
end
2015-04-05 23:28:52 +00:00
# lookup attributes of ref. objects (normally name and note)
attributes = search_index_attribute_lookup( attributes, ticket )
# list ignored file extentions
ignore_attachments = [ '.png', '.jpg', '.jpeg', '.mpeg', '.mov' ]
2015-04-05 23:28:52 +00:00
# collect article data
articles = Ticket::Article.where( :ticket_id => self.id )
attributes['articles_all'] = []
articles.each {|article|
article_attributes = article.attributes
2015-04-05 23:28:52 +00:00
# remove note needed attributes
ignore = ['created_by_id', 'updated_by_id', 'updated_at', 'references', 'message_id_md5', 'message_id', 'in_reply_to', 'ticket_id']
ignore.each {|attribute|
article_attributes.delete( attribute )
}
# lookup attributes of ref. objects (normally name and note)
article_attributes = search_index_attribute_lookup( article_attributes, article )
2014-02-02 18:58:59 +00:00
2015-04-05 23:28:52 +00:00
# index raw text body
if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
article_attributes['body'] = article_attributes['body'].html2text
end
2014-02-02 18:58:59 +00:00
# lookup attachments
article.attachments.each {|attachment|
2015-04-05 23:28:52 +00:00
if !attributes['attachments']
attributes['attachments'] = []
end
# check file size
# check ignored files
if attachment.filename
filename_extention = attachment.filename.downcase
filename_extention.gsub!(/^.*(\..+?)$/, "\\1")
if !ignore_attachments.include?( filename_extention.downcase )
data = {
"_name" => attachment.filename,
"content" => Base64.encode64( attachment.content )
}
attributes['attachments'].push data
end
2014-02-02 18:58:59 +00:00
end
}
attributes['articles_all'].push article_attributes
}
return if !attributes
SearchIndexBackend.add(self.class.to_s, attributes)
end
2015-04-05 23:28:52 +00:00
end