Refactoring: Migrated Kernel.const_get to Rails .constantize.

This commit is contained in:
Thorsten Eckel 2019-01-06 19:41:29 +01:00
parent 8df6e3c33b
commit b2c065812f
23 changed files with 35 additions and 98 deletions

View file

@ -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

View file

@ -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])

View file

@ -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'])

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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],

View file

@ -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

View file

@ -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 = {}

View file

@ -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

View file

@ -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