Namespace

Class/Module Index [+]

Quicksearch

Rails::Mongoid

Public Instance Methods

create_indexes(*globs) click to toggle source

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

@example Create all the indexes.

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

@param [ Array<String> ] globs The file matching globs.

@return [ Array<Class> ] The indexed models.

@since 2.1.0

# File lib/rails/mongoid.rb, line 16
def create_indexes(*globs)
  models(*globs).each do |model|
    next if model.index_options.empty?
    unless model.embedded?
      model.create_indexes
      logger.info("MONGOID: Created indexes on #{model}:")
      model.index_options.each_pair do |index, options|
        logger.info("MONGOID: Index: #{index}, Options: #{options}")
      end
      model
    else
      logger.info("MONGOID: Index ignored on: #{model}, please define in the root model.")
      nil
    end
  end.compact
end
load_models(app) click to toggle source

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.

# File lib/rails/mongoid.rb, line 105
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
models(*globs) click to toggle source

Return all models matching the globs or, if no globs are specified, all possible models known from engines, the app, any gems, etc.

@example Return all models. Return all models under app/models/

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

@param [ String ] glob The file matching glob.

@return [ Array<Class> ] The models.

# File lib/rails/mongoid.rb, line 65
def models(*globs)
  all_possible_models = globs.empty?

  if globs.empty?
    engines_models_paths = Rails.application.railties.engines.map{|engine| engine.paths["app/models"].expanded}
    root_models_paths = Rails.application.paths["app/models"]
    models_paths = engines_models_paths.push(root_models_paths).flatten
    globs.replace(models_paths.map{|path| "#{path}/**/*.rb"})
  end

  models = []

  globs.flatten.compact.each do |glob|
    Dir.glob(glob).map do |file|
      begin
        model = determine_model(file, logger)
        models.push(model)
      rescue => e
        logger.error(%{MONGOID: Failed to determine model from #{file}:
          #{e.class}:#{e.message}
          #{e.backtrace.join("\n")}
        })
        nil
      end
    end
  end

  models = (::Mongoid.models | models) if all_possible_models

  models.compact.sort_by { |model| model.name || '' }
end
preload_models(app) click to toggle source

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

@param [ Application ] app The rails application.

# File lib/rails/mongoid.rb, line 124
def preload_models(app)
  load_models(app) if ::Mongoid.preload_models
end
remove_indexes(*globs) click to toggle source

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

@example Remove all the indexes.

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

@param [ Array<String> ] globs The file matching globs.

@return [ Array<Class> ] The un-indexed models.

# File lib/rails/mongoid.rb, line 43
def remove_indexes(*globs)
  models(*globs).each do |model|
    next if model.embedded?
    indexes = model.collection.indexes.map{ |doc| doc["name"] }
    indexes.delete_one("_id_")
    model.remove_indexes
    logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
    model
  end.compact
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.