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_indexesArray<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

Returns:

  • (Array<Class>)

    The indexed models.

Since:

  • 2.1.0



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

def create_indexes
  ::Mongoid.models.each do |model|
    next if model.index_specifications.empty?
    unless model.embedded?
      model.create_indexes
      logger.info("MONGOID: Created indexes on #{model}:")
      model.index_specifications.each do |spec|
        logger.info("MONGOID: Index: #{spec.key}, Options: #{spec.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.



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

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

#preload_models(app) ⇒ Object

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

Parameters:

  • app (Application)

    The rails application.



127
128
129
# File 'lib/rails/mongoid.rb', line 127

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

#remove_indexesArray<Class>

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

Examples:

Remove all the indexes.

Rails::Mongoid.remove_indexes

Returns:

  • (Array<Class>)

    The un-indexed models.



89
90
91
92
93
94
95
96
97
98
# File 'lib/rails/mongoid.rb', line 89

def remove_indexes
  ::Mongoid.models.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

#remove_undefined_indexesHash{Class => Array(Hash)}

Remove indexes that exist in the database but aren’t specified on the models.

Examples:

Remove undefined indexes.

Rails::Mongoid.remove_undefined_indexes

Returns:

  • (Hash{Class => Array(Hash)})

    The list of indexes that were removed by model.

Since:

  • 4.0.0



71
72
73
74
75
76
77
78
79
# File 'lib/rails/mongoid.rb', line 71

def remove_undefined_indexes
  undefined_indexes.each do |model, indexes|
    indexes.each do |index|
      key = index['key'].symbolize_keys
      model.collection.indexes.drop(key)
      logger.info("MONGOID: Removing index: #{index['name']} on #{model}.")
    end
  end
end

#undefined_indexesObject

Return the list of indexes by model that exist in the database but aren’t specified on the models.

Examples:

Return the list of unused indexes.

Rails::Mongoid.undefined_indexes


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails/mongoid.rb', line 39

def undefined_indexes
  undefined_by_model = {}

  ::Mongoid.models.each do |model|
    unless model.embedded?
      model.collection.indexes.each do |index|
        # ignore default index
        unless index['name'] == '_id_'
          key = index['key'].symbolize_keys
          spec = model.index_specification(key)
          unless spec
            # index not specified
            undefined_by_model[model] ||= []
            undefined_by_model[model] << index
          end
        end
      end
    end
  end

  undefined_by_model
end