Module: Mongoid::Loadable

Included in:
Mongoid
Defined in:
lib/mongoid/loadable.rb

Overview

Defines how Mongoid can autoload all defined models.

Constant Summary collapse

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.)

%w[ ./app/models ./lib/models ].freeze
DEFAULT_IGNORE_PATTERNS =

The default list of glob patterns that match paths to ignore when loading models. Defaults to '/models/concerns/', which Rails uses for extensions to models (and which cause errors when loaded out of order).

See #ignore_patterns.

%w[ */models/concerns/* ].freeze

Instance Method Summary collapse

Instance Method Details

#files_under_path(path) ⇒ Array<String>

Given a single path, returns all ruby files under that path (or, if preload_models is a list of model names, returns only the files for those named models).



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongoid/loadable.rb', line 61

def files_under_path(path)
  files = if preload_models.resizable?
            preload_models
              .map { |model| "#{path}/#{model.underscore}.rb" }
              .select { |file_name| File.exist?(file_name) }
          else
            Dir.glob("#{path}/**/*.rb")
               .reject { |file_name| ignored?(file_name) }
          end

  # strip the path and the suffix from each entry
  files.map { |file| file.gsub(%r{^#{path}/}, '').gsub(/\.rb$/, '') }
end

#files_under_paths(paths) ⇒ Array<String>

Given a list of paths, return all ruby files under that path (or, if preload_models is a list of model names, returns only the files for those named models).



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

def files_under_paths(paths)
  paths.flat_map { |path| files_under_path(path) }
end

#ignore_patternsArray<String>

Returns the array of glob patterns that determine whether a given path should be ignored by the model loader.



115
116
117
# File 'lib/mongoid/loadable.rb', line 115

def ignore_patterns
  @ignore_patterns ||= DEFAULT_IGNORE_PATTERNS.dup
end

#ignore_patterns=(patterns) ⇒ Object

Sets the ignore patterns to the given array of patterns. These are glob patterns that determine whether a given path should be ignored by the model loader or not.



132
133
134
# File 'lib/mongoid/loadable.rb', line 132

def ignore_patterns=(patterns)
  @ignore_patterns = patterns
end

#ignored?(file_path) ⇒ true | false

Returns true if the given file path matches any of the ignore patterns.



142
143
144
# File 'lib/mongoid/loadable.rb', line 142

def ignored?(file_path)
  ignore_patterns.any? { |pattern| File.fnmatch?(pattern, file_path) }
end

#load_model(file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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")


85
86
87
88
89
90
91
# File 'lib/mongoid/loadable.rb', line 85

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

#load_models(paths = model_paths) ⇒ Object

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


31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/loadable.rb', line 31

def load_models(paths = model_paths)
  files = files_under_paths(paths)

  files.sort.each do |file|
    load_model(file)
  end

  nil
end

#model_pathsArray<String>

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["app/models"]); 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.



103
104
105
106
107
108
109
# File 'lib/mongoid/loadable.rb', line 103

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

#model_paths=(paths) ⇒ Object

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



123
124
125
# File 'lib/mongoid/loadable.rb', line 123

def model_paths=(paths)
  @model_paths = paths
end