Module: Rails::Mongoid

Extended by:
Mongoid
Included in:
Mongoid
Defined in:
lib/rails/mongoid.rb,
lib/mongoid/railtie.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Railtie

Instance Method Summary collapse

Instance Method Details

#create_indexes(pattern) ⇒ Array<String>

Create indexes for each model given the provided pattern and the class is not embedded.

Examples:

Create all the indexes.

Rails::Mongoid.create_indexes("app/models/**/*.rb")

Parameters:

  • pattern (String)

    The file matching pattern.

Returns:

Since:

  • 2.1.0



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails/mongoid.rb', line 17

def create_indexes(pattern)
  logger = Logger.new($stdout)
  Dir.glob(pattern).each do |file|
    logger = Logger.new($stdout)
    begin
      model = determine_model(file)
      if model
        model.create_indexes
        logger.info("Generated indexes for #{model}")
      else
        logger.info("Not a Mongoid parent model: #{file}")
      end
    rescue => e
      logger.error %Q{Failed to create indexes for #{model}:
        #{e.class}:#{e.message}
        #{e.backtrace.join("\n")}
      }
    end
  end
end

#load_models(app) ⇒ Object

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.

Examples:

Load all the application models.

Rails::Mongoid.load_models(app)

Parameters:

  • app (Application)

    The rails application.



46
47
48
49
50
51
52
# File 'lib/rails/mongoid.rb', line 46

def load_models(app)
  app.config.paths["app/models"].each do |path|
    Dir.glob("#{path}/**/*.rb").sort.each do |file|
      load_model(file.gsub("#{path}/" , "").gsub(".rb", ""))
    end
  end
end

#preload_models(app) ⇒ Object

Conditionally calls ‘Rails::Mongoid.load_models(app)` if the `::Mongoid.preload_models` is `true`.

Parameters:

  • app (Application)

    The rails application.



58
59
60
# File 'lib/rails/mongoid.rb', line 58

def preload_models(app)
  load_models(app) if ::Mongoid.preload_models
end