Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 234 lines (194 sloc) 6.972 kb
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
1 require 'fileutils'
dc677f7 tests pass with requiring the frameworks in rails.rb
Carlhuda authored
2
accd9b4 @josh Restore "Start Rails::Application object
josh authored
3 module Rails
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
4 class Application < Engine
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
5 # TODO Clear up 2 way delegation flow between App class and instance.
6 # Infact just add a method_missing on the class.
7 #
8 # TODO I'd like to track the "default app" different using an inherited hook.
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
9 #
10 # TODO Check helpers works as expected
11 #
12 # TODO Check routes namespaces
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
13 class << self
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
14 alias :configure :class_eval
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
15 delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
d859402 @josh Use Rails.initialize! where we just want to run the initializers and …
josh authored
16
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
17 private :new
18 def instance
19 @instance ||= new
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
20 end
21
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
22 def config
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
23 @config ||= Configuration.new(original_root)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
24 end
25
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
26 def original_root
27 @original_root ||= find_root_with_file_flag("config.ru", Dir.pwd)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
28 end
29
30 def inherited(base)
31 super
32 Railtie.plugins.delete(base)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
33 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
34
426348b @josh Update routes.rb template to use App name
josh authored
35 def routes
36 ActionController::Routing::Routes
37 end
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
38 end
6f6a589 Create the application object from config/environment.rb
Yehuda Katz + Carl Lerche authored
39
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
40 delegate :routes, :to => :'self.class'
5f8e48c @josh Move route reloading into railties
josh authored
41 attr_reader :route_configuration_files
42
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
43 def initialize
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
44 require_environment
530b8ff Have script/* and Rakefile use the application object
Carlhuda authored
45 Rails.application ||= self
5f8e48c @josh Move route reloading into railties
josh authored
46 @route_configuration_files = []
d982fe2 Replace reopening the class with App.configure as an alias to class_eval
Carlhuda authored
47 end
48
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
49 def initialize!
50 run_initializers(self)
51 self
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
52 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
53
5f8e48c @josh Move route reloading into railties
josh authored
54 def routes_changed_at
55 routes_changed_at = nil
56
57 route_configuration_files.each do |config|
58 config_changed_at = File.stat(config).mtime
59
60 if routes_changed_at.nil? || config_changed_at > routes_changed_at
61 routes_changed_at = config_changed_at
62 end
63 end
64
65 routes_changed_at
66 end
67
68 def reload_routes!
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
69 routes = Rails::Application.routes
5f8e48c @josh Move route reloading into railties
josh authored
70 routes.disable_clear_and_finalize = true
71
72 routes.clear!
73 route_configuration_files.each { |config| load(config) }
74 routes.finalize!
75
76 nil
77 ensure
78 routes.disable_clear_and_finalize = false
79 end
80
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
81 def require_environment
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
82 environment = config.paths.config.environment.to_a.first
83 require environment if environment
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
84 end
85
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
86 def load_tasks
87 require "rails/tasks"
468bdb3 @spohlenz Make plugins responsible for loading their own rake tasks
spohlenz authored
88 plugins.each { |p| p.load_tasks }
c02f278 Add the ability to have plugins load rake tasks.
Carl Lerche authored
89 # Load all application tasks
90 # TODO: extract out the path to the rake tasks
94bb331 @jeremy Shift more responsibility from application class to its singleton ins…
jeremy authored
91 Dir["#{root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
92 task :environment do
93 $rails_rake_task = true
94 initialize!
95 end
96 end
97
4ca9765 @josevalim Allow railties to specify generators paths.
josevalim authored
98 def load_generators
99 plugins.each { |p| p.load_generators }
100 end
101
d1ffc54 @spastorino fixes the failure of config.plugins without :all option tests [#3718 …
spastorino authored
102 # TODO: Fix this method. It loads all railties independent if :all is given
103 # or not, otherwise frameworks are never loaded.
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
104 def plugins
105 @plugins ||= begin
bbe80ae @josh config.plugins should still work with an array of strings
josh authored
106 plugin_names = (config.plugins || [:all]).map { |p| p.to_sym }
d1ffc54 @spastorino fixes the failure of config.plugins without :all option tests [#3718 …
spastorino authored
107 Railtie.plugins.map(&:new) + Plugin.all(plugin_names, config.paths.vendor.plugins)
82b9b15 Refactor plugins
Yehuda Katz + Carl Lerche authored
108 end
109 end
110
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored
111 def app
112 @app ||= begin
113 reload_routes!
114 middleware.build(routes)
115 end
116 end
117
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
118 def call(env)
fc4f237 @josevalim Make filter parameters based on request, so they can be modified for …
josevalim authored
119 env["action_dispatch.parameter_filter"] = config.filter_parameters
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored
120 app.call(env)
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
121 end
122
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
123 def initializers
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
124 initializers = Bootstrap.initializers
125 initializers += super
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
126 plugins.each { |p| initializers += p.initializers }
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
127 initializers += Finisher.initializers
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
128 initializers
129 end
130
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
131 module Bootstrap
132 include Initializable
133
134 initializer :load_all_active_support do |app|
135 require "active_support/all" unless app.config.active_support.bare
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
136 end
603d4fb @josh Build middleware stack and reload routes after app initializers run
josh authored
137
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
138 # Preload all frameworks specified by the Configuration#frameworks.
139 # Used by Passenger to ensure everything's loaded before forking and
140 # to avoid autoload race conditions in JRuby.
141 initializer :preload_frameworks do |app|
142 require 'active_support/dependencies'
143 ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks
144 end
145
146 # Initialize the logger early in the stack in case we need to log some deprecation.
147 initializer :initialize_logger do |app|
148 Rails.logger ||= app.config.logger || begin
149 path = app.config.paths.log.to_a.first
150 logger = ActiveSupport::BufferedLogger.new(path)
151 logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase)
152 logger.auto_flushing = false if Rails.env.production?
153 logger
154 rescue StandardError => e
155 logger = ActiveSupport::BufferedLogger.new(STDERR)
156 logger.level = ActiveSupport::BufferedLogger::WARN
157 logger.warn(
158 "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
159 "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
160 )
161 logger
162 end
163 end
164
165 # Initialize cache early in the stack so railties can make use of it.
166 initializer :initialize_cache do |app|
167 unless defined?(RAILS_CACHE)
168 silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) }
169
170 if RAILS_CACHE.respond_to?(:middleware)
171 app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
172 end
173 end
174 end
175
176 # Initialize rails subscriber on top of notifications.
177 initializer :initialize_subscriber do |app|
178 require 'active_support/notifications'
179
180 if app.config.colorize_logging == false
181 Rails::Subscriber.colorize_logging = false
182 app.config.generators.colorize_logging = false
183 end
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
184
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
185 ActiveSupport::Notifications.subscribe do |*args|
186 Rails::Subscriber.dispatch(args)
187 end
188 end
189
190 initializer :set_clear_dependencies_hook do |app|
191 unless app.config.cache_classes
192 ActionDispatch::Callbacks.after do
193 ActiveSupport::Dependencies.clear
194 end
195 end
196 end
197
198 # Sets the dependency loading mechanism.
199 # TODO: Remove files from the $" and always use require.
200 initializer :initialize_dependency_mechanism do |app|
201 ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load
c8cc8a9 @josevalim Moved more configuration away from bootstrap.
josevalim authored
202 end
203 end
204
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
205 module Finisher
206 include Initializable
207
208 initializer :add_builtin_route do |app|
209 if Rails.env.development?
210 app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
211 end
212 end
213
214 initializer :build_middleware_stack do |app|
215 app.app
216 end
217
218 # Fires the user-supplied after_initialize block (config#after_initialize)
219 initializer :after_initialize do |app|
220 app.config.after_initialize_blocks.each do |block|
221 block.call(app)
222 end
223 end
224
225 # Disable dependency loading during request cycle
226 initializer :disable_dependency_loading do |app|
227 if app.config.cache_classes && !app.config.dependency_loading
228 ActiveSupport::Dependencies.unhook!
229 end
992c2db Finish porting over the initializers to the app object and fix all th…
Carl Lerche authored
230 end
231 end
accd9b4 @josh Restore "Start Rails::Application object
josh authored
232 end
233 end
Something went wrong with that request. Please try again.