Class: Object

Inherits:
BasicObject
Defined in:
lib/brick/extensions.rb

Class Method Summary collapse

Class Method Details

._brick_const_missingObject



430
# File 'lib/brick/extensions.rb', line 430

alias _brick_const_missing const_missing

.const_missing(*args) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/brick/extensions.rb', line 431

def const_missing(*args)
  return self.const_get(args.first) if self.const_defined?(args.first)
  return Object.const_get(args.first) if Object.const_defined?(args.first) unless self == Object

  class_name = args.first.to_s
  # See if a file is there in the same way that ActiveSupport::Dependencies#load_missing_constant
  # checks for it in ~/.rvm/gems/ruby-2.7.5/gems/activesupport-5.2.6.2/lib/active_support/dependencies.rb
  # that is, checking #qualified_name_for with:  from_mod, const_name
  # If we want to support namespacing in the future, might have to utilise something like this:
  # path_suffix = ActiveSupport::Dependencies.qualified_name_for(Object, args.first).underscore
  # return self._brick_const_missing(*args) if ActiveSupport::Dependencies.search_for_file(path_suffix)
  # If the file really exists, go and snag it:
  if !(is_found = ActiveSupport::Dependencies.search_for_file(class_name.underscore)) && (filepath = self.name&.split('::'))
    filepath = (filepath[0..-2] + [class_name]).join('/').underscore + '.rb'
  end
  if is_found
    return self._brick_const_missing(*args)
  elsif ActiveSupport::Dependencies.search_for_file(filepath) # Last-ditch effort to pick this thing up before we fill in the gaps on our own
    my_const = parent.const_missing(class_name) # ends up having:  MyModule::MyClass
    return my_const
  end

  relations = ::Brick.instance_variable_get(:@relations)[ActiveRecord::Base.connection_pool.object_id] || {}
  result = if ::Brick.enable_controllers? && class_name.end_with?('Controller') && (plural_class_name = class_name[0..-11]).length.positive?
             # Otherwise now it's up to us to fill in the gaps
             if (model = plural_class_name.singularize.constantize)
               # if it's a controller and no match or a model doesn't really use the same table name, eager load all models and try to find a model class of the right name.
               build_controller(class_name, plural_class_name, model, relations)
             end
           elsif ::Brick.enable_models?
             # See if a file is there in the same way that ActiveSupport::Dependencies#load_missing_constant
             # checks for it in ~/.rvm/gems/ruby-2.7.5/gems/activesupport-5.2.6.2/lib/active_support/dependencies.rb
             plural_class_name = ActiveSupport::Inflector.pluralize(model_name = class_name)
             singular_table_name = ActiveSupport::Inflector.underscore(model_name)
 
             # Adjust for STI if we know of a base model for the requested model name
             table_name = if (base_model = ::Brick.sti_models[model_name]&.fetch(:base, nil))
                            base_model.table_name
                          else
                            ActiveSupport::Inflector.pluralize(singular_table_name)
                          end
 
             # Maybe, just maybe there's a database table that will satisfy this need
             if (matching = [table_name, singular_table_name, plural_class_name, model_name].find { |m| relations.key?(m) })
               build_model(model_name, singular_table_name, table_name, relations, matching)
             end
           end
  if result
    built_class, code = result
    puts "\n#{code}"
    built_class
  elsif ::Brick.config.sti_namespace_prefixes&.key?("::#{class_name}")
#         module_prefixes = type_name.split('::')
#         path = self.name.split('::')[0..-2] + []
#         module_prefixes.unshift('') unless module_prefixes.first.blank?
#         candidate_file = Rails.root.join('app/models' + module_prefixes.map(&:underscore).join('/') + '.rb')
    self._brick_const_missing(*args)
  else
    puts "MISSING! #{self.name} #{args.inspect} #{table_name}"
    self._brick_const_missing(*args)
  end
end