Module: Rails::Mongoid
Defined Under Namespace
Classes: Railtie
Instance Method Summary collapse
-
#create_indexes(*globs) ⇒ Array<Class>
Create indexes for each model given the provided globs and the class is not embedded.
-
#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.
-
#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.
-
#preload_models(app) ⇒ Object
Conditionally calls ‘Rails::Mongoid.load_models(app)` if the `::Mongoid.preload_models` is `true`.
-
#remove_indexes(*globs) ⇒ Array<Class>
Remove indexes for each model given the provided globs and the class is not embedded.
Instance Method Details
#create_indexes(*globs) ⇒ Array<Class>
Create indexes for each model given the provided globs and the class is not embedded.
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..empty? unless model. model.create_indexes logger.info("MONGOID: Created indexes on #{model}:") model..each_pair do |index, | logger.info("MONGOID: Index: #{index}, 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.
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.
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"].} 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.} #{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`.
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.
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. 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 |