Module: Rails::Mongoid
Defined Under Namespace
Classes: Railtie
Instance Method Summary (collapse)
-
- (Array<String>) create_indexes(pattern)
Create indexes for each model given the provided pattern and the class is not embedded.
-
- (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.
-
- (Array<Class>) models(pattern)
Return all models matching the pattern.
-
- (Object) preload_models(app)
Conditionally calls `Rails::Mongoid.load_models(app)` if the `::Mongoid.preload_models` is `true`.
-
- (Array<String>) remove_indexes(pattern)
Remove indexes for each model given the provided pattern and the class is not embedded.
Instance Method Details
- (Array<String>) create_indexes(pattern)
Create indexes for each model given the provided pattern and the class is not embedded.
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..empty? unless model. model.create_indexes logger.info("Creating indexes on: #{model} for: #{model..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.
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.
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.} #{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`.
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.
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. 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 |