Moved back to compact module/class declaration.
This commit is contained in:
parent
19d2b9750c
commit
70f3bb5dd4
5 changed files with 170 additions and 179 deletions
|
@ -1,7 +1,6 @@
|
||||||
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
|
# rubocop:disable ClassAndModuleChildren
|
||||||
class ApplicationModel
|
module ApplicationModel::ActivityStreamBase
|
||||||
module ActivityStreamBase
|
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
|
@ -19,28 +18,27 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def activity_stream_log (type, user_id, force = false)
|
def activity_stream_log (type, user_id, force = false)
|
||||||
|
|
||||||
# return if we run import mode
|
# return if we run import mode
|
||||||
return if Setting.get('import_mode')
|
return if Setting.get('import_mode')
|
||||||
|
|
||||||
# return if we run on init mode
|
# return if we run on init mode
|
||||||
return if !Setting.get('system_init_done')
|
return if !Setting.get('system_init_done')
|
||||||
|
|
||||||
role = self.class.activity_stream_support_config[:role]
|
role = self.class.activity_stream_support_config[:role]
|
||||||
updated_at = self.updated_at
|
updated_at = self.updated_at
|
||||||
if force
|
if force
|
||||||
updated_at = Time.new
|
updated_at = Time.new
|
||||||
end
|
|
||||||
ActivityStream.add(
|
|
||||||
o_id: self['id'],
|
|
||||||
type: type,
|
|
||||||
object: self.class.name,
|
|
||||||
group_id: self['group_id'],
|
|
||||||
role: role,
|
|
||||||
created_at: updated_at,
|
|
||||||
created_by_id: user_id,
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
ActivityStream.add(
|
||||||
|
o_id: self['id'],
|
||||||
|
type: type,
|
||||||
|
object: self.class.name,
|
||||||
|
group_id: self['group_id'],
|
||||||
|
role: role,
|
||||||
|
created_at: updated_at,
|
||||||
|
created_by_id: user_id,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
|
# rubocop:disable ClassAndModuleChildren
|
||||||
class ApplicationModel
|
module ApplicationModel::Assets
|
||||||
module Assets
|
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
|
@ -21,24 +20,23 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def assets (data = {})
|
def assets (data = {})
|
||||||
|
|
||||||
if !data[ self.class.to_app_model ]
|
if !data[ self.class.to_app_model ]
|
||||||
data[ self.class.to_app_model ] = {}
|
data[ self.class.to_app_model ] = {}
|
||||||
end
|
|
||||||
if !data[ self.class.to_app_model ][ self.id ]
|
|
||||||
data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
|
|
||||||
end
|
|
||||||
|
|
||||||
return data if !self['created_by_id'] && !self['updated_by_id']
|
|
||||||
['created_by_id', 'updated_by_id'].each {|item|
|
|
||||||
next if !self[ item ]
|
|
||||||
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
|
|
||||||
user = User.lookup( id: self[ item ] )
|
|
||||||
data = user.assets( data )
|
|
||||||
end
|
|
||||||
}
|
|
||||||
data
|
|
||||||
end
|
end
|
||||||
|
if !data[ self.class.to_app_model ][ self.id ]
|
||||||
|
data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
|
||||||
|
end
|
||||||
|
|
||||||
|
return data if !self['created_by_id'] && !self['updated_by_id']
|
||||||
|
['created_by_id', 'updated_by_id'].each {|item|
|
||||||
|
next if !self[ item ]
|
||||||
|
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
|
||||||
|
user = User.lookup( id: self[ item ] )
|
||||||
|
data = user.assets( data )
|
||||||
|
end
|
||||||
|
}
|
||||||
|
data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
# perform background job
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
class ApplicationModel
|
# rubocop:disable ClassAndModuleChildren
|
||||||
class BackgroundJobSearchIndex
|
class ApplicationModel::BackgroundJobSearchIndex
|
||||||
def initialize(object, o_id)
|
def initialize(object, o_id)
|
||||||
@object = object
|
@object = object
|
||||||
@o_id = o_id
|
@o_id = o_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
Object.const_get(@object).find(@o_id).search_index_update_backend
|
Object.const_get(@object).find(@o_id).search_index_update_backend
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
|
# rubocop:disable ClassAndModuleChildren
|
||||||
class ApplicationModel
|
module ApplicationModel::HistoryLogBase
|
||||||
module HistoryLogBase
|
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
|
@ -16,15 +15,15 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def history_log (type, user_id, data = {})
|
def history_log (type, user_id, data = {})
|
||||||
data[:o_id] = self['id']
|
data[:o_id] = self['id']
|
||||||
data[:history_type] = type
|
data[:history_type] = type
|
||||||
data[:history_object] = self.class.name
|
data[:history_object] = self.class.name
|
||||||
data[:related_o_id] = nil
|
data[:related_o_id] = nil
|
||||||
data[:related_history_object] = nil
|
data[:related_history_object] = nil
|
||||||
data[:created_by_id] = user_id
|
data[:created_by_id] = user_id
|
||||||
History.add(data)
|
History.add(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
|
@ -75,27 +74,26 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def history_get(fulldata = false)
|
def history_get(fulldata = false)
|
||||||
if !fulldata
|
if !fulldata
|
||||||
return History.list( self.class.name, self['id'] )
|
return History.list( self.class.name, self['id'] )
|
||||||
end
|
|
||||||
|
|
||||||
# get related objects
|
|
||||||
history = History.list( self.class.name, self['id'], nil, true )
|
|
||||||
history[:list].each {|item|
|
|
||||||
record = Kernel.const_get( item['object'] ).find( item['o_id'] )
|
|
||||||
|
|
||||||
history[:assets] = record.assets( history[:assets] )
|
|
||||||
|
|
||||||
if item['related_object']
|
|
||||||
record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
|
|
||||||
history[:assets] = record.assets( history[:assets] )
|
|
||||||
end
|
|
||||||
}
|
|
||||||
{
|
|
||||||
history: history[:list],
|
|
||||||
assets: history[:assets],
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# get related objects
|
||||||
|
history = History.list( self.class.name, self['id'], nil, true )
|
||||||
|
history[:list].each {|item|
|
||||||
|
record = Kernel.const_get( item['object'] ).find( item['o_id'] )
|
||||||
|
|
||||||
|
history[:assets] = record.assets( history[:assets] )
|
||||||
|
|
||||||
|
if item['related_object']
|
||||||
|
record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
|
||||||
|
history[:assets] = record.assets( history[:assets] )
|
||||||
|
end
|
||||||
|
}
|
||||||
|
{
|
||||||
|
history: history[:list],
|
||||||
|
assets: history[:assets],
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
|
# rubocop:disable ClassAndModuleChildren
|
||||||
class ApplicationModel
|
module ApplicationModel::SearchIndexBase
|
||||||
module SearchIndexBase
|
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
|
@ -16,48 +15,48 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def search_index_update_backend
|
def search_index_update_backend
|
||||||
return if !self.class.search_index_support_config
|
return if !self.class.search_index_support_config
|
||||||
|
|
||||||
# default ignored attributes
|
# default ignored attributes
|
||||||
ignore_attributes = {
|
ignore_attributes = {
|
||||||
created_by_id: true,
|
created_by_id: true,
|
||||||
updated_by_id: true,
|
updated_by_id: true,
|
||||||
active: 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
|
||||||
}
|
}
|
||||||
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
|
|
||||||
|
|
||||||
# for performance reasons, Model.search_index_reload will only collect if of object
|
|
||||||
# get whole data here
|
|
||||||
data = self.class.find(self.id)
|
|
||||||
|
|
||||||
# remove ignored attributes
|
|
||||||
attributes = data.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, data)
|
|
||||||
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
|
end
|
||||||
|
|
||||||
|
# for performance reasons, Model.search_index_reload will only collect if of object
|
||||||
|
# get whole data here
|
||||||
|
data = self.class.find(self.id)
|
||||||
|
|
||||||
|
# remove ignored attributes
|
||||||
|
attributes = data.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, data)
|
||||||
|
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
|
=begin
|
||||||
|
|
||||||
get data to store in search index
|
get data to store in search index
|
||||||
|
@ -71,16 +70,16 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def search_index_data
|
def search_index_data
|
||||||
attributes = {}
|
attributes = {}
|
||||||
['name', 'note'].each { |key|
|
['name', 'note'].each { |key|
|
||||||
if self[key] && !self[key].empty?
|
if self[key] && !self[key].empty?
|
||||||
attributes[key] = self[key]
|
attributes[key] = self[key]
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
return if attributes.empty?
|
return if attributes.empty?
|
||||||
attributes
|
attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
@ -96,51 +95,50 @@ returns
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def search_index_attribute_lookup(attributes, ref_object)
|
def search_index_attribute_lookup(attributes, ref_object)
|
||||||
|
|
||||||
# default keep attributes
|
# default keep attributes
|
||||||
keep_attributes = {}
|
keep_attributes = {}
|
||||||
if self.class.search_index_support_config[:keep_attributes]
|
if self.class.search_index_support_config[:keep_attributes]
|
||||||
self.class.search_index_support_config[:keep_attributes].each {|key, value|
|
self.class.search_index_support_config[:keep_attributes].each {|key, value|
|
||||||
keep_attributes[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
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue