forked from mongodb/mongoid
/
railtie.rb
132 lines (119 loc) · 4.5 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# encoding: utf-8
require "singleton"
require "mongoid"
require "mongoid/config"
require "mongoid/railties/document"
require "rails"
require "rails/mongoid"
module Rails #:nodoc:
module Mongoid #:nodoc:
class Railtie < Rails::Railtie #:nodoc:
# Determine which generator to use. app_generators was introduced after
# 3.0.0.
#
# @example Get the generators method.
# railtie.generators
#
# @return [ Symbol ] The method name to use.
#
# @since 2.0.0.rc.4
def self.generator
config.respond_to?(:app_generators) ? :app_generators : :generators
end
config.send(generator).orm :mongoid, :migration => false
rake_tasks do
load "mongoid/railties/database.rake"
end
# Exposes Mongoid's configuration to the Rails application configuration.
#
# @example Set up configuration in the Rails app.
# module MyApplication
# class Application < Rails::Application
# config.mongoid.logger = Logger.new($stdout, :warn)
# config.mongoid.persist_in_safe_mode = true
# end
# end
config.mongoid = ::Mongoid::Config
# Initialize Mongoid. This will look for a mongoid.yml in the config
# directory and configure mongoid appropriately.
#
# @example mongoid.yml
#
# development:
# host: localhost
# database: mongoid
# slaves:
# # - host: localhost
# # port: 27018
# # - host: localhost
# # port: 27019
# allow_dynamic_fields: false
# parameterize_keys: false
# persist_in_safe_mode: false
#
initializer "setup database" do
config_file = Rails.root.join("config", "mongoid.yml")
if config_file.file?
::Mongoid.load!(config_file)
end
end
# After initialization we will warn the user if we can't find a mongoid.yml and
# alert to create one.
initializer "warn when configuration is missing" do
config.after_initialize do
unless Rails.root.join("config", "mongoid.yml").file?
puts "\nMongoid config not found. Create a config file at: config/mongoid.yml"
puts "to generate one run: rails generate mongoid:config\n\n"
end
end
end
# Set the proper error types for Rails. DocumentNotFound errors should be
# 404s and not 500s, validation errors are 422s.
initializer "load http errors" do |app|
config.after_initialize do
ActionDispatch::ShowExceptions.rescue_responses.update({
"Mongoid::Errors::DocumentNotFound" => :not_found,
"Mongoid::Errors::Validations" => 422
})
end
end
# Due to all models not getting loaded and messing up inheritance queries
# and indexing, we need to preload the models in order to address this.
#
# This will happen every request in development, once in ther other
# environments.
initializer "preload all application models" do |app|
config.to_prepare do
::Rails::Mongoid.load_models(app) unless $rails_rake_task
end
end
# This initializer warns the user that preloading models is set to false,
# and queries will be inconsistent in dev mode if models are using
# inheritance.
initializer "warn of preload models configuration" do |app|
config.after_initialize do
if !::Mongoid.preload_models && !Rails.configuration.cache_classes
puts "\nMongoid preload_models is set to false. If you are using"
puts "inheritance in your application model please set this to true or "
puts "you will experience querying inconsistencies in dev mode. Note that"
puts "this will severely decrease performance in dev mode only.\n\n"
end
end
end
# Need to include the Mongoid identity map middleware.
initializer "include the identity map" do |app|
app.config.middleware.use "Rack::Mongoid::Middleware::IdentityMap"
end
# Instantitate any registered observers after Rails initialization and
# instantiate them after being reloaded in the development environment
initializer "instantiate observers" do
config.after_initialize do
::Mongoid.instantiate_observers
ActionDispatch::Callbacks.to_prepare do
::Mongoid.instantiate_observers
end
end
end
end
end
end