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

#create_indexes(*globs) ⇒ Array<Class>

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

Examples:

Create all the indexes.

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

Parameters:

  • globs (Array<String>)

    The file matching globs.

Returns:

  • (Array<Class>)

    The indexed models.

Since:

  • 2.1.0



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

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) ⇒ 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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rails/mongoid.rb', line 106

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) ⇒ Array<Class>

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

Examples:

Return all models. Return all models under app/models/

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

Parameters:

  • glob (String)

    The file matching glob.

Returns:

  • (Array<Class>)

    The models.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rails/mongoid.rb', line 66

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(%Q{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) ⇒ Object

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

Parameters:

  • app (Application)

    The rails application.



125
126
127
# File 'lib/rails/mongoid.rb', line 125

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

#remove_indexes(*globs) ⇒ Array<Class>

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

Examples:

Remove all the indexes.

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

Parameters:

  • globs (Array<String>)

    The file matching globs.

Returns:

  • (Array<Class>)

    The un-indexed models.



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

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