Module: Rails::Mongoid

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

Defined Under Namespace

Classes: Railtie

Instance Method Summary (collapse)

Instance Method Details

- (Array<String>) create_indexes(pattern)

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:

  • (Array<String>)

    The file names.

Since:

  • 2.1.0



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails/mongoid.rb', line 17

def create_indexes(pattern)
  logger = Logger.new($stdout)
  models(pattern).each do |model|
    next if model.index_options.empty?
    unless model.embedded?
      model.create_indexes
      logger.info("Creating indexes on: #{model} for: #{model.index_options.keys.join(", ")}.")
    else
      logger.info("Index ignored on: #{model}, please define in the root model.")
    end
  end
end

- (Object) load_models(app)

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rails/mongoid.rb', line 83

def load_models(app)
  app.config.paths["app/models"].each do |path|
    preload = ::Mongoid.preload_models
    if preload.resizable?
      files = preload.map { |model| "#{path}/#{model}.rb" }
    else
      files = Dir.glob("#{path}/**/*.rb")
    end

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

- (Array<Class>) models(pattern)

Return all models matching the pattern.

Examples:

Return all models.

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

Parameters:

  • pattern (String)

    The file matching pattern.

Returns:

  • (Array<Class>)

    The models.



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

def models(pattern)
  Dir.glob(pattern).map do |file|
    logger = Logger.new($stdout)
    begin
      determine_model(file, logger)
    rescue => e
      logger.error(%Q{Failed to determine model from #{file}:
        #{e.class}:#{e.message}
        #{e.backtrace.join("\n")}
      })
      nil
    end
  end.flatten.compact
end

- (Object) preload_models(app)

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

Parameters:

  • app (Application)

    The rails application.



102
103
104
# File 'lib/rails/mongoid.rb', line 102

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

- (Array<String>) remove_indexes(pattern)

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

Examples:

Remove all the indexes.

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

Parameters:

  • pattern (String)

    The file matching pattern.

Returns:

  • (Array<String>)

    The file names.



40
41
42
43
44
45
46
47
48
49
# File 'lib/rails/mongoid.rb', line 40

def remove_indexes(pattern)
  logger = Logger.new($stdout)
  models(pattern).each do |model|
    next if model.embedded?
    indexes = model.collection.indexes.map{ |doc| doc["name"] }
    indexes.delete_one("_id_")
    model.remove_indexes
    logger.info("Removing indexes on: #{model} for: #{indexes.join(', ')}.")
  end
end