From bd29c7168754bc39dba2002b39320bcdd9dd9b8b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 27 Apr 2015 23:44:41 +0200 Subject: [PATCH] Some new rubocop checks. --- .../application_model/activity_stream_base.rb | 41 ++-- app/models/application_model/assets.rb | 37 ++-- .../background_job_search_index.rb | 16 +- .../application_model/history_log_base.rb | 63 +++--- .../application_model/search_index_base.rb | 183 +++++++++--------- app/models/history/assets.rb | 17 +- app/models/organization/assets.rb | 53 ++--- app/models/organization/permission.rb | 33 ++-- app/models/organization/search.rb | 82 ++++---- app/models/organization/search_index.rb | 71 +++---- app/models/store/file.rb | 148 +++++++------- app/models/store/object.rb | 6 +- app/models/store/provider/db.rb | 43 ++-- app/models/store/provider/file.rb | 145 +++++++------- app/models/ticket/escalation.rb | 2 +- app/models/ticket/history_log.rb | 2 +- 16 files changed, 482 insertions(+), 460 deletions(-) diff --git a/app/models/application_model/activity_stream_base.rb b/app/models/application_model/activity_stream_base.rb index 3329d51b3..4b199bc1e 100644 --- a/app/models/application_model/activity_stream_base.rb +++ b/app/models/application_model/activity_stream_base.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module ApplicationModel::ActivityStreamBase +class ApplicationModel + module ActivityStreamBase =begin @@ -18,28 +19,28 @@ 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 + 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 - 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 cedd86c0b..4d78a7cc4 100644 --- a/app/models/application_model/assets.rb +++ b/app/models/application_model/assets.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module ApplicationModel::Assets +class ApplicationModel + module Assets =begin @@ -20,24 +21,24 @@ 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 ) + 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 - } - data - 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 diff --git a/app/models/application_model/background_job_search_index.rb b/app/models/application_model/background_job_search_index.rb index 9205f3a6c..3aa6603ed 100644 --- a/app/models/application_model/background_job_search_index.rb +++ b/app/models/application_model/background_job_search_index.rb @@ -1,11 +1,13 @@ # perform background job -class ApplicationModel::BackgroundJobSearchIndex - def initialize(object, o_id) - @object = object - @o_id = o_id - end +class ApplicationModel + class 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 + def perform + Object.const_get(@object).find(@o_id).search_index_update_backend + end end end diff --git a/app/models/application_model/history_log_base.rb b/app/models/application_model/history_log_base.rb index db8463cc2..0c887136c 100644 --- a/app/models/application_model/history_log_base.rb +++ b/app/models/application_model/history_log_base.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module ApplicationModel::HistoryLogBase +class ApplicationModel + module HistoryLogBase =begin @@ -15,15 +16,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 @@ -74,27 +75,27 @@ 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] ) + def history_get(fulldata = false) + if !fulldata + return History.list( self.class.name, self['id'] ) end - } - return { - history: history[:list], - assets: history[:assets], - } - 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 diff --git a/app/models/application_model/search_index_base.rb b/app/models/application_model/search_index_base.rb index c5ad7fb81..4499c4e58 100644 --- a/app/models/application_model/search_index_base.rb +++ b/app/models/application_model/search_index_base.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module ApplicationModel::SearchIndexBase +class ApplicationModel + module SearchIndexBase =begin @@ -15,47 +16,47 @@ 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, - } - if self.class.search_index_support_config[:ignore_attributes] - self.class.search_index_support_config[:ignore_attributes].each {|key, value| - ignore_attributes[key] = value + # default ignored attributes + ignore_attributes = { + created_by_id: true, + updated_by_id: true, + active: true, } - 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 ) + 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 - else - SearchIndexBackend.add( self.class.to_s, attributes ) end - end =begin @@ -70,16 +71,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 @@ -95,51 +96,51 @@ 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 + # 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 - - 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 diff --git a/app/models/history/assets.rb b/app/models/history/assets.rb index e9f3b1816..58c7e2c5c 100644 --- a/app/models/history/assets.rb +++ b/app/models/history/assets.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module History::Assets +class History + module Assets =begin @@ -20,14 +21,14 @@ returns =end - def assets (data) + def assets (data) - if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ] - user = User.lookup( id: self['created_by_id'] ) - data = user.assets( data ) + if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ] + user = User.lookup( id: self['created_by_id'] ) + data = user.assets( data ) + end + + data end - - data end - end diff --git a/app/models/organization/assets.rb b/app/models/organization/assets.rb index 935311f60..b146372c2 100644 --- a/app/models/organization/assets.rb +++ b/app/models/organization/assets.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module Organization::Assets +class Organization + module Assets =begin @@ -20,33 +21,33 @@ returns =end - def assets (data) + def assets (data) - if !data[ Organization.to_app_model ] - data[ Organization.to_app_model ] = {} - end - if !data[ User.to_app_model ] - data[ User.to_app_model ] = {} - end - if !data[ Organization.to_app_model ][ self.id ] - data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations - if data[ Organization.to_app_model ][ self.id ]['member_ids'] - data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id| - if !data[ User.to_app_model ][ user_id ] - user = User.lookup( id: user_id ) - data = user.assets( data ) - end - } + if !data[ Organization.to_app_model ] + data[ Organization.to_app_model ] = {} end - end - ['created_by_id', 'updated_by_id'].each {|item| - next if !self[ item ] - if !data[ User.to_app_model ][ self[ item ] ] - user = User.lookup( id: self[ item ] ) - data = user.assets( data ) + if !data[ User.to_app_model ] + data[ User.to_app_model ] = {} end - } - data + if !data[ Organization.to_app_model ][ self.id ] + data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations + if data[ Organization.to_app_model ][ self.id ]['member_ids'] + data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id| + if !data[ User.to_app_model ][ user_id ] + user = User.lookup( id: user_id ) + data = user.assets( data ) + end + } + end + end + ['created_by_id', 'updated_by_id'].each {|item| + next if !self[ item ] + if !data[ User.to_app_model ][ self[ item ] ] + user = User.lookup( id: self[ item ] ) + data = user.assets( data ) + end + } + data + end end - end diff --git a/app/models/organization/permission.rb b/app/models/organization/permission.rb index e90c6aa01..70abaf7a8 100644 --- a/app/models/organization/permission.rb +++ b/app/models/organization/permission.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module Organization::Permission +class Organization + module Permission =begin @@ -15,24 +16,24 @@ returns =end - def permission (data) + def permission (data) - # check customer - if data[:current_user].is_role('Customer') + # check customer + if data[:current_user].is_role('Customer') - # access ok if its own organization - return false if data[:type] != 'ro' - return false if !data[:current_user].organization_id - return true if self.id == data[:current_user].organization_id + # access ok if its own organization + return false if data[:type] != 'ro' + return false if !data[:current_user].organization_id + return true if self.id == data[:current_user].organization_id - # no access - return false + # no access + return false + end + + # check agent + return true if data[:current_user].is_role(Z_ROLENAME_ADMIN) + return true if data[:current_user].is_role('Agent') + false end - - # check agent - return true if data[:current_user].is_role(Z_ROLENAME_ADMIN) - return true if data[:current_user].is_role('Agent') - return false end - end diff --git a/app/models/organization/search.rb b/app/models/organization/search.rb index 7abcd7f5a..c8537e8fb 100644 --- a/app/models/organization/search.rb +++ b/app/models/organization/search.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module Organization::Search +class Organization + module Search =begin @@ -18,52 +19,53 @@ returns =end - def search(params) + def search(params) - # get params - query = params[:query] - limit = params[:limit] || 10 - current_user = params[:current_user] + # get params + query = params[:query] + limit = params[:limit] || 10 + current_user = params[:current_user] - # enable search only for agents and admins - return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN) + # enable search only for agents and admins + return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN) - # try search index backend - if SearchIndexBackend.enabled? - items = SearchIndexBackend.search( query, limit, 'Organization' ) - organizations = [] - items.each { |item| - organizations.push Organization.lookup( id: item[:id] ) - } - return organizations - end + # try search index backend + if SearchIndexBackend.enabled? + items = SearchIndexBackend.search( query, limit, 'Organization' ) + organizations = [] + items.each { |item| + organizations.push Organization.lookup( id: item[:id] ) + } + return organizations + end - # fallback do sql query - # - stip out * we already search for *query* - - query.gsub! '*', '' - organizations = Organization.where( - 'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%" - ).order('name').limit(limit) + # fallback do sql query + # - stip out * we already search for *query* - + query.gsub! '*', '' + organizations = Organization.where( + 'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%" + ).order('name').limit(limit) - # if only a few organizations are found, search for names of users - if organizations.length <= 3 - organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( - 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" - ).order('organizations.name').limit(limit) - organizations_by_user.each {|organization_by_user| - organization_exists = false - organizations.each {|organization| - if organization.id == organization_by_user.id - organization_exists = true + # if only a few organizations are found, search for names of users + if organizations.length <= 3 + organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( + 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" + ).order('organizations.name').limit(limit) + organizations_by_user.each {|organization_by_user| + organization_exists = false + organizations.each {|organization| + if organization.id == organization_by_user.id + organization_exists = true + end + } + + # get model with full data + if !organization_exists + organizations.push Organization.find(organization_by_user) end } - - # get model with full data - if !organization_exists - organizations.push Organization.find(organization_by_user) - end - } + end + organizations end - organizations end end diff --git a/app/models/organization/search_index.rb b/app/models/organization/search_index.rb index 33421f9d6..b2cb977b9 100644 --- a/app/models/organization/search_index.rb +++ b/app/models/organization/search_index.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -module Organization::SearchIndex +class Organization + module SearchIndex =begin @@ -14,47 +15,47 @@ returns =end - def search_index_attribute_lookup(attributes, ref_object) - attributes_new = {} - attributes.each {|key, value| - next if !value + def search_index_attribute_lookup(attributes, ref_object) + attributes_new = {} + attributes.each {|key, value| + 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 ] + # 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 - next if !ref_object.respond_to?( attribute_name ) + # 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 + # 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 + # 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 + # 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) - } + # save name of ref object + attributes_new[ attribute_name ] = value + attributes.delete(key) + } - # add org member for search index data - attributes['member'] = [] - users = User.where( organization_id: self.id ) - users.each { |user| - attributes['member'].push user.search_index_data - } + # add org member for search index data + attributes['member'] = [] + users = User.where( organization_id: self.id ) + users.each { |user| + attributes['member'].push user.search_index_data + } - attributes_new.merge(attributes) + attributes_new.merge(attributes) + end end - end diff --git a/app/models/store/file.rb b/app/models/store/file.rb index c78afb4dc..0ec9f6e11 100644 --- a/app/models/store/file.rb +++ b/app/models/store/file.rb @@ -1,90 +1,92 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Store::File < ApplicationModel - include ApplicationLib - after_destroy :destroy_provider +class Store + class File < ApplicationModel + include ApplicationLib + after_destroy :destroy_provider - # add new file - def self.add(data) - sha = Digest::SHA256.hexdigest( data ) + # add new file + def self.add(data) + sha = Digest::SHA256.hexdigest( data ) - file = Store::File.where( sha: sha ).first - if file == nil + file = Store::File.where( sha: sha ).first + if file == nil - # load backend based on config - adapter_name = Setting.get('storage_provider') || 'DB' - if !adapter_name - raise 'Missing storage_provider setting option' - end - adapter = self.load_adapter( "Store::Provider::#{ adapter_name }" ) - adapter.add( data, sha ) - file = Store::File.create( - provider: adapter_name, - sha: sha, - ) - end - file - end - - # read content - def content - adapter = self.class.load_adapter("Store::Provider::#{ self.provider }") - if self.sha - c = adapter.get( self.sha ) - else - # fallback until migration is done - c = Store::Provider::DB.where( md5: self.md5 ).first.data - end - c - end - - # check data and sha, in case fix it - def self.verify(fix_it = nil) - success = true - Store::File.all.each {|item| - content = item.content - sha = Digest::SHA256.hexdigest( content ) - puts "CHECK: Store::File.find(#{item.id}) " - if sha != item.sha - success = false - puts "DIFF: sha diff of Store::File.find(#{item.id}) " - if fix_it - item.update_attribute( :sha, sha ) + # load backend based on config + adapter_name = Setting.get('storage_provider') || 'DB' + if !adapter_name + raise 'Missing storage_provider setting option' end + adapter = self.load_adapter( "Store::Provider::#{ adapter_name }" ) + adapter.add( data, sha ) + file = Store::File.create( + provider: adapter_name, + sha: sha, + ) end - } - success - end + file + end - # move file from one to other provider - # e. g. Store::File.move('File', 'DB') - # e. g. Store::File.move('DB', 'File') - def self.move(source, target) - adapter_source = load_adapter("Store::Provider::#{ source }") - adapter_target = load_adapter("Store::Provider::#{ target }") + # read content + def content + adapter = self.class.load_adapter("Store::Provider::#{ self.provider }") + if self.sha + c = adapter.get( self.sha ) + else + # fallback until migration is done + c = Store::Provider::DB.where( md5: self.md5 ).first.data + end + c + end - Store::File.all.each {|item| - next if item.provider == target - content = item.content + # check data and sha, in case fix it + def self.verify(fix_it = nil) + success = true + Store::File.all.each {|item| + content = item.content + sha = Digest::SHA256.hexdigest( content ) + puts "CHECK: Store::File.find(#{item.id}) " + if sha != item.sha + success = false + puts "DIFF: sha diff of Store::File.find(#{item.id}) " + if fix_it + item.update_attribute( :sha, sha ) + end + end + } + success + end - # add to new provider - adapter_target.add( content, item.sha ) + # move file from one to other provider + # e. g. Store::File.move('File', 'DB') + # e. g. Store::File.move('DB', 'File') + def self.move(source, target) + adapter_source = load_adapter("Store::Provider::#{ source }") + adapter_target = load_adapter("Store::Provider::#{ target }") - # update meta data - item.update_attribute( :provider, target ) + Store::File.all.each {|item| + next if item.provider == target + content = item.content - # remove from old provider - adapter_source.delete( item.sha ) + # add to new provider + adapter_target.add( content, item.sha ) - puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}" - } - true - end + # update meta data + item.update_attribute( :provider, target ) - private + # remove from old provider + adapter_source.delete( item.sha ) - def destroy_provider - adapter = self.class.load_adapter("Store::Provider::#{ self.provider }") - adapter.delete( self.sha ) + puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}" + } + true + end + + private + + def destroy_provider + adapter = self.class.load_adapter("Store::Provider::#{ self.provider }") + adapter.delete( self.sha ) + end end end diff --git a/app/models/store/object.rb b/app/models/store/object.rb index e7102dff3..dd6544a87 100644 --- a/app/models/store/object.rb +++ b/app/models/store/object.rb @@ -1,5 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Store::Object < ApplicationModel - validates :name, presence: true +class Store + class Object < ApplicationModel + validates :name, presence: true + end end diff --git a/app/models/store/provider/db.rb b/app/models/store/provider/db.rb index 4dbb9ccdf..8832e690e 100644 --- a/app/models/store/provider/db.rb +++ b/app/models/store/provider/db.rb @@ -1,25 +1,28 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Store::Provider::DB < ApplicationModel - self.table_name = 'store_provider_dbs' +class Store + class Provider + class DB < ApplicationModel + self.table_name = 'store_provider_dbs' - def self.add(data, sha) - Store::Provider::DB.create( - data: data, - sha: sha, - ) - true + def self.add(data, sha) + Store::Provider::DB.create( + data: data, + sha: sha, + ) + true + end + + def self.get(sha) + file = Store::Provider::DB.where( sha: sha ).first + return if !file + file.data + end + + def self.delete(sha) + Store::Provider::DB.where( sha: sha ).destroy_all + true + end + end end - - def self.get(sha) - file = Store::Provider::DB.where( sha: sha ).first - return if !file - file.data - end - - def self.delete(sha) - Store::Provider::DB.where( sha: sha ).destroy_all - true - end - end diff --git a/app/models/store/provider/file.rb b/app/models/store/provider/file.rb index fdcc8a562..731bf5186 100644 --- a/app/models/store/provider/file.rb +++ b/app/models/store/provider/file.rb @@ -1,84 +1,87 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Store::Provider::File +class Store + class Provider + class File - def self.add(data, sha) - write_to_fs(data, sha) - true - end + def self.add(data, sha) + write_to_fs(data, sha) + true + end - def self.get(sha) - read_from_fs(sha) - end + def self.get(sha) + read_from_fs(sha) + end - def self.delete(sha) - unlink_from_fs(sha) - end + def self.delete(sha) + unlink_from_fs(sha) + end - private + private - # generate file location - def self.get_locaton(sha) + # generate file location + def self.get_locaton(sha) - # generate directory - base = Rails.root.to_s + '/storage/fs/' - parts = sha.scan(/.{1,4}/) - path = parts[ 1 .. 10 ].join('/') + '/' - file = parts[ 11 .. parts.count ].join('') - location = "#{base}/#{path}" + # generate directory + base = Rails.root.to_s + '/storage/fs/' + parts = sha.scan(/.{1,4}/) + path = parts[ 1 .. 10 ].join('/') + '/' + file = parts[ 11 .. parts.count ].join('') + location = "#{base}/#{path}" - # create directory if not exists - if !File.exist?( location ) - FileUtils.mkdir_p( location ) - end - location += file - end + # create directory if not exists + if !File.exist?( location ) + FileUtils.mkdir_p( location ) + end + location += file + end - # unlink file from fs - def self.unlink_from_fs(sha) - if File.exist?( get_locaton(sha) ) - puts "NOTICE: storge remove '#{ get_locaton(sha) }'" - File.delete( get_locaton(sha) ) + # unlink file from fs + def self.unlink_from_fs(sha) + if File.exist?( get_locaton(sha) ) + puts "NOTICE: storge remove '#{ get_locaton(sha) }'" + File.delete( get_locaton(sha) ) + end + end + + # read file from fs + def self.read_from_fs(sha) + puts "read from fs #{ get_locaton(sha) }" + if !File.exist?( get_locaton(sha) ) + raise "ERROR: No such file #{ get_locaton(sha) }" + end + data = File.open( get_locaton(sha), 'rb' ) + content = data.read + + # check sha + local_sha = Digest::SHA256.hexdigest( content ) + if local_sha != sha + raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}" + end + content + end + + # write file to fs + def self.write_to_fs(data, sha) + + # install file + permission = '600' + if !File.exist?( get_locaton(sha) ) + puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})" + file = File.new( get_locaton(sha), 'wb' ) + file.write( data ) + file.close + end + File.chmod( permission.to_i(8), get_locaton(sha) ) + + # check sha + local_sha = Digest::SHA256.hexdigest( read_from_fs(sha) ) + if sha != local_sha + raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}" + end + + true + end end end - - # read file from fs - def self.read_from_fs(sha) - puts "read from fs #{ get_locaton(sha) }" - if !File.exist?( get_locaton(sha) ) - raise "ERROR: No such file #{ get_locaton(sha) }" - end - data = File.open( get_locaton(sha), 'rb' ) - content = data.read - - # check sha - local_sha = Digest::SHA256.hexdigest( content ) - if local_sha != sha - raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}" - end - content - end - - # write file to fs - def self.write_to_fs(data, sha) - - # install file - permission = '600' - if !File.exist?( get_locaton(sha) ) - puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})" - file = File.new( get_locaton(sha), 'wb' ) - file.write( data ) - file.close - end - File.chmod( permission.to_i(8), get_locaton(sha) ) - - # check sha - local_sha = Digest::SHA256.hexdigest( read_from_fs(sha) ) - if sha != local_sha - raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}" - end - - true - end - end diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb index c3512a53f..ba8389b34 100644 --- a/app/models/ticket/escalation.rb +++ b/app/models/ticket/escalation.rb @@ -319,7 +319,7 @@ returns return check_time if !escalation_time return escalation_time if !check_time return check_time if escalation_time > check_time - return escalation_time + escalation_time end end end diff --git a/app/models/ticket/history_log.rb b/app/models/ticket/history_log.rb index c3ee1afb6..a806a456e 100644 --- a/app/models/ticket/history_log.rb +++ b/app/models/ticket/history_log.rb @@ -73,7 +73,7 @@ returns assets = record.assets(assets) end } - return { + { history: list, assets: assets, }