Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 124 lines (100 sloc) 3.838 kB
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
1 require 'active_support/core_ext/module/delegation'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
2 require 'rails/railtie'
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
3
4 module Rails
5 class Engine < Railtie
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
6 autoload :Configurable, "rails/engine/configurable"
7 autoload :Configuration, "rails/engine/configuration"
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
8
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
9 class << self
10 attr_accessor :called_from
11
e548f96 @josevalim Rename plugin_name to railtie_name and engine_name.
josevalim authored
12 alias :engine_name :railtie_name
13 alias :engine_names :railtie_names
14
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
15 def inherited(base)
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
16 unless abstract_railtie?(base)
17 base.called_from = begin
18 call_stack = caller.map { |p| p.split(':').first }
19 File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] })
20 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
21 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
22
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
23 super
24 end
25
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
26 def find_root_with_flag(flag, default=nil)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
27 root_path = self.called_from
28
29 while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
30 parent = File.dirname(root_path)
31 root_path = parent != root_path && parent
32 end
33
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
34 root = File.exist?("#{root_path}/#{flag}") ? root_path : default
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
35 raise "Could not find root path for #{self}" unless root
36
37 RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
13d66cd @josevalim Extract Railtie load from application.
josevalim authored
38 Pathname.new(root).expand_path : Pathname.new(root).realpath
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
39 end
40 end
41
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
42 delegate :middleware, :paths, :root, :to => :config
43
44 def load_tasks
45 super
46 config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
47 end
48
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
49 # Add configured load paths to ruby load paths and remove duplicates.
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
50 initializer :set_load_path do
51 config.load_paths.reverse_each do |path|
4ae7936 @josevalim Got tests working once again.
josevalim authored
52 $LOAD_PATH.unshift(path) if File.directory?(path)
53 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
54 $LOAD_PATH.uniq!
55 end
56
57 # Set the paths from which Rails will automatically load source files,
58 # and the load_once paths.
924fa08 @josevalim First steps into making Plugin < Engine.
josevalim authored
59 initializer :set_autoload_paths do |app|
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
60 ActiveSupport::Dependencies.load_paths.concat(config.load_paths)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
61
924fa08 @josevalim First steps into making Plugin < Engine.
josevalim authored
62 if reloadable?(app)
63 ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths)
64 else
65 ActiveSupport::Dependencies.load_once_paths.concat(config.load_paths)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
66 end
67
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
68 # Freeze so future modifications will fail rather than do nothing mysteriously
924fa08 @josevalim First steps into making Plugin < Engine.
josevalim authored
69 config.load_paths.freeze
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
70 config.load_once_paths.freeze
71 end
72
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
73 initializer :add_routing_paths do
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
74 config.paths.config.routes.to_a.each do |route|
2fde9d7 @josevalim Solve some pendencies.
josevalim authored
75 config.action_dispatch.route_paths.unshift(route) if File.exists?(route)
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
76 end
77 end
78
e0bdc4f @josevalim Ensure namespaced controllers in engines work.
josevalim authored
79 initializer :add_routing_namespaces do |app|
80 config.paths.app.controllers.to_a.each do |load_path|
81 load_path = File.expand_path(load_path)
82 Dir["#{load_path}/*/*_controller.rb"].collect do |path|
83 namespace = File.dirname(path).sub(/#{load_path}\/?/, '')
84 app.routes.controller_namespaces << namespace unless namespace.empty?
85 end
86 end
87 end
88
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
89 initializer :add_locales do
4eab3aa @josevalim Ensure user set load paths have higher preference and move Bootstrap …
josevalim authored
90 config.i18n.load_path.unshift(*config.paths.config.locales.to_a)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
91 end
92
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
93 initializer :add_view_paths do
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
94 views = config.paths.app.views.to_a
95 ActionController::Base.view_paths.concat(views) if defined?(ActionController)
96 ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer)
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
97 end
98
99 initializer :load_application_initializers do
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
100 config.paths.config.initializers.each do |initializer|
101 load(initializer)
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
102 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
103 end
104
105 initializer :load_application_classes do |app|
106 next if $rails_rake_task
107
108 if app.config.cache_classes
109 config.eager_load_paths.each do |load_path|
110 matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
111 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
112 require_dependency file.sub(matcher, '\1')
113 end
114 end
115 end
116 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
117
118 protected
119
120 def reloadable?(app)
121 app.config.reload_plugins
122 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
123 end
124 end
Something went wrong with that request. Please try again.