Skip to content
This repository
Newer
Older
100644 220 lines (196 sloc) 7.86 kb
b17e358e » josevalim
2010-01-23 Move configuration to subfolders.
1 require 'rails/railtie'
812136a0 » jeremy
2010-03-01 Fix unstated usage of Pathname
2 require 'active_support/core_ext/module/delegation'
3 require 'pathname'
53c13f1a » anildigital
2010-04-26 Use Config::CONFIG['host_os'] instead of RUBY_PLATFORM [#4477 state:r…
4 require 'rbconfig'
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
5
6 module Rails
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
7 # Rails::Engine allows you to wrap a specific Rails application and share it accross
de0753d2 » mikel
2010-02-08 Editing the railties/../railtie.rb and engine.rb docs
8 # different applications. Since Rails 3.0, every Rails::Application is nothing
9 # more than an Engine, allowing you to share it very easily.
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
10 #
11 # Any Rails::Engine is also a Rails::Railtie, so the same methods (like rake_tasks and
12 # generators) and configuration available in the latter can also be used in the former.
13 #
14 # == Creating an Engine
15 #
16 # In Rails versions before to 3.0, your gems automatically behaved as Engine, however
17 # this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically
18 # behave as Engine, you have to specify an Engine for it somewhere inside your plugin
19 # lib folder (similar with how we spceify a Railtie):
20 #
21 # # lib/my_engine.rb
22 # module MyEngine
23 # class Engine < Rails::Engine
24 # end
25 # end
26 #
27 # Then ensure that this file is loaded at the top of your config/application.rb (or in
45e60283 » wycats
2010-05-29 Removing Metal from Rails 3.
28 # your Gemfile) and it will automatically load models, controllers and helpers
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
29 # inside app, load routes at "config/routes.rb", load locales at "config/locales/*",
30 # load tasks at "lib/tasks/*".
31 #
32 # == Configuration
33 #
34 # Besides the Railtie configuration which is shared across the application, in a
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
35 # Rails::Engine you can access autoload_paths, eager_load_paths and autoload_once_paths,
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
36 # which differently from a Railtie, are scoped to the current Engine.
37 #
38 # Example:
39 #
40 # class MyEngine < Rails::Engine
41 # # Add a load path for this specific Engine
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
42 # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
4bacc2a6 » josevalim
2010-04-06 Update the documentation for Engine and Railtie.
43 #
44 # initializer "my_engine.add_middleware" do |app|
20f0e9f3 » jeremy
2010-04-29 Fix docs typo: app.middlewares -> app.middleware
45 # app.middleware.use MyEngine::Middleware
4bacc2a6 » josevalim
2010-04-06 Update the documentation for Engine and Railtie.
46 # end
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
47 # end
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
48 #
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
49 # == Paths
50 #
de0753d2 » mikel
2010-02-08 Editing the railties/../railtie.rb and engine.rb docs
51 # Since Rails 3.0, both your Application and Engines do not have hardcoded paths.
781d0a9b » josevalim
2010-02-02 Add docs for Railtie, Engine, Plugin and Application.
52 # This means that you are not required to place your controllers at "app/controllers",
53 # but in any place which you find convenient.
54 #
55 # For example, let's suppose you want to lay your controllers at lib/controllers, all
56 # you need to do is:
57 #
58 # class MyEngine < Rails::Engine
59 # paths.app.controllers = "lib/controllers"
60 # end
61 #
62 # You can also have your controllers being loaded from both "app/controllers" and
63 # "lib/controllers":
64 #
65 # class MyEngine < Rails::Engine
66 # paths.app.controllers << "lib/controllers"
67 # end
68 #
69 # The available paths in an Engine are:
70 #
71 # class MyEngine < Rails::Engine
72 # paths.app = "app"
73 # paths.app.controllers = "app/controllers"
74 # paths.app.helpers = "app/helpers"
75 # paths.app.models = "app/models"
76 # paths.app.views = "app/views"
77 # paths.lib = "lib"
78 # paths.lib.tasks = "lib/tasks"
79 # paths.config = "config"
80 # paths.config.initializers = "config/initializers"
81 # paths.config.locales = "config/locales"
82 # paths.config.routes = "config/routes.rb"
83 # end
84 #
85 # Your Application class adds a couple more paths to this set. And as in your Application,
86 # all folders under "app" are automatically added to the load path. So if you have
87 # "app/observers", it's added by default.
88 #
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
89 class Engine < Railtie
b17e358e » josevalim
2010-01-23 Move configuration to subfolders.
90 autoload :Configurable, "rails/engine/configurable"
91 autoload :Configuration, "rails/engine/configuration"
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
92
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
93 class << self
94 attr_accessor :called_from
95
395d6648 » josevalim
2010-03-26 Move application configuration to the application configuration objec…
96 # TODO Remove this. It's deprecated.
97 alias :engine_name :railtie_name
e548f96b » josevalim
2010-01-24 Rename plugin_name to railtie_name and engine_name.
98
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
99 def inherited(base)
f5ee855f » josevalim
2010-03-26 Improve heuristic for railties default name, otherwise railties may b…
100 unless base.abstract_railtie?
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
101 base.called_from = begin
a5684dfa » josevalim
2010-02-18 Ensure config.after_initializer is executed before building the middl…
102 # Remove the line number from backtraces making sure we don't leave anything behind
1477a610 » aaronchi
2010-02-18 Fix called_from under Windows so engines works properly
103 call_stack = caller.map { |p| p.split(':')[0..-2].join(':') }
b1b922d6 » jeremy
2010-02-27 Revert "Revert "Application detection should also allow dots in the p…
104 File.dirname(call_stack.detect { |p| p !~ %r[railties[\w\-\.]*/lib/rails|rack[\w\-\.]*/lib/rack] })
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
105 end
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
106 end
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
107
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
108 super
109 end
110
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
111 def find_root_with_flag(flag, default=nil)
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
112 root_path = self.called_from
113
114 while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
115 parent = File.dirname(root_path)
116 root_path = parent != root_path && parent
117 end
118
02c5137e » josevalim
2010-01-22 Add view paths to Engine setup.
119 root = File.exist?("#{root_path}/#{flag}") ? root_path : default
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
120 raise "Could not find root path for #{self}" unless root
121
c8b84a1c » spastorino
2010-08-23 Config is deprecated on 1.8.8 and 1.9.3 use RbConfig
122 RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ?
13d66cdf » josevalim
2010-01-23 Extract Railtie load from application.
123 Pathname.new(root).expand_path : Pathname.new(root).realpath
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
124 end
125 end
126
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
127 delegate :paths, :root, :to => :config
2b75b94a » josevalim
2010-01-23 Plugin is now an Engine.
128
129 def load_tasks
7317d9ef » josh
2010-02-28 Remove implicit controller namespacing from new dsl
130 super
2b75b94a » josevalim
2010-01-23 Plugin is now an Engine.
131 config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
132 end
133
351816fa » josevalim
2010-05-15 Ensure that eager_load actually takes place just after the middleware…
134 def eager_load!
135 config.eager_load_paths.each do |load_path|
136 matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
137 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
138 require_dependency file.sub(matcher, '\1')
139 end
140 end
141 end
142
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
143 # Add configured load paths to ruby load paths and remove duplicates.
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
144 initializer :set_load_path, :before => :bootstrap_hook do
9b19a6f1 » josevalim
2010-06-28 A few changes were done in this commit:
145 _all_load_paths.reverse_each do |path|
4ae79367 » josevalim
2010-01-22 Got tests working once again.
146 $LOAD_PATH.unshift(path) if File.directory?(path)
147 end
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
148 $LOAD_PATH.uniq!
149 end
150
151 # Set the paths from which Rails will automatically load source files,
152 # and the load_once paths.
9cfeefb6 » wycats
2010-05-15 Reorganized initializers a bit to enable better hooks for common case…
153 #
154 # This needs to be an initializer, since it needs to run once
155 # per engine and get the engine as a block parameter
156 initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
9b19a6f1 » josevalim
2010-06-28 A few changes were done in this commit:
157 ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
158 ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_once_paths)
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
159
98240c49 » josevalim
2010-01-23 Get rid of initializers global and create i18n railtie.
160 # Freeze so future modifications will fail rather than do nothing mysteriously
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
161 config.autoload_paths.freeze
9b19a6f1 » josevalim
2010-06-28 A few changes were done in this commit:
162 config.eager_load_paths.freeze
6f83a503 » fxn
2010-06-22 renames load_(once_)paths to autoload_(once_)paths in dependencies an…
163 config.autoload_once_paths.freeze
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
164 end
165
226d8e74 » josevalim
2010-01-27 Refactor MetalLoader and RoutesReloader to rely less on class configu…
166 initializer :add_routing_paths do |app|
84ebfa45 » josevalim
2010-01-24 Ensure metals and initializers in plugins are loaded.
167 paths.config.routes.to_a.each do |route|
226d8e74 » josevalim
2010-01-27 Refactor MetalLoader and RoutesReloader to rely less on class configu…
168 app.routes_reloader.paths.unshift(route) if File.exists?(route)
98240c49 » josevalim
2010-01-23 Get rid of initializers global and create i18n railtie.
169 end
170 end
171
7317d9ef » josh
2010-02-28 Remove implicit controller namespacing from new dsl
172 # DEPRECATED: Remove in 3.1
e0bdc4f4 » josevalim
2010-01-24 Ensure namespaced controllers in engines work.
173 initializer :add_routing_namespaces do |app|
84ebfa45 » josevalim
2010-01-24 Ensure metals and initializers in plugins are loaded.
174 paths.app.controllers.to_a.each do |load_path|
e0bdc4f4 » josevalim
2010-01-24 Ensure namespaced controllers in engines work.
175 load_path = File.expand_path(load_path)
028e54c6 » josevalim
2010-03-06 Add mailers to paths in case someone wants to access it directly and …
176 Dir["#{load_path}/*/**/*_controller.rb"].collect do |path|
d03196c8 » phs
2010-04-19 Regexp.escape(load_path) in add_routing_namespaces initializer [#4442
177 namespace = File.dirname(path).sub(/#{Regexp.escape(load_path)}\/?/, '')
e0bdc4f4 » josevalim
2010-01-24 Ensure namespaced controllers in engines work.
178 app.routes.controller_namespaces << namespace unless namespace.empty?
179 end
180 end
181 end
182
1177a40e » josevalim
2010-01-25 Fix i18n locales order test.
183 # I18n load paths are a special case since the ones added
184 # later have higher priority.
98240c49 » josevalim
2010-01-23 Get rid of initializers global and create i18n railtie.
185 initializer :add_locales do
fed72b58 » josevalim
2010-02-20 Rename engines_load_path to railties_load_path.
186 config.i18n.railties_load_path.concat(paths.config.locales.to_a)
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
187 end
188
02c5137e » josevalim
2010-01-22 Add view paths to Engine setup.
189 initializer :add_view_paths do
84ebfa45 » josevalim
2010-01-24 Ensure metals and initializers in plugins are loaded.
190 views = paths.app.views.to_a
4aded43b » wycats
2010-03-29 Replace the placeholder base_hook API with on_load. To specify some c…
191 ActiveSupport.on_load(:action_controller) do
192 prepend_view_path(views)
193 end
194 ActiveSupport.on_load(:action_mailer) do
195 prepend_view_path(views)
196 end
02c5137e » josevalim
2010-01-22 Add view paths to Engine setup.
197 end
198
6690d662 » josevalim
2010-04-05 Rename config.cookie_secret to config.secret_token and pass it as con…
199 initializer :load_config_initializers do
84ebfa45 » josevalim
2010-01-24 Ensure metals and initializers in plugins are loaded.
200 paths.config.initializers.to_a.sort.each do |initializer|
98240c49 » josevalim
2010-01-23 Get rid of initializers global and create i18n railtie.
201 load(initializer)
02c5137e » josevalim
2010-01-22 Add view paths to Engine setup.
202 end
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
203 end
204
351816fa » josevalim
2010-05-15 Ensure that eager_load actually takes place just after the middleware…
205 initializer :engines_blank_point do
206 # We need this initializer so all extra initializers added in engines are
207 # consistently executed after all the initializers above across all engines.
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
208 end
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
209
210 protected
211
9b19a6f1 » josevalim
2010-06-28 A few changes were done in this commit:
212 def _all_autoload_paths
213 @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
214 end
215
216 def _all_load_paths
217 @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
788fce25 » josevalim
2010-01-23 Create configurable modules and ensure that they are added only on di…
218 end
7fcf8590 » josevalim
2010-01-21 Massive cleanup in Railties and load stack.
219 end
6324eeef » jeremy
2010-02-26 plugin rails/init.rb deprecation message
220 end
Something went wrong with that request. Please try again.