|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mongoid |
| 4 | + |
| 5 | + # Defines how Mongoid can autoload all defined models. |
| 6 | + module Loadable |
| 7 | + # The default list of paths where model classes should be looked for. If |
| 8 | + # Rails is present, the "app/models" paths will be used instead. |
| 9 | + # (See #model_paths.) |
| 10 | + DEFAULT_MODEL_PATHS = %w( ./app/models ./lib/models ).freeze |
| 11 | + |
| 12 | + # Search a list of model paths to get every model and require it, so |
| 13 | + # that indexing and inheritance work in both development and production |
| 14 | + # with the same results. |
| 15 | + # |
| 16 | + # @example Load all the application models from default model paths. |
| 17 | + # Mongoid.load_models |
| 18 | + # |
| 19 | + # @example Load all application models from a non-standard set of paths. |
| 20 | + # Mongoid.load_models(%w( ./models ./admin/models )) |
| 21 | + # |
| 22 | + # @param [ Array ] paths The list of paths that should be looked in |
| 23 | + # for model files. These must either be absolute paths, or relative to |
| 24 | + # the current working directory. |
| 25 | + def load_models(paths = model_paths) |
| 26 | + paths.each do |path| |
| 27 | + if preload_models.resizable? |
| 28 | + files = preload_models.map { |model| "#{path}/#{model.underscore}.rb" } |
| 29 | + else |
| 30 | + files = Dir.glob("#{path}/**/*.rb") |
| 31 | + end |
| 32 | + |
| 33 | + files.sort.each do |file| |
| 34 | + load_model(file.gsub(/^#{path}\// , "").gsub(/\.rb$/, "")) |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # A convenience method for loading a model's file. If Rails' |
| 40 | + # `require_dependency` method exists, it will be used; otherwise |
| 41 | + # `require` will be used. |
| 42 | + # |
| 43 | + # @example Load the model. |
| 44 | + # Mongoid.load_model("/mongoid/behavior") |
| 45 | + # |
| 46 | + # @param [ String ] file The base filename. |
| 47 | + # |
| 48 | + # @api private |
| 49 | + def load_model(file) |
| 50 | + if defined?(require_dependency) |
| 51 | + require_dependency(file) |
| 52 | + else |
| 53 | + require(file) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # Returns the array of paths where the application's model definitions |
| 58 | + # are located. If Rails is loaded, this defaults to the configured |
| 59 | + # "app/models" paths (e.g. `config.paths["app/models"]`); otherwise, it |
| 60 | + # defaults to `%w(./app/models ./lib/models)`. |
| 61 | + # |
| 62 | + # Note that these paths are the *roots* of the directory hierarchies where |
| 63 | + # the models are located; it is not necessary to indicate every subdirectory, |
| 64 | + # as long as these root paths are located in `$LOAD_PATH`. |
| 65 | + # |
| 66 | + # @return [ Array<String> ] the array of model paths |
| 67 | + def model_paths |
| 68 | + @model_paths ||= defined?(Rails) ? |
| 69 | + Rails.application.config.paths["app/models"].expanded : |
| 70 | + DEFAULT_MODEL_PATHS |
| 71 | + end |
| 72 | + |
| 73 | + # Sets the model paths to the given array of paths. These are the paths |
| 74 | + # where the application's model definitions are located. |
| 75 | + # |
| 76 | + # @param [ Array<String> ] paths The list of model paths |
| 77 | + def model_paths=(paths) |
| 78 | + @model_paths = paths |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | +end |
0 commit comments