123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Loadable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/mongoid/loadable.rb

Overview

Defines how ::Mongoid can autoload all defined models.

Constant Summary

  • DEFAULT_MODEL_PATHS =

    The default list of paths where model classes should be looked for. If ::Rails is present, the “app/models” paths will be used instead. (See #model_paths.)

    # File 'lib/mongoid/loadable.rb', line 11
    %w( ./app/models ./lib/models ).freeze

Instance Attribute Summary

Instance Method Summary

  • #load_model(file) Internal use only Internal use only

    A convenience method for loading a model’s file.

  • #load_models(paths = model_paths)

    Search a list of model paths to get every model and require it, so that indexing and inheritance work in both development and production with the same results.

Instance Attribute Details

#model_pathsArray<String> (rw)

Returns the array of paths where the application’s model definitions are located. If Rails is loaded, this defaults to the configured “app/models” paths (e.g. ‘config.paths`); otherwise, it defaults to `%w(./app/models ./lib/models)`.

Note that these paths are the roots of the directory hierarchies where the models are located; it is not necessary to indicate every subdirectory, as long as these root paths are located in ‘$LOAD_PATH`.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/loadable.rb', line 68

def model_paths
  @model_paths ||= defined?(Rails) ?
    Rails.application.config.paths["app/models"].expanded :
    DEFAULT_MODEL_PATHS
end

#model_paths=(paths) (rw)

Sets the model paths to the given array of paths. These are the paths where the application’s model definitions are located.

Parameters:

[ GitHub ]

  
# File 'lib/mongoid/loadable.rb', line 78

def model_paths=(paths)
  @model_paths = paths
end

Instance Method Details

#load_model(file)

This method is for internal use only.

A convenience method for loading a model’s file. If Rails’ ‘require_dependency` method exists, it will be used; otherwise require will be used.

Examples:

Load the model.

Mongoid.load_model("/mongoid/behavior")

Parameters:

  • file (String)

    The base filename.

[ GitHub ]

  
# File 'lib/mongoid/loadable.rb', line 50

def load_model(file)
  if defined?(require_dependency)
    require_dependency(file)
  else
    require(file)
  end
end

#load_models(paths = model_paths)

Search a list of model paths to get every model and require it, so that indexing and inheritance work in both development and production with the same results.

Examples:

Load all the application models from default model paths.

Mongoid.load_models

Load all application models from a non-standard set of paths.

Mongoid.load_models(%w( ./models ./admin/models ))

Parameters:

  • paths (Array) (defaults to: model_paths)

    The list of paths that should be looked in for model files. These must either be absolute paths, or relative to the current working directory.

[ GitHub ]

  
# File 'lib/mongoid/loadable.rb', line 26

def load_models(paths = model_paths)
  paths.each do |path|
    if preload_models.resizable?
      files = preload_models.map { |model| "#{path}/#{model.underscore}.rb" }
    else
      files = Dir.glob("#{path}/**/*.rb")
    end

    files.sort.each do |file|
      load_model(file.gsub(/^#{path}\// , "").gsub(/\.rb$/, ""))
    end
  end
end