Create indexes for each model given the provided pattern and the class is not embedded.
@example Create all the indexes.
Rails::Mongoid.create_indexes("app/models/**/*.rb")
@param [ String ] pattern The file matching pattern.
@return [ Array<String> ] The file names.
@since 2.1.0
# File lib/rails/mongoid.rb, line 16 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("MONGOID: Created indexes on #{model}:") model.index_options.each_pair do |index, options| logger.info("MONGOID: Index: #{index}, Options: #{options}") end else logger.info("MONGOID: Index ignored on: #{model}, please define in the root model.") end end end
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 85 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
Return all models matching the pattern.
@example Return all models.
Rails::Mongoid.models("app/models/**/*.rb")
@param [ String ] pattern The file matching pattern.
@return [ Array<Class> ] The models.
# File lib/rails/mongoid.rb, line 62 def models(pattern) Dir.glob(pattern).map do |file| logger = Logger.new($stdout) begin determine_model(file, logger) rescue => e logger.error(%{MONGOID: Failed to determine model from #{file}: #{e.class}:#{e.message} #{e.backtrace.join("\n")} }) nil end end.flatten.compact end
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 104 def preload_models(app) load_models(app) if ::Mongoid.preload_models end
Remove indexes for each model given the provided pattern and the class is not embedded.
@example Remove all the indexes.
Rails::Mongoid.create_indexes("app/models/**/*.rb")
@param [ String ] pattern The file matching pattern.
@return [ Array<String> ] The file names.
# File lib/rails/mongoid.rb, line 42 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("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.") end end
Generated with the Darkfish Rdoc Generator 2.