S k i p t o C o n t e n t
S k i p t o S e a r c h
M e n u
C l o s e
i n d e x
R u b y o n R a i l s 7 . 1 . 3 . 4
C l a s s
R a i l s : : R a i l t i e
<
O b j e c t
● r a i l t i e s / l i b / r a i l s / r a i l t i e . r b
● r a i l t i e s / l i b / r a i l s / r a i l t i e / c o n f i g u r a b l e . r b
● r a i l t i e s / l i b / r a i l s / r a i l t i e / c o n f i g u r a t i o n . r b
v 7 . 1 . 3 . 4
R a i l s : : R a i l t i e
i s t h e c o r e o f t h e R a i l s f r a m e w o r k a n d p r o v i d e s s e v e r a l h o o k s t o e x t e n d R a i l s a n d / o r m o d i f y t h e i n i t i a l i z a t i o n p r o c e s s .
E v e r y m a j o r c o m p o n e n t o f R a i l s ( A c t i o n M a i l e r , A c t i o n C o n t r o l l e r , A c t i v e R e c o r d , e t c . ) i m p l e m e n t s a r a i l t i e . E a c h o f t h e m i s r e s p o n s i b l e f o r t h e i r o w n i n i t i a l i z a t i o n . T h i s m a k e s R a i l s i t s e l f a b s e n t o f a n y c o m p o n e n t h o o k s , a l l o w i n g o t h e r c o m p o n e n t s t o b e u s e d i n p l a c e o f a n y o f t h e R a i l s d e f a u l t s .
D e v e l o p i n g a R a i l s e x t e n s i o n d o e s n o t r e q u i r e i m p l e m e n t i n g a r a i l t i e , b u t i f y o u n e e d t o i n t e r a c t w i t h t h e R a i l s f r a m e w o r k d u r i n g o r a f t e r b o o t , t h e n a r a i l t i e i s n e e d e d .
F o r e x a m p l e , a n e x t e n s i o n d o i n g a n y o f t h e f o l l o w i n g w o u l d n e e d a r a i l t i e :
c r e a t i n g i n i t i a l i z e r s
c o n f i g u r i n g a R a i l s f r a m e w o r k f o r t h e a p p l i c a t i o n , l i k e s e t t i n g a g e n e r a t o r
a d d i n g c o n f i g . *
k e y s t o t h e e n v i r o n m e n t
s e t t i n g u p a s u b s c r i b e r w i t h A c
t i v e S u p p o r t : : N o t i f i c a t i o n s
a d d i n g R a k e t a s k s
T o e x t e n d R a i l s u s i n g a r a i l t i e , c r e a t e a s u b c l a s s o f R a i l s : : R a i
l t i e
. T h i s c l a s s m u s t b e l o a d e d d u r i n g t h e R a i l s b o o t p r o c e s s , a n d i s c o n v e n t i o n a l l y c a l l e d M y N a
m e s p a c e : : R a i l t i e
.
T h e f o l l o w i n g e x a m p l e d e m o n s t r a t e s a n e x t e n s i o n w h i c h c a n b e u s e d w i t h o r w i t h o u t R a i l s .
# lib/my_gem/railtie.rb
module MyGem
class Railtie < Rails::Railtie
end
end
# lib/my_gem.rb
require "my_gem/railtie" if defined?(Rails::Railtie)
I n i t i a l i z e r s
T o a d d a n i n i t i a l i z a t i o n s t e p t o t h e R a i l s b o o t p r o c e s s f r o m y o u r r a i l t i e , j u s t d e f i n e t h e i n i t i a l i z a t i o n c o d e w i t h t h e i n i t i a l i
z e r
m a c r o :
class MyRailtie < Rails::Railtie
initializer "my_railtie.configure_rails_initialization" do
# some initialization behavior
end
end
I f s p e c i f i e d , t h e b l o c k c a n a l s o r e c e i v e t h e a p p l i c a t i o n o b j e c t , i n c a s e y o u n e e d t o a c c e s s s o m e a p p l i c a t i o n - s p e c i f i c c o n f i g u r a t i o n , l i k e m i d d l e w a r e :
class MyRailtie < Rails::Railtie
initializer "my_railtie.configure_rails_initialization" do |app|
app.middleware.use MyRailtie::Middleware
end
end
F i n a l l y , y o u c a n a l s o p a s s : b e f o
r e
a n d : a f t e r
a s o p t i o n s t o i n i t
i a l i z e r
, i n c a s e y o u w a n t t o c o u p l e i t w i t h a s p e c i f i c s t e p i n t h e i n i t i a l i z a t i o n p r o c e s s .
R a i l t i e s c a n a c c e s s a c o n f i g o b j e c t w h i c h c o n t a i n s c o n f i g u r a t i o n s h a r e d b y a l l r a i l t i e s a n d t h e a p p l i c a t i o n :
class MyRailtie < Rails::Railtie
# Customize the ORM
config.app_generators.orm :my_railtie_orm
# Add a to_prepare block which is executed once in production
# and before each request in development.
config.to_prepare do
MyRailtie.setup!
end
end
L o a d i n g R a k e T a s k s a n d G e n e r a t o r
s
I f y o u r r a i l t i e h a s R a k e t a s k s , y o u c a n t e l l R a i l s t o l o a d t h e m t h r o u g h t h e m e t h o d r a k e _ t a s k s
:
class MyRailtie < Rails::Railtie
rake_tasks do
load "path/to/my_railtie.tasks"
end
end
B y d e f a u l t , R a i l s l o a d s g e n e r a t o r s f r o m y o u r l o a d p a t h . H o w e v e r , i f y o u w a n t t o p l a c e y o u r g e n e r a t o r s a t a d i f f e r e n t l o c a t i o n , y o u c a n s p e c i f y i n y o u r r a i l t i e a b l o c k w h i c h w i l l l o a d t h e m d u r i n g n o r m a l g e n e r a t o r s l o o k u p :
class MyRailtie < Rails::Railtie
generators do
require "path/to/my_railtie_generator"
end
end
S i n c e f i l e n a m e s o n t h e l o a d p a t h a r e s h a r e d a c r o s s g e m s , b e s u r e t h a t f i l e s y o u l o a d t h r o u g h a r a i l t i e h a v e u n i q u e n a m e s .
R u n a n o t h e r p r o g r a m w h e n t h e R a i l s s e r v e r s t a r t s
I n d e v e l o p m e n t , i t ’ s v e r y u s u a l t o h a v e t o r u n a n o t h e r p r o c e s s n e x t t o t h e R a i l s S e r v e r
. I n e x a m p l e y o u m i g h t w a n t t o s t a r t t h e W e b p a c k o r R e a c t s e r v e r . O r m a y b e y o u n e e d t o r u n y o u r j o b s c h e d u l e r p r o c e s s l i k e S i d e k i q . T h i s i s u s u a l l y d o n e b y o p e n i n g a n e w s h e l l a n d r u n n i n g t h e p r o g r a m f r o m h e r e .
R a i l s a l l o w y o u t o s p e c i f y a s e r
v e r
b l o c k w h i c h w i l l g e t c a l l e d w h e n a R a i l s s e r v e r s t a r t s . T h i s w a y , y o u r u s e r s d o n ’ t n e e d t o r e m e m b e r t o h a v e t o o p e n a n e w s h e l l a n d r u n a n o t h e r p r o g r a m , m a k i n g t h i s l e s s c o n f u s i n g f o r e v e r y o n e . I t c a n b e u s e d l i k e t h i s :
class MyRailtie < Rails::Railtie
server do
WebpackServer.start
end
end
A n e n g i n e i s n o t h i n g m o r e t h a n a r a i l t i e w i t h s o m e i n i t i a l i z e r s a l r e a d y s e t . A n d s i n c e R a i l s : : A p
p l i c a t i o n
i s a n e n g i n e , t h e s a m e c o n f i g u r a t i o n d e s c r i b e d h e r e c a n b e u s e d i n b o t h .
B e s u r e t o l o o k a t t h e d o c u m e n t a t i o n o f t h o s e s p e c i f i c c l a s s e s f o r m o r e i n f o r m a t i o n .
N a m e s p a c e
●
M O D U L E
R a i l s : : R a i l t i e : : C o n f i g u r a b l e
●
C L A S S
R a i l s : : R a i l t i e : : C o n f i g u r a t i o n
M e t h o d s
A
●
a b s t r a c t _ r a i l t i e ?
C
●
c o n f i g ,
●
c o n f i g u r e ,
●
c o n s o l e
G
●
g e n e r a t o r s
I
●
i n c r e m e n t _ l o a d _ i n d e x ,
●
i n h e r i t e d ,
●
i n s t a n c e
R
●
r a i l t i e _ n a m e ,
●
r a k e _ t a s k s ,
●
r u n n e r
S
●
s e r v e r ,
●
s u b c l a s s e s
I n c l u d e d M o d u l e s
●
R a i l s : : I n i t i a l i z a b l e
C o n s t a n t s
ABSTRACT_RAILTIES
=
%w(Rails::Railtie Rails::Engine Rails::Application)
A t t r i b u t e s
C l a s s P u b l i c m e t h o d s
a b s t r a c t _ r a i l t i e ? ( )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def abstract_railtie?
ABSTRACT_RAILTIES .include? (name )
end
A l l o w s y o u t o c o n f i g u r e t h e r a i l t i e . T h i s i s t h e s a m e m e t h o d s e e n i n R a i l t i e : : C o n f i g u r a b l e
, b u t t h i s m o d u l e i s n o l o n g e r r e q u i r e d f o r a l l s u b c l a s s e s o f R a i l t i e
s o w e p r o v i d e t h e c l a s s m e t h o d h e r e .
S o u r c e :
s h o w
|
o n G i t H u b
def configure (& block )
instance .configure (& block )
end
c o n s o l e ( & b l k )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def console (& blk )
register_block_for (:load_console , & blk )
end
g e n e r a t o r s ( & b l k )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def generators (& blk )
register_block_for (:generators , & blk )
end
i n h e r i t e d ( s u b c l a s s )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def inherited (subclass )
subclass .increment_load_index
super
end
i n s t a n c e ( )
L i n k
S i n c e R a i l s : : R a i l t i e
c a n n o t b e i n s t a n t i a t e d , a n y m e t h o d s t h a t c a l l i n s t a n c e
a r e i n t e n d e d t o b e c a l l e d o n l y o n s u b c l a s s e s o f a R a
i l t i e
.
S o u r c e :
s h o w
|
o n G i t H u b
def instance
@instance ||= new
end
r a i l t i e _ n a m e ( n a m e = n i l )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def railtie_name (name = nil )
@railtie_name = name .to_s if name
@railtie_name ||= generate_railtie_name (self .name )
end
r a k e _ t a s k s ( & b l k )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def rake_tasks (& blk )
register_block_for (:rake_tasks , & blk )
end
r u n n e r ( & b l k )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def runner (& blk )
register_block_for (:runner , & blk )
end
s e r v e r ( & b l k )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def server (& blk )
register_block_for (:server , & blk )
end
s u b c l a s s e s ( )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def subclasses
super .reject (& :abstract_railtie? ).sort
end
C l a s s P r o t e c t e d m e t h o d s
i n c r e m e n t _ l o a d _ i n d e x ( )
L i n k
S o u r c e :
s h o w
|
o n G i t H u b
def increment_load_index
@@load_counter ||= 0
@load_index = (@@load_counter += 1 )
end
I n s t a n c e P u b l i c m e t h o d s
c o n f i g ( )
L i n k
T h i s i s u s e d t o c r e a t e t h e c o n f i
g
o b j e c t o n R a i l t i e s , a n i n s t a n c e o f R a i l t i e : : C o n f i g u r a t i o n
, t h a t i s u s e d b y R a i l t i e s a n d A p p l i c
a t i o n
t o s t o r e r e l a t e d c o n f i g u r a t i o n .
S o u r c e :
s h o w
|
o n G i t H u b
def config
@config ||= Railtie :: Configuration .new
end