From 70f3bb5dd493333e141ef9f11d4cf20e49118664 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 1 May 2015 15:08:43 +0200 Subject: [PATCH] Moved back to compact module/class declaration. --- .../application_model/activity_stream_base.rb | 42 ++-- app/models/application_model/assets.rb | 38 ++-- .../background_job_search_index.rb | 19 +- .../application_model/history_log_base.rb | 64 +++--- .../application_model/search_index_base.rb | 186 +++++++++--------- 5 files changed, 170 insertions(+), 179 deletions(-) diff --git a/app/models/application_model/activity_stream_base.rb b/app/models/application_model/activity_stream_base.rb index 4b199bc1e..006edef4a 100644 --- a/app/models/application_model/activity_stream_base.rb +++ b/app/models/application_model/activity_stream_base.rb @@ -1,7 +1,6 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ - -class ApplicationModel - module ActivityStreamBase +# rubocop:disable ClassAndModuleChildren +module ApplicationModel::ActivityStreamBase =begin @@ -19,28 +18,27 @@ returns =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 Setting.get('import_mode') + # return if we run import mode + return if Setting.get('import_mode') - # return if we run on init mode - return if !Setting.get('system_init_done') + # return if we run on init mode + return if !Setting.get('system_init_done') - role = self.class.activity_stream_support_config[:role] - updated_at = self.updated_at - if force - 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, - ) + role = self.class.activity_stream_support_config[:role] + updated_at = self.updated_at + if force + 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 diff --git a/app/models/application_model/assets.rb b/app/models/application_model/assets.rb index 4d78a7cc4..b20fb8ab9 100644 --- a/app/models/application_model/assets.rb +++ b/app/models/application_model/assets.rb @@ -1,7 +1,6 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ - -class ApplicationModel - module Assets +# rubocop:disable ClassAndModuleChildren +module ApplicationModel::Assets =begin @@ -21,24 +20,23 @@ returns =end - def assets (data = {}) + def assets (data = {}) - if !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 + if !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 diff --git a/app/models/application_model/background_job_search_index.rb b/app/models/application_model/background_job_search_index.rb index 3aa6603ed..bae7e1716 100644 --- a/app/models/application_model/background_job_search_index.rb +++ b/app/models/application_model/background_job_search_index.rb @@ -1,13 +1,12 @@ -# perform background job -class ApplicationModel - class BackgroundJobSearchIndex - def initialize(object, o_id) - @object = object - @o_id = o_id - end +# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ +# rubocop:disable ClassAndModuleChildren +class ApplicationModel::BackgroundJobSearchIndex + def initialize(object, o_id) + @object = object + @o_id = o_id + end - def perform - Object.const_get(@object).find(@o_id).search_index_update_backend - end + def perform + Object.const_get(@object).find(@o_id).search_index_update_backend end end diff --git a/app/models/application_model/history_log_base.rb b/app/models/application_model/history_log_base.rb index 0c887136c..f75166e1d 100644 --- a/app/models/application_model/history_log_base.rb +++ b/app/models/application_model/history_log_base.rb @@ -1,7 +1,6 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ - -class ApplicationModel - module HistoryLogBase +# rubocop:disable ClassAndModuleChildren +module ApplicationModel::HistoryLogBase =begin @@ -16,15 +15,15 @@ returns =end - def history_log (type, user_id, data = {}) - data[:o_id] = self['id'] - data[:history_type] = type - data[:history_object] = self.class.name - data[:related_o_id] = nil - data[:related_history_object] = nil - data[:created_by_id] = user_id - History.add(data) - end + def history_log (type, user_id, data = {}) + data[:o_id] = self['id'] + data[:history_type] = type + data[:history_object] = self.class.name + data[:related_o_id] = nil + data[:related_history_object] = nil + data[:created_by_id] = user_id + History.add(data) + end =begin @@ -75,27 +74,26 @@ returns =end - def history_get(fulldata = false) - if !fulldata - 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], - } + def history_get(fulldata = false) + if !fulldata + 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 diff --git a/app/models/application_model/search_index_base.rb b/app/models/application_model/search_index_base.rb index 4499c4e58..41a253f19 100644 --- a/app/models/application_model/search_index_base.rb +++ b/app/models/application_model/search_index_base.rb @@ -1,7 +1,6 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ - -class ApplicationModel - module SearchIndexBase +# rubocop:disable ClassAndModuleChildren +module ApplicationModel::SearchIndexBase =begin @@ -16,48 +15,48 @@ returns =end - def search_index_update_backend - return if !self.class.search_index_support_config + 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, + # 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 } - 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 + # 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 get data to store in search index @@ -71,16 +70,16 @@ returns =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 + 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 @@ -96,51 +95,50 @@ returns =end - def search_index_attribute_lookup(attributes, ref_object) + 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 + # 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 } - attributes_new.merge(attributes) 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