Sha256: 9edf6989e84c0329003fdb93ddb1f48a426a8bb3b0ee701e3764c77650307160

Contents?: true

Size: 1.51 KB

Versions: 25

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Rails
  module Mongoid
    extend self

    # Use the application configuration to get every model and require it, so
    # that indexing and inheritance work in both development and production
    # with the same results.
    #
    # @example Load all the application models.
    #   Rails::Mongoid.load_models(app)
    #
    # @param [ Application ] app The rails application.
    def load_models(app)
      app.config.paths["app/models"].expanded.each do |path|
        preload = ::Mongoid.preload_models
        if preload.resizable?
          files = preload.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

    # Conditionally calls `Rails::Mongoid.load_models(app)` if the
    # `::Mongoid.preload_models` is `true`.
    #
    # @param [ Application ] app The rails application.
    def preload_models(app)
      load_models(app) if ::Mongoid.preload_models
    end

    private

    # I don't want to mock out kernel for unit testing purposes, so added this
    # method as a convenience.
    #
    # @example Load the model.
    #   Mongoid.load_model("/mongoid/behavior")
    #
    # @param [ String ] file The base filename.
    def load_model(file)
      begin
        require_dependency(file)
      rescue Exception => e
        Logger.new(STDERR).warn(e.message)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
mongoid-8.1.8 lib/rails/mongoid.rb
mongoid-8.1.7 lib/rails/mongoid.rb
mongoid-8.1.6 lib/rails/mongoid.rb
mongoid-8.0.8 lib/rails/mongoid.rb
mongoid-8.1.5 lib/rails/mongoid.rb
mongoid-8.1.4 lib/rails/mongoid.rb
mongoid-8.0.7 lib/rails/mongoid.rb
mongoid-8.1.3 lib/rails/mongoid.rb
mongoid-8.1.2 lib/rails/mongoid.rb
mongoid-8.0.6 lib/rails/mongoid.rb
mongoid-7.5.4 lib/rails/mongoid.rb
mongoid-8.1.1 lib/rails/mongoid.rb
mongoid-8.0.5 lib/rails/mongoid.rb
mongoid-8.1.0 lib/rails/mongoid.rb
mongoid-7.5.3 lib/rails/mongoid.rb
mongoid-8.0.4 lib/rails/mongoid.rb
mongoid-8.0.3 lib/rails/mongoid.rb
mongoid-7.5.2 lib/rails/mongoid.rb
mongoid-8.0.2 lib/rails/mongoid.rb
mongoid-7.5.1 lib/rails/mongoid.rb