Skip to content
Newer
Older
100644 213 lines (189 sloc) 6.24 KB
b17e358 @josevalim Move configuration to subfolders.
josevalim authored Jan 23, 2010
1 require 'rails/initializable'
2 require 'rails/configuration'
8bb162f @jeremy Fix unstated usage of inflector
jeremy authored Mar 1, 2010
3 require 'active_support/inflector'
f182831 Use namespace if it's a mountable engine
Stefan Sprenger authored Jun 7, 2011
4 require 'active_support/core_ext/module/introspection'
83eec4c @arunagw Requiring delegate.
arunagw authored Aug 16, 2011
5 require 'active_support/core_ext/module/delegation'
b17e358 @josevalim Move configuration to subfolders.
josevalim authored Jan 23, 2010
6
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
7 module Rails
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 18, 2011
8 # Railtie is the core of the Rails framework and provides several hooks to extend
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
9 # Rails and/or modify the initialization process.
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
10 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
11 # Every major component of Rails (Action Mailer, Action Controller,
f1637bf @sikachu Remove Active Resource source files from the repository
sikachu authored May 15, 2011
12 # Action View and Active Record) is a Railtie. Each of
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
13 # them is responsible for their own initialization. This makes Rails itself
14 # absent of any component hooks, allowing other components to be used in
15 # place of any of the Rails defaults.
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
16 #
de0753d @mikel Editing the railties/../railtie.rb and engine.rb docs
mikel authored Feb 8, 2010
17 # Developing a Rails extension does _not_ require any implementation of
18 # Railtie, but if you need to interact with the Rails framework during
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
19 # or after boot, then Railtie is needed.
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
20 #
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
21 # For example, an extension doing any of the following would require Railtie:
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
22 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
23 # * creating initializers
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
24 # * configuring a Rails framework for the application, like setting a generator
72795d7 @mark-rushakoff Code-format references to config settings
mark-rushakoff authored Apr 27, 2012
25 # * +adding config.*+ keys to the environment
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
26 # * setting up a subscriber with ActiveSupport::Notifications
27 # * adding rake tasks
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
28 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
29 # == Creating your Railtie
30 #
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
31 # To extend Rails using Railtie, create a Railtie class which inherits
32 # from Rails::Railtie within your extension's namespace. This class must be
33 # loaded during the Rails boot process.
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
34 #
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
35 # The following example demonstrates an extension which can be used with or without Rails.
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
36 #
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
37 # # lib/my_gem/railtie.rb
38 # module MyGem
39 # class Railtie < Rails::Railtie
4443f41 @mikel Added more documentation on railtie.rb
mikel authored Jan 22, 2010
40 # end
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
41 # end
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
42 #
8af066b @jimmycuadra improve language and examples in Railtie docs
jimmycuadra authored Feb 19, 2011
43 # # lib/my_gem.rb
44 # require 'my_gem/railtie' if defined?(Rails)
4443f41 @mikel Added more documentation on railtie.rb
mikel authored Jan 22, 2010
45 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
46 # == Initializers
47 #
48 # To add an initialization step from your Railtie to Rails boot process, you just need
49 # to create an initializer block:
50 #
51 # class MyRailtie < Rails::Railtie
52 # initializer "my_railtie.configure_rails_initialization" do
53 # # some initialization behavior
54 # end
55 # end
56 #
7745f71 @weppos Fixed Railtie Rdoc examples not properly formatted [#4918 state:resol…
weppos authored Jun 21, 2010
57 # If specified, the block can also receive the application object, in case you
4bacc2a @josevalim Update the documentation for Engine and Railtie.
josevalim authored Apr 6, 2010
58 # need to access some application specific configuration, like middleware:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
59 #
60 # class MyRailtie < Rails::Railtie
61 # initializer "my_railtie.configure_rails_initialization" do |app|
9a9fb12 @jackdempsey Fix small middlewares typo
jackdempsey authored Jul 24, 2010
62 # app.middleware.use MyRailtie::Middleware
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
63 # end
64 # end
65 #
66 # Finally, you can also pass :before and :after as option to initializer, in case
67 # you want to couple it with a specific step in the initialization process.
68 #
69 # == Configuration
70 #
71 # Inside the Railtie class, you can access a config object which contains configuration
72 # shared by all railties and the application:
73 #
74 # class MyRailtie < Rails::Railtie
75 # # Customize the ORM
04cbabb @josevalim Deprecate generators in Railties. You should use app_generators instead.
josevalim authored Oct 2, 2010
76 # config.app_generators.orm :my_railtie_orm
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
77 #
78 # # Add a to_prepare block which is executed once in production
2a4eb69 fixing typo
Neeraj Singh authored Aug 17, 2010
79 # # and before each request in development
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
80 # config.to_prepare do
81 # MyRailtie.setup!
82 # end
83 # end
84 #
85 # == Loading rake tasks and generators
86 #
87 # If your railtie has rake tasks, you can tell Rails to load them through the method
04c31e2 @thoefer2 fixed typo for a methodname
thoefer2 authored Jun 11, 2011
88 # rake_tasks:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
89 #
4a51328 @zhengjia Minor fix on Rails:Railtie documentation
zhengjia authored Nov 16, 2010
90 # class MyRailtie < Rails::Railtie
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
91 # rake_tasks do
92 # load "path/to/my_railtie.tasks"
93 # end
94 # end
95 #
96 # By default, Rails load generators from your load path. However, if you want to place
97 # your generators at a different location, you can specify in your Railtie a block which
98 # will load them during normal generators lookup:
99 #
4a51328 @zhengjia Minor fix on Rails:Railtie documentation
zhengjia authored Nov 16, 2010
100 # class MyRailtie < Rails::Railtie
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
101 # generators do
102 # require "path/to/my_railtie_generator"
103 # end
104 # end
105 #
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored Jan 2, 2012
106 # == Application and Engine
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
107 #
108 # A Rails::Engine is nothing more than a Railtie with some initializers already set.
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored Jan 2, 2012
109 # And since Rails::Application is an engine, the same configuration described here
110 # can be used in both.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored Feb 2, 2010
111 #
112 # Be sure to look at the documentation of those specific classes for more information.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
113 #
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
114 class Railtie
b17e358 @josevalim Move configuration to subfolders.
josevalim authored Jan 23, 2010
115 autoload :Configurable, "rails/railtie/configurable"
116 autoload :Configuration, "rails/railtie/configuration"
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
117
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
118 include Initializable
119
dad7fdc @spastorino Rails::Plugin has gone
spastorino authored Jan 2, 2012
120 ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Engine Rails::Application)
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
121
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
122 class << self
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored Jul 19, 2010
123 private :new
124
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
125 def subclasses
126 @subclasses ||= []
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
127 end
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
128
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
129 def inherited(base)
f5ee855 @josevalim Improve heuristic for railties default name, otherwise railties may b…
josevalim authored Mar 26, 2010
130 unless base.abstract_railtie?
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored Jul 19, 2010
131 base.send(:include, Railtie::Configurable)
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
132 subclasses << base
133 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
134 end
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
135
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
136 def rake_tasks(&blk)
137 @rake_tasks ||= []
138 @rake_tasks << blk if blk
139 @rake_tasks
140 end
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
141
fa98eca @josevalim Add console hook to force ActiveRecord::Base to be loaded when consol…
josevalim authored Jul 17, 2010
142 def console(&blk)
143 @load_console ||= []
144 @load_console << blk if blk
145 @load_console
146 end
147
f8e7904 @kennyj Add support runner hook.
kennyj authored May 29, 2012
148 def runner(&blk)
149 @load_runner ||= []
150 @load_runner << blk if blk
151 @load_runner
152 end
153
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored Jan 21, 2010
154 def generators(&blk)
155 @generators ||= []
156 @generators << blk if blk
157 @generators
158 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored Jan 23, 2010
159
f5ee855 @josevalim Improve heuristic for railties default name, otherwise railties may b…
josevalim authored Mar 26, 2010
160 def abstract_railtie?
161 ABSTRACT_RAILTIES.include?(name)
8bb162f @jeremy Fix unstated usage of inflector
jeremy authored Mar 2, 2010
162 end
bfccbc6 @drogus Add Rails::Railtie.railtie_name method to allow setting custom name f…
drogus authored Jul 26, 2010
163
164 def railtie_name(name = nil)
559979b @drogus Always convert railtie_name to string
drogus authored Jul 26, 2010
165 @railtie_name = name.to_s if name
db8a864 @drogus Add table_name_prefix to Engine's namespace automatically
drogus authored Aug 3, 2010
166 @railtie_name ||= generate_railtie_name(self.name)
bfccbc6 @drogus Add Rails::Railtie.railtie_name method to allow setting custom name f…
drogus authored Jul 26, 2010
167 end
db8a864 @drogus Add table_name_prefix to Engine's namespace automatically
drogus authored Aug 3, 2010
168
169 protected
170 def generate_railtie_name(class_or_module)
2d8396f @jurriaan Updated/changed useless tr/gsubs
jurriaan authored Apr 3, 2012
171 ActiveSupport::Inflector.underscore(class_or_module).tr("/", "_")
db8a864 @drogus Add table_name_prefix to Engine's namespace automatically
drogus authored Aug 3, 2010
172 end
4ca9765 @josevalim Allow railties to specify generators paths.
josevalim authored Jan 19, 2010
173 end
174
bfccbc6 @drogus Add Rails::Railtie.railtie_name method to allow setting custom name f…
drogus authored Jul 26, 2010
175 delegate :railtie_name, :to => "self.class"
176
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored Jul 19, 2010
177 def config
178 @config ||= Railtie::Configuration.new
179 end
180
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
181 def railtie_namespace
182 @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
183 end
184
185 protected
186
187 def run_console_blocks(app) #:nodoc:
52e01fc @josevalim Everyone receives app as argument for consistency.
josevalim authored May 25, 2011
188 self.class.console.each { |block| block.call(app) }
4ca9765 @josevalim Allow railties to specify generators paths.
josevalim authored Jan 19, 2010
189 end
190
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
191 def run_generators_blocks(app) #:nodoc:
192 self.class.generators.each { |block| block.call(app) }
193 end
194
195 def run_runner_blocks(app) #:nodoc:
f8e7904 @kennyj Add support runner hook.
kennyj authored May 29, 2012
196 self.class.runner.each { |block| block.call(app) }
197 end
198
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
199 def run_tasks_blocks(app) #:nodoc:
c1a0c77 @jeremy Rake::DSL should always be available
jeremy authored May 31, 2012
200 extend Rake::DSL
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
201 self.class.rake_tasks.each { |block| instance_exec(app, &block) }
b417cfb @drogus Load rake tasks defined in superclasses in context of railties
drogus authored Oct 9, 2010
202
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
203 # Load also tasks from all superclasses
b417cfb @drogus Load rake tasks defined in superclasses in context of railties
drogus authored Oct 9, 2010
204 klass = self.class.superclass
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
205
b417cfb @drogus Load rake tasks defined in superclasses in context of railties
drogus authored Oct 9, 2010
206 while klass.respond_to?(:rake_tasks)
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored Jun 29, 2012
207 klass.rake_tasks.each { |t| instance_exec(app, &t) }
b417cfb @drogus Load rake tasks defined in superclasses in context of railties
drogus authored Oct 9, 2010
208 klass = klass.superclass
209 end
ae7ada1 Some railties cleanup:
Carl Lerche authored Dec 31, 2009
210 end
211 end
212 end
Something went wrong with that request. Please try again.