Skip to content

Commit

Permalink
Zeitwerk groundwork
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka authored and ofedoren committed Jul 1, 2022
1 parent a439e1e commit 327d617
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/helpers/host_description_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module HostDescriptionHelper
UI.register_host_description do
::UI.register_host_description do
multiple_actions_provider :base_multiple_actions
overview_fields_provider :base_status_overview_fields
overview_fields_provider :base_host_overview_fields
Expand Down
29 changes: 29 additions & 0 deletions app/models/concerns/foreman/sti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Foreman
module STI
def self.prepended(base)
class << base
cattr_accessor :preloaded, instance_accessor: false
prepend ClassMethods
end
end
Expand All @@ -27,5 +28,33 @@ def save(*args)
ensure
self.class.instance_variable_set("@finder_needs_type_condition", :true) if type_changed
end

def descendants
preload_sti unless preloaded
super
end

# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
puts "#{self}: #{base_class.connected?} #{base_class.table_exists?}"
return [] unless base_class.connected? && base_class.table_exists?
types_in_db = base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact

types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end

self.preloaded = true
end
end
end
2 changes: 2 additions & 0 deletions app/models/concerns/nested_ancestry_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ def update_matchers
lookup_values.update_all(:match => "#{obj_type}=#{title}")
end
end

require_dependency 'nested_ancestry_common/search'
2 changes: 1 addition & 1 deletion app/models/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.inherited(child)
end
child.const_set('Jail', jail_class)
end
child.send(:include, NestedAncestryCommon::Search)
child.send(:include, ::NestedAncestryCommon::Search)
super
end

Expand Down
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Application < Rails::Application
Dir["#{Rails.root}/config/routes/**/*.rb"].each do |route_file|
config.paths['config/routes.rb'] << route_file
end

config.load_defaults 6.0
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand All @@ -115,7 +115,7 @@ class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/app/models/compute_resources)
config.autoload_paths += %W(#{config.root}/app/models/fact_names)
config.autoload_paths += %W(#{config.root}/app/models/lookup_keys)
config.autoload_paths += %W(#{config.root}/app/models/host_status)
# config.autoload_paths += %W(#{config.root}/app/models/host_status)
config.autoload_paths += %W(#{config.root}/app/models/operatingsystems)
config.autoload_paths += %W(#{config.root}/app/models/parameters)
config.autoload_paths += %W(#{config.root}/app/models/taxonomies)
Expand Down
17 changes: 17 additions & 0 deletions config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Rails.autoloaders.each do |autoloader|
autoloader.inflector = Zeitwerk::Inflector.new
autoloader.inflector.inflect(
'ui' => 'UI',
'proxy_api' => 'ProxyAPI',
'sti' => 'STI',
'dhcp' => 'DHCP',
'dns' => 'DNS',
'tftp' => 'TFTP',
'external_ipam' => 'ExternalIPAM',
'bmc' => 'BMC',
'ui_notifications' => 'UINotifications',
'ssh_provision' => 'SSHProvision',
'ipam' => 'IPAM'
)
end
Rails.autoloaders.log!
2 changes: 1 addition & 1 deletion db/migrate/20140902102858_convert_ipam_to_string.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ConvertIpamToString < ActiveRecord::Migration[4.2]
def up
change_column :subnets, :ipam, :string, :default => IPAM::MODES[:dhcp], :null => false, :limit => 255
change_column :subnets, :ipam, :string, :default => ::IPAM::MODES[:dhcp], :null => false, :limit => 255
end

def down
Expand Down

0 comments on commit 327d617

Please sign in to comment.