Refactoring: Migrated Kernel.const_get to Rails .constantize.
This commit is contained in:
parent
8df6e3c33b
commit
b2c065812f
23 changed files with 35 additions and 98 deletions
|
@ -67,7 +67,7 @@ class SearchController < ApplicationController
|
||||||
items = SearchIndexBackend.search(query, objects_with_direct_search_index, limit: limit)
|
items = SearchIndexBackend.search(query, objects_with_direct_search_index, limit: limit)
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
require_dependency item[:type].to_filename
|
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])
|
record = local_class.lookup(id: item[:id])
|
||||||
next if !record
|
next if !record
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ curl http://localhost/api/v1/user_access_token -v -u #{login}:#{password}
|
||||||
local_permissions = current_user.permissions
|
local_permissions = current_user.permissions
|
||||||
local_permissions_new = {}
|
local_permissions_new = {}
|
||||||
local_permissions.each_key do |key|
|
local_permissions.each_key do |key|
|
||||||
keys = Object.const_get('Permission').with_parents(key)
|
keys = ::Permission.with_parents(key)
|
||||||
keys.each do |local_key|
|
keys.each do |local_key|
|
||||||
next if local_permissions_new.key?([local_key])
|
next if local_permissions_new.key?([local_key])
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,7 @@ get assets of object list
|
||||||
def assets_of_object_list(list, assets = {})
|
def assets_of_object_list(list, assets = {})
|
||||||
list.each do |item|
|
list.each do |item|
|
||||||
require_dependency item['object'].to_filename
|
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)
|
assets = record.assets(assets)
|
||||||
if item['created_by_id'].present?
|
if item['created_by_id'].present?
|
||||||
user = User.find(item['created_by_id'])
|
user = User.find(item['created_by_id'])
|
||||||
|
|
|
@ -17,9 +17,7 @@ touch references by params
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def touch_reference_by_params(data)
|
def touch_reference_by_params(data)
|
||||||
|
object = data[:object].constantize.lookup(id: data[:o_id])
|
||||||
object_class = Kernel.const_get(data[:object])
|
|
||||||
object = object_class.lookup(id: data[:o_id])
|
|
||||||
return if !object
|
return if !object
|
||||||
|
|
||||||
object.touch # rubocop:disable Rails/SkipsModelValidations
|
object.touch # rubocop:disable Rails/SkipsModelValidations
|
||||||
|
|
|
@ -321,7 +321,7 @@ load channel driver and return class
|
||||||
# http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
|
# http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
|
||||||
require_dependency "channel/driver/#{adapter.to_filename}"
|
require_dependency "channel/driver/#{adapter.to_filename}"
|
||||||
|
|
||||||
Object.const_get("::Channel::Driver::#{adapter.to_classname}")
|
"::Channel::Driver::#{adapter.to_classname}".constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
|
@ -302,7 +302,7 @@ returns
|
||||||
# run postmaster post filter
|
# run postmaster post filter
|
||||||
filters = {}
|
filters = {}
|
||||||
Setting.where(area: 'Postmaster::PostFilter').order(:name).each do |setting|
|
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
|
end
|
||||||
filters.each_value do |backend|
|
filters.each_value do |backend|
|
||||||
Rails.logger.debug { "run postmaster post filter #{backend}" }
|
Rails.logger.debug { "run postmaster post filter #{backend}" }
|
||||||
|
@ -338,7 +338,7 @@ returns
|
||||||
key_short = attribute[ attribute.length - 3, attribute.length ]
|
key_short = attribute[ attribute.length - 3, attribute.length ]
|
||||||
return true if key_short != '_id'
|
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
|
return if !class_object
|
||||||
|
|
||||||
class_instance = class_object.new
|
class_instance = class_object.new
|
||||||
|
|
|
@ -213,12 +213,12 @@ returns
|
||||||
# get related objects
|
# get related objects
|
||||||
history = History.list(self.class.name, self['id'], relation_object, true)
|
history = History.list(self.class.name, self['id'], relation_object, true)
|
||||||
history[:list].each do |item|
|
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])
|
history[:assets] = record.assets(history[:assets])
|
||||||
|
|
||||||
if item['related_object']
|
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])
|
history[:assets] = record.assets(history[:assets])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,7 +30,7 @@ class ExternalCredential < ApplicationModel
|
||||||
def self.load_backend(provider)
|
def self.load_backend(provider)
|
||||||
adapter = "ExternalCredential::#{provider.camelcase}"
|
adapter = "ExternalCredential::#{provider.camelcase}"
|
||||||
require_dependency adapter.to_filename.to_s
|
require_dependency adapter.to_filename.to_s
|
||||||
load_adapter(adapter)
|
adapter.constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -629,7 +629,7 @@ to send no browser reload event, pass false
|
||||||
execute_db_count = 0
|
execute_db_count = 0
|
||||||
execute_config_count = 0
|
execute_config_count = 0
|
||||||
migrations.each do |attribute|
|
migrations.each do |attribute|
|
||||||
model = Kernel.const_get(attribute.object_lookup.name)
|
model = attribute.object_lookup.name.constantize
|
||||||
|
|
||||||
# remove field
|
# remove field
|
||||||
if attribute.to_delete
|
if attribute.to_delete
|
||||||
|
|
|
@ -35,7 +35,7 @@ class Observer::Transaction < ActiveRecord::Observer
|
||||||
backend = Setting.get(setting.name)
|
backend = Setting.get(setting.name)
|
||||||
next if params[:disable]&.include?(backend)
|
next if params[:disable]&.include?(backend)
|
||||||
|
|
||||||
sync_backends.push Kernel.const_get(backend)
|
sync_backends.push backend.constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
# get uniq objects
|
# get uniq objects
|
||||||
|
@ -132,7 +132,7 @@ class Observer::Transaction < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
|
|
||||||
# get current state of objects
|
# 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 is already deleted
|
||||||
next if !object
|
next if !object
|
||||||
|
|
|
@ -6,9 +6,7 @@ module StatsStore::SearchIndex
|
||||||
return if !attributes
|
return if !attributes
|
||||||
|
|
||||||
begin
|
begin
|
||||||
return if !Kernel.const_get(attributes['stats_store_object'])
|
record = attributes['stats_store_object'].constantize.lookup(id: o_id)
|
||||||
|
|
||||||
record = Kernel.const_get(attributes['stats_store_object']).lookup(id: o_id)
|
|
||||||
return if !record
|
return if !record
|
||||||
|
|
||||||
attributes['stats_store_object_ref'] = record.search_index_attribute_lookup
|
attributes['stats_store_object_ref'] = record.search_index_attribute_lookup
|
||||||
|
|
|
@ -29,7 +29,7 @@ do also verify of written data
|
||||||
raise 'Missing storage_provider setting option'
|
raise 'Missing storage_provider setting option'
|
||||||
end
|
end
|
||||||
|
|
||||||
adapter = load_adapter("Store::Provider::#{adapter_name}")
|
adapter = "Store::Provider::#{adapter_name}".constantize
|
||||||
adapter.add(data, sha)
|
adapter.add(data, sha)
|
||||||
file = Store::File.create(
|
file = Store::File.create(
|
||||||
provider: adapter_name,
|
provider: adapter_name,
|
||||||
|
@ -59,8 +59,7 @@ read content of a file
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def content
|
def content
|
||||||
adapter = self.class.load_adapter("Store::Provider::#{provider}")
|
"Store::Provider::#{provider}".constantize.get(sha)
|
||||||
adapter.get(sha)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
@ -117,8 +116,8 @@ nice move to keep system responsive
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.move(source, target, delay = nil)
|
def self.move(source, target, delay = nil)
|
||||||
adapter_source = load_adapter("Store::Provider::#{source}")
|
adapter_source = "Store::Provider::#{source}".constantize
|
||||||
adapter_target = load_adapter("Store::Provider::#{target}")
|
adapter_target = "Store::Provider::#{target}".constantize
|
||||||
|
|
||||||
file_ids = Store::File.all.pluck(:id)
|
file_ids = Store::File.all.pluck(:id)
|
||||||
file_ids.each do |item_id|
|
file_ids.each do |item_id|
|
||||||
|
@ -146,8 +145,7 @@ nice move to keep system responsive
|
||||||
private
|
private
|
||||||
|
|
||||||
def destroy_provider
|
def destroy_provider
|
||||||
adapter = self.class.load_adapter("Store::Provider::#{provider}")
|
"Store::Provider::#{provider}".constantize.delete(sha)
|
||||||
adapter.delete(sha)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,15 +46,8 @@ returns
|
||||||
|
|
||||||
# load backend based on config
|
# load backend based on config
|
||||||
adapter_name = Setting.get('ticket_number')
|
adapter_name = Setting.get('ticket_number')
|
||||||
if !adapter_name
|
raise 'Missing ticket_number setting option' if !adapter_name
|
||||||
raise 'Missing ticket_number setting option'
|
|
||||||
end
|
|
||||||
|
|
||||||
adapter = load_adapter(adapter_name)
|
adapter_name.constantize
|
||||||
if !adapter
|
|
||||||
raise "Can't load ticket_number adapter '#{adapter_name}'"
|
|
||||||
end
|
|
||||||
|
|
||||||
adapter
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,8 +27,7 @@ class Transaction::BackgroundJob
|
||||||
backend = Setting.get(setting.name)
|
backend = Setting.get(setting.name)
|
||||||
next if @params[:disable]&.include?(backend)
|
next if @params[:disable]&.include?(backend)
|
||||||
|
|
||||||
backend = Kernel.const_get(backend)
|
Observer::Transaction.execute_singel_backend(backend.constantize, @item, @params)
|
||||||
Observer::Transaction.execute_singel_backend(backend, @item, @params)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,42 +21,7 @@ returns
|
||||||
adapter = Setting.get(setting)
|
adapter = Setting.get(setting)
|
||||||
return if adapter.blank?
|
return if adapter.blank?
|
||||||
|
|
||||||
# load backend
|
adapter.constantize
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -107,11 +107,7 @@ returns
|
||||||
def self.backend_validates?(config:, user:, password:)
|
def self.backend_validates?(config:, user:, password:)
|
||||||
return false if !config[:adapter]
|
return false if !config[:adapter]
|
||||||
|
|
||||||
# load backend
|
instance = config[:adapter].constantize.new(config)
|
||||||
backend = load_adapter(config[:adapter])
|
|
||||||
return false if !backend
|
|
||||||
|
|
||||||
instance = backend.new(config)
|
|
||||||
|
|
||||||
instance.valid?(user, password)
|
instance.valid?(user, password)
|
||||||
end
|
end
|
||||||
|
|
|
@ -97,9 +97,8 @@ returns
|
||||||
next if !auto_wizard_hash[map_name]
|
next if !auto_wizard_hash[map_name]
|
||||||
|
|
||||||
auto_wizard_hash[map_name].each do |data|
|
auto_wizard_hash[map_name].each do |data|
|
||||||
generic_object = Kernel.const_get(model)
|
|
||||||
data.symbolize_keys!
|
data.symbolize_keys!
|
||||||
generic_object.create_or_update_with_ref(data)
|
model.constantize.create_or_update_with_ref(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -141,9 +140,8 @@ returns
|
||||||
next if !auto_wizard_hash[map_name]
|
next if !auto_wizard_hash[map_name]
|
||||||
|
|
||||||
auto_wizard_hash[map_name].each do |data|
|
auto_wizard_hash[map_name].each do |data|
|
||||||
generic_object = Kernel.const_get(model)
|
|
||||||
data.symbolize_keys!
|
data.symbolize_keys!
|
||||||
generic_object.create_or_update_with_ref(data)
|
model.constantize.create_or_update_with_ref(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ class CalendarSubscriptions
|
||||||
events_data = []
|
events_data = []
|
||||||
if @preferences[ object_name ].present?
|
if @preferences[ object_name ].present?
|
||||||
sub_class_name = object_name.to_s.capitalize
|
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 ])
|
instance = object.new(@user, @preferences[ object_name ])
|
||||||
method = instance.method(method_name)
|
method = instance.method(method_name)
|
||||||
events_data += method.call
|
events_data += method.call
|
||||||
|
|
|
@ -223,7 +223,7 @@ returns on fail
|
||||||
begin
|
begin
|
||||||
require_dependency "channel/driver/#{adapter.to_filename}"
|
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 = driver_class.new
|
||||||
result_inbound = driver_instance.fetch(params[:options], nil, 'check')
|
result_inbound = driver_instance.fetch(params[:options], nil, 'check')
|
||||||
rescue => e
|
rescue => e
|
||||||
|
@ -321,7 +321,7 @@ returns on fail
|
||||||
begin
|
begin
|
||||||
require_dependency "channel/driver/#{adapter.to_filename}"
|
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 = driver_class.new
|
||||||
driver_instance.send(
|
driver_instance.send(
|
||||||
params[:options],
|
params[:options],
|
||||||
|
|
|
@ -82,7 +82,7 @@ or
|
||||||
begin
|
begin
|
||||||
require_dependency "channel/driver/#{adapter.to_filename}"
|
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 = driver_class.new
|
||||||
fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
|
fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
|
||||||
rescue => e
|
rescue => e
|
||||||
|
|
|
@ -38,8 +38,7 @@ returns
|
||||||
|
|
||||||
entry.gsub!(dir, '')
|
entry.gsub!(dir, '')
|
||||||
entry = entry.to_classname
|
entry = entry.to_classname
|
||||||
model_class = load_adapter(entry)
|
model_class = entry.constantize
|
||||||
next if !model_class
|
|
||||||
next if !model_class.respond_to? :new
|
next if !model_class.respond_to? :new
|
||||||
next if !model_class.respond_to? :table_name
|
next if !model_class.respond_to? :table_name
|
||||||
|
|
||||||
|
@ -118,8 +117,7 @@ returns
|
||||||
object_name = object_name.to_s
|
object_name = object_name.to_s
|
||||||
|
|
||||||
# check if model exists
|
# check if model exists
|
||||||
object_model = load_adapter(object_name)
|
object_name.constantize.find(object_id)
|
||||||
object_model.find(object_id)
|
|
||||||
|
|
||||||
list = all
|
list = all
|
||||||
references = {}
|
references = {}
|
||||||
|
@ -248,7 +246,7 @@ returns
|
||||||
# update references
|
# update references
|
||||||
references = references(object_name, object_id_to_merge)
|
references = references(object_name, object_id_to_merge)
|
||||||
references.each do |model, attributes|
|
references.each do |model, attributes|
|
||||||
model_object = Object.const_get(model)
|
model_object = model.constantize
|
||||||
|
|
||||||
# collect items and attributes to update
|
# collect items and attributes to update
|
||||||
items_to_update = {}
|
items_to_update = {}
|
||||||
|
|
|
@ -2,10 +2,8 @@ class Sessions::Event
|
||||||
include ApplicationLib
|
include ApplicationLib
|
||||||
|
|
||||||
def self.run(params)
|
def self.run(params)
|
||||||
adapter = "Sessions::Event::#{params[:event].to_classname}"
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
backend = load_adapter(adapter)
|
backend = "Sessions::Event::#{params[:event].to_classname}".constantize
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error e.inspect
|
Rails.logger.error e.inspect
|
||||||
Rails.logger.error e.backtrace
|
Rails.logger.error e.backtrace
|
||||||
|
|
|
@ -36,11 +36,7 @@ returns
|
||||||
config.each do |config_item|
|
config.each do |config_item|
|
||||||
next if !config_item[:adapter]
|
next if !config_item[:adapter]
|
||||||
|
|
||||||
# load backend
|
user_auth = config_item[:adapter].constantize.check( params, config_item )
|
||||||
backend = load_adapter( config_item[:adapter] )
|
|
||||||
next if !backend
|
|
||||||
|
|
||||||
user_auth = backend.check( params, config_item )
|
|
||||||
|
|
||||||
# auth not ok
|
# auth not ok
|
||||||
next if !user_auth
|
next if !user_auth
|
||||||
|
|
Loading…
Reference in a new issue