From b2c065812f82fd1e6f656def9f70dd1ad7aa1ac1 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Sun, 6 Jan 2019 19:41:29 +0100 Subject: [PATCH] Refactoring: Migrated Kernel.const_get to Rails .constantize. --- app/controllers/search_controller.rb | 2 +- .../user_access_token_controller.rb | 2 +- app/models/application_model/can_assets.rb | 2 +- .../application_model/can_touch_references.rb | 4 +- app/models/channel.rb | 2 +- app/models/channel/email_parser.rb | 4 +- app/models/concerns/has_history.rb | 4 +- app/models/external_credential.rb | 2 +- app/models/object_manager/attribute.rb | 2 +- app/models/observer/transaction.rb | 4 +- app/models/stats_store/search_index.rb | 4 +- app/models/store/file.rb | 12 +++--- app/models/ticket/number.rb | 11 +----- app/models/transaction/background_job.rb | 3 +- lib/application_lib.rb | 37 +------------------ lib/auth.rb | 6 +-- lib/auto_wizard.rb | 6 +-- lib/calendar_subscriptions.rb | 2 +- lib/email_helper/probe.rb | 4 +- lib/email_helper/verify.rb | 2 +- lib/models.rb | 8 ++-- lib/sessions/event.rb | 4 +- lib/sso.rb | 6 +-- 23 files changed, 35 insertions(+), 98 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 1d0f7ba47..1a2aba132 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -67,7 +67,7 @@ class SearchController < ApplicationController items = SearchIndexBackend.search(query, objects_with_direct_search_index, limit: limit) items.each do |item| require_dependency item[:type].to_filename - local_class = Kernel.const_get(item[:type]) + local_class = item[:type].constantize record = local_class.lookup(id: item[:id]) next if !record diff --git a/app/controllers/user_access_token_controller.rb b/app/controllers/user_access_token_controller.rb index 123ba2785..4ba010106 100644 --- a/app/controllers/user_access_token_controller.rb +++ b/app/controllers/user_access_token_controller.rb @@ -38,7 +38,7 @@ curl http://localhost/api/v1/user_access_token -v -u #{login}:#{password} local_permissions = current_user.permissions local_permissions_new = {} local_permissions.each_key do |key| - keys = Object.const_get('Permission').with_parents(key) + keys = ::Permission.with_parents(key) keys.each do |local_key| next if local_permissions_new.key?([local_key]) diff --git a/app/models/application_model/can_assets.rb b/app/models/application_model/can_assets.rb index c1f084390..411e37c97 100644 --- a/app/models/application_model/can_assets.rb +++ b/app/models/application_model/can_assets.rb @@ -145,7 +145,7 @@ get assets of object list def assets_of_object_list(list, assets = {}) list.each do |item| require_dependency item['object'].to_filename - record = Kernel.const_get(item['object']).find(item['o_id']) + record = item['object'].constantize.find(item['o_id']) assets = record.assets(assets) if item['created_by_id'].present? user = User.find(item['created_by_id']) diff --git a/app/models/application_model/can_touch_references.rb b/app/models/application_model/can_touch_references.rb index 18037f927..f6a5c4433 100644 --- a/app/models/application_model/can_touch_references.rb +++ b/app/models/application_model/can_touch_references.rb @@ -17,9 +17,7 @@ touch references by params =end def touch_reference_by_params(data) - - object_class = Kernel.const_get(data[:object]) - object = object_class.lookup(id: data[:o_id]) + object = data[:object].constantize.lookup(id: data[:o_id]) return if !object object.touch # rubocop:disable Rails/SkipsModelValidations diff --git a/app/models/channel.rb b/app/models/channel.rb index cd39c781e..6b378b197 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -321,7 +321,7 @@ load channel driver and return class # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html require_dependency "channel/driver/#{adapter.to_filename}" - Object.const_get("::Channel::Driver::#{adapter.to_classname}") + "::Channel::Driver::#{adapter.to_classname}".constantize end =begin diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index ea1f09677..5d39ab21e 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -302,7 +302,7 @@ returns # run postmaster post filter filters = {} Setting.where(area: 'Postmaster::PostFilter').order(:name).each do |setting| - filters[setting.name] = Kernel.const_get(Setting.get(setting.name)) + filters[setting.name] = Setting.get(setting.name).constantize end filters.each_value do |backend| Rails.logger.debug { "run postmaster post filter #{backend}" } @@ -338,7 +338,7 @@ returns key_short = attribute[ attribute.length - 3, attribute.length ] return true if key_short != '_id' - class_object = Object.const_get(class_name.to_classname) + class_object = class_name.to_classname.constantize return if !class_object class_instance = class_object.new diff --git a/app/models/concerns/has_history.rb b/app/models/concerns/has_history.rb index 46bcbc528..2ea369468 100644 --- a/app/models/concerns/has_history.rb +++ b/app/models/concerns/has_history.rb @@ -213,12 +213,12 @@ returns # get related objects history = History.list(self.class.name, self['id'], relation_object, true) history[:list].each do |item| - record = Kernel.const_get(item['object']).find(item['o_id']) + record = item['object'].constantize.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']) + record = item['related_object'].constantize.find(item['related_o_id']) history[:assets] = record.assets(history[:assets]) end end diff --git a/app/models/external_credential.rb b/app/models/external_credential.rb index 829679373..9efd8627b 100644 --- a/app/models/external_credential.rb +++ b/app/models/external_credential.rb @@ -30,7 +30,7 @@ class ExternalCredential < ApplicationModel def self.load_backend(provider) adapter = "ExternalCredential::#{provider.camelcase}" require_dependency adapter.to_filename.to_s - load_adapter(adapter) + adapter.constantize end end diff --git a/app/models/object_manager/attribute.rb b/app/models/object_manager/attribute.rb index e44eaba2b..088eb2bd4 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -629,7 +629,7 @@ to send no browser reload event, pass false execute_db_count = 0 execute_config_count = 0 migrations.each do |attribute| - model = Kernel.const_get(attribute.object_lookup.name) + model = attribute.object_lookup.name.constantize # remove field if attribute.to_delete diff --git a/app/models/observer/transaction.rb b/app/models/observer/transaction.rb index da35c64cb..8cc0e26cf 100644 --- a/app/models/observer/transaction.rb +++ b/app/models/observer/transaction.rb @@ -35,7 +35,7 @@ class Observer::Transaction < ActiveRecord::Observer backend = Setting.get(setting.name) next if params[:disable]&.include?(backend) - sync_backends.push Kernel.const_get(backend) + sync_backends.push backend.constantize end # get uniq objects @@ -132,7 +132,7 @@ class Observer::Transaction < ActiveRecord::Observer end # get current state of objects - object = Kernel.const_get(event[:object]).find_by(id: event[:id]) + object = event[:object].constantize.find_by(id: event[:id]) # next if object is already deleted next if !object diff --git a/app/models/stats_store/search_index.rb b/app/models/stats_store/search_index.rb index 73600834f..386fdadfc 100644 --- a/app/models/stats_store/search_index.rb +++ b/app/models/stats_store/search_index.rb @@ -6,9 +6,7 @@ module StatsStore::SearchIndex return if !attributes begin - return if !Kernel.const_get(attributes['stats_store_object']) - - record = Kernel.const_get(attributes['stats_store_object']).lookup(id: o_id) + record = attributes['stats_store_object'].constantize.lookup(id: o_id) return if !record attributes['stats_store_object_ref'] = record.search_index_attribute_lookup diff --git a/app/models/store/file.rb b/app/models/store/file.rb index 7817b03df..c084ca338 100644 --- a/app/models/store/file.rb +++ b/app/models/store/file.rb @@ -29,7 +29,7 @@ do also verify of written data raise 'Missing storage_provider setting option' end - adapter = load_adapter("Store::Provider::#{adapter_name}") + adapter = "Store::Provider::#{adapter_name}".constantize adapter.add(data, sha) file = Store::File.create( provider: adapter_name, @@ -59,8 +59,7 @@ read content of a file =end def content - adapter = self.class.load_adapter("Store::Provider::#{provider}") - adapter.get(sha) + "Store::Provider::#{provider}".constantize.get(sha) end =begin @@ -117,8 +116,8 @@ nice move to keep system responsive =end def self.move(source, target, delay = nil) - adapter_source = load_adapter("Store::Provider::#{source}") - adapter_target = load_adapter("Store::Provider::#{target}") + adapter_source = "Store::Provider::#{source}".constantize + adapter_target = "Store::Provider::#{target}".constantize file_ids = Store::File.all.pluck(:id) file_ids.each do |item_id| @@ -146,8 +145,7 @@ nice move to keep system responsive private def destroy_provider - adapter = self.class.load_adapter("Store::Provider::#{provider}") - adapter.delete(sha) + "Store::Provider::#{provider}".constantize.delete(sha) end end end diff --git a/app/models/ticket/number.rb b/app/models/ticket/number.rb index 58fee6bc5..66070816c 100644 --- a/app/models/ticket/number.rb +++ b/app/models/ticket/number.rb @@ -46,15 +46,8 @@ returns # load backend based on config adapter_name = Setting.get('ticket_number') - if !adapter_name - raise 'Missing ticket_number setting option' - end + raise 'Missing ticket_number setting option' if !adapter_name - adapter = load_adapter(adapter_name) - if !adapter - raise "Can't load ticket_number adapter '#{adapter_name}'" - end - - adapter + adapter_name.constantize end end diff --git a/app/models/transaction/background_job.rb b/app/models/transaction/background_job.rb index f0f4a57ec..5b7680f20 100644 --- a/app/models/transaction/background_job.rb +++ b/app/models/transaction/background_job.rb @@ -27,8 +27,7 @@ class Transaction::BackgroundJob backend = Setting.get(setting.name) next if @params[:disable]&.include?(backend) - backend = Kernel.const_get(backend) - Observer::Transaction.execute_singel_backend(backend, @item, @params) + Observer::Transaction.execute_singel_backend(backend.constantize, @item, @params) end end diff --git a/lib/application_lib.rb b/lib/application_lib.rb index 7b3ace036..87a857a40 100644 --- a/lib/application_lib.rb +++ b/lib/application_lib.rb @@ -21,42 +21,7 @@ returns adapter = Setting.get(setting) return if adapter.blank? - # load backend - load_adapter(adapter) - end - -=begin - -load adapter - - result = self.load_adapter('Some::Classname') - -returns - - result = Some::Classname - -=end - - def load_adapter(adapter) - - # load adapter - - # will only work on ruby 2.0 - #Object.const_get(adapter) - - # will work on ruby 1.9 and 2.0 - #adapter.split('::').inject(Object) do |mod, class_name| - # mod.const_get(class_name) - #end - - # will work with active_support - adapter = adapter.constantize - - if !adapter - raise "Can't load adapter '#{adapter_name}'" - end - - adapter + adapter.constantize end end end diff --git a/lib/auth.rb b/lib/auth.rb index ad9dda285..417cd98d2 100644 --- a/lib/auth.rb +++ b/lib/auth.rb @@ -107,11 +107,7 @@ returns def self.backend_validates?(config:, user:, password:) return false if !config[:adapter] - # load backend - backend = load_adapter(config[:adapter]) - return false if !backend - - instance = backend.new(config) + instance = config[:adapter].constantize.new(config) instance.valid?(user, password) end diff --git a/lib/auto_wizard.rb b/lib/auto_wizard.rb index 707cc37dd..b652fb372 100644 --- a/lib/auto_wizard.rb +++ b/lib/auto_wizard.rb @@ -97,9 +97,8 @@ returns next if !auto_wizard_hash[map_name] auto_wizard_hash[map_name].each do |data| - generic_object = Kernel.const_get(model) data.symbolize_keys! - generic_object.create_or_update_with_ref(data) + model.constantize.create_or_update_with_ref(data) end end @@ -141,9 +140,8 @@ returns next if !auto_wizard_hash[map_name] auto_wizard_hash[map_name].each do |data| - generic_object = Kernel.const_get(model) data.symbolize_keys! - generic_object.create_or_update_with_ref(data) + model.constantize.create_or_update_with_ref(data) end end diff --git a/lib/calendar_subscriptions.rb b/lib/calendar_subscriptions.rb index 47d9effbc..2569aa735 100644 --- a/lib/calendar_subscriptions.rb +++ b/lib/calendar_subscriptions.rb @@ -41,7 +41,7 @@ class CalendarSubscriptions events_data = [] if @preferences[ object_name ].present? sub_class_name = object_name.to_s.capitalize - object = Object.const_get("CalendarSubscriptions::#{sub_class_name}") + object = "CalendarSubscriptions::#{sub_class_name}".constantize instance = object.new(@user, @preferences[ object_name ]) method = instance.method(method_name) events_data += method.call diff --git a/lib/email_helper/probe.rb b/lib/email_helper/probe.rb index fcdad877a..4e7b9d5c6 100644 --- a/lib/email_helper/probe.rb +++ b/lib/email_helper/probe.rb @@ -223,7 +223,7 @@ returns on fail begin require_dependency "channel/driver/#{adapter.to_filename}" - driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}") + driver_class = "Channel::Driver::#{adapter.to_classname}".constantize driver_instance = driver_class.new result_inbound = driver_instance.fetch(params[:options], nil, 'check') rescue => e @@ -321,7 +321,7 @@ returns on fail begin require_dependency "channel/driver/#{adapter.to_filename}" - driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}") + driver_class = "Channel::Driver::#{adapter.to_classname}".constantize driver_instance = driver_class.new driver_instance.send( params[:options], diff --git a/lib/email_helper/verify.rb b/lib/email_helper/verify.rb index c05f54d7a..517614b48 100644 --- a/lib/email_helper/verify.rb +++ b/lib/email_helper/verify.rb @@ -82,7 +82,7 @@ or begin require_dependency "channel/driver/#{adapter.to_filename}" - driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}") + driver_class = "Channel::Driver::#{adapter.to_classname}".constantize driver_instance = driver_class.new fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject) rescue => e diff --git a/lib/models.rb b/lib/models.rb index 5b470bd89..8a74e9bfe 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -38,8 +38,7 @@ returns entry.gsub!(dir, '') entry = entry.to_classname - model_class = load_adapter(entry) - next if !model_class + model_class = entry.constantize next if !model_class.respond_to? :new next if !model_class.respond_to? :table_name @@ -118,8 +117,7 @@ returns object_name = object_name.to_s # check if model exists - object_model = load_adapter(object_name) - object_model.find(object_id) + object_name.constantize.find(object_id) list = all references = {} @@ -248,7 +246,7 @@ returns # update references references = references(object_name, object_id_to_merge) references.each do |model, attributes| - model_object = Object.const_get(model) + model_object = model.constantize # collect items and attributes to update items_to_update = {} diff --git a/lib/sessions/event.rb b/lib/sessions/event.rb index 2fd420531..f8012c955 100644 --- a/lib/sessions/event.rb +++ b/lib/sessions/event.rb @@ -2,10 +2,8 @@ class Sessions::Event include ApplicationLib def self.run(params) - adapter = "Sessions::Event::#{params[:event].to_classname}" - begin - backend = load_adapter(adapter) + backend = "Sessions::Event::#{params[:event].to_classname}".constantize rescue => e Rails.logger.error e.inspect Rails.logger.error e.backtrace diff --git a/lib/sso.rb b/lib/sso.rb index 9f653f36c..6acae6579 100644 --- a/lib/sso.rb +++ b/lib/sso.rb @@ -36,11 +36,7 @@ returns config.each do |config_item| next if !config_item[:adapter] - # load backend - backend = load_adapter( config_item[:adapter] ) - next if !backend - - user_auth = backend.check( params, config_item ) + user_auth = config_item[:adapter].constantize.check( params, config_item ) # auth not ok next if !user_auth