class Rails::Railtie

Be sure to look at the documentation of those specific classes for more information.
used in both.
Rails::Application is an engine, the same configuration described here can be
An engine is nothing more than a railtie with some initializers already set. And since
== Application and Engine
end
end
WebpackServer.start
server do
class MyRailtie < Rails::Railtie
It can be used like this:
this less confusing for everyone.
This way, your users don’t need to remember to have to open a new shell and run another program, making
Rails allow you to specify a server block which will get called when a Rails server starts.
like Sidekiq. This is usually done by opening a new shell and running the program from here.
you might want to start the Webpack or React server. Or maybe you need to run your job scheduler process
In development, it’s very usual to have to run another process next to the Rails Server. In example
== Run another program when the Rails server starts
through a railtie have unique names.
Since filenames on the load path are shared across gems, be sure that files you load
end
end
require “path/to/my_railtie_generator”
generators do
class MyRailtie < Rails::Railtie
will load them during normal generators lookup:
your generators at a different location, you can specify in your railtie a block which
By default, Rails loads generators from your load path. However, if you want to place
end
end
load “path/to/my_railtie.tasks”
rake_tasks do
class MyRailtie < Rails::Railtie
rake_tasks:
If your railtie has Rake tasks, you can tell Rails to load them through the method
== Loading Rake Tasks and Generators
end
end
MyRailtie.setup!
config.to_prepare do
# and before each request in development.
# Add a to_prepare block which is executed once in production
config.app_generators.orm :my_railtie_orm
# Customize the ORM
class MyRailtie < Rails::Railtie
railties and the application:
Railties can access a config object which contains configuration shared by all
== Configuration
initialization process.
initializer, in case you want to couple it with a specific step in the
Finally, you can also pass :before and :after as options to
end
end
app.middleware.use MyRailtie::Middleware
initializer “my_railtie.configure_rails_initialization” do |app|
class MyRailtie < Rails::Railtie
need to access some application-specific configuration, like middleware:
If specified, the block can also receive the application object, in case you
end
end
# some initialization behavior
initializer “my_railtie.configure_rails_initialization” do
class MyRailtie < Rails::Railtie
define the initialization code with the initializer macro:
To add an initialization step to the Rails boot process from your railtie, just
== Initializers
require “my_gem/railtie” if defined?(Rails::Railtie)
# lib/my_gem.rb
end
end
class Railtie < Rails::Railtie
module MyGem
# lib/my_gem/railtie.rb
without Rails.
The following example demonstrates an extension which can be used with or
called MyNamespace::Railtie.
This class must be loaded during the Rails boot process, and is conventionally
To extend Rails using a railtie, create a subclass of Rails::Railtie.
== Creating a Railtie
* adding Rake tasks
* setting up a subscriber with ActiveSupport::Notifications
* adding config.* keys to the environment
* configuring a Rails framework for the application, like setting a generator
* creating initializers
For example, an extension doing any of the following would need a railtie:
a railtie is needed.
if you need to interact with the Rails framework during or after boot, then
Developing a Rails extension does not require implementing a railtie, but
allowing other components to be used in place of any of the Rails defaults.
own initialization. This makes Rails itself absent of any component hooks,
Record, etc.) implements a railtie. Each of them is responsible for their
Every major component of Rails (Action Mailer, Action Controller, Active
several hooks to extend Rails and/or modify the initialization process.
Rails::Railtie is the core of the Rails framework and provides

def <=>(other) # :nodoc:

:nodoc:
def <=>(other) # :nodoc:
  load_index <=> other.load_index
end

def abstract_railtie?

def abstract_railtie?
  ABSTRACT_RAILTIES.include?(name)
end

def config

related configuration.
Railtie::Configuration, that is used by Railties and Application to store
This is used to create the config object on Railties, an instance of
def config
  @config ||= Railtie::Configuration.new
end

def configure(&block)

subclasses of Railtie so we provide the class method here.
Railtie::Configurable, but this module is no longer required for all
Allows you to configure the railtie. This is the same method seen in
def configure(&block)
  instance.configure(&block)
end

def configure(&block) # :nodoc:

:nodoc:
def configure(&block) # :nodoc:
  instance_eval(&block)
end

def console(&blk)

def console(&blk)
  register_block_for(:load_console, &blk)
end

def each_registered_block(type, &block)

run `&block` in every registered block in `#register_block_for`
def each_registered_block(type, &block)
  klass = self.class
  while klass.respond_to?(type)
    klass.public_send(type).each(&block)
    klass = klass.superclass
  end
end

def generate_railtie_name(string)

def generate_railtie_name(string)
  ActiveSupport::Inflector.underscore(string).tr("/", "_")
end

def generators(&blk)

def generators(&blk)
  register_block_for(:generators, &blk)
end

def increment_load_index

def increment_load_index
  @@load_counter ||= 0
  @load_index = (@@load_counter += 1)
end

def inherited(subclass)

def inherited(subclass)
  subclass.increment_load_index
  super
end

def initialize # :nodoc:

:nodoc:
def initialize # :nodoc:
  if self.class.abstract_railtie?
    raise "#{self.class.name} is abstract, you cannot instantiate it directly."
  end
end

def inspect # :nodoc:

:nodoc:
def inspect # :nodoc:
  "#<#{self.class.name}>"
end

def instance

+instance+ are intended to be called only on subclasses of a Railtie.
Since Rails::Railtie cannot be instantiated, any methods that call
def instance
  @instance ||= new
end

def method_missing(name, *args, &block)

to the Railtie instance.
If the class method does not have a method, then send the method call
def method_missing(name, *args, &block)
  if !abstract_railtie? && instance.respond_to?(name)
    instance.public_send(name, *args, &block)
  else
    super
  end
end

def railtie_name(name = nil)

def railtie_name(name = nil)
  @railtie_name = name.to_s if name
  @railtie_name ||= generate_railtie_name(self.name)
end

def railtie_namespace # :nodoc:

:nodoc:
def railtie_namespace # :nodoc:
  @railtie_namespace ||= self.class.module_parents.detect { |n| n.respond_to?(:railtie_namespace) }
end

def rake_tasks(&blk)

def rake_tasks(&blk)
  register_block_for(:rake_tasks, &blk)
end

def register_block_for(type, &blk)

`#each_registered_block(type, &block)`
blank and append given block to value, which will be used later in
receives an instance variable identifier, set the variable value if is
def register_block_for(type, &blk)
  var_name = "@#{type}"
  blocks = instance_variable_defined?(var_name) ? instance_variable_get(var_name) : instance_variable_set(var_name, [])
  blocks << blk if blk
  blocks
end

def respond_to_missing?(name, _)

def respond_to_missing?(name, _)
  return super if abstract_railtie?
  instance.respond_to?(name) || super
end

def run_console_blocks(app) # :nodoc:

:nodoc:
def run_console_blocks(app) # :nodoc:
  each_registered_block(:console) { |block| block.call(app) }
end

def run_generators_blocks(app) # :nodoc:

:nodoc:
def run_generators_blocks(app) # :nodoc:
  each_registered_block(:generators) { |block| block.call(app) }
end

def run_runner_blocks(app) # :nodoc:

:nodoc:
def run_runner_blocks(app) # :nodoc:
  each_registered_block(:runner) { |block| block.call(app) }
end

def run_server_blocks(app) # :nodoc:

:nodoc:
def run_server_blocks(app) # :nodoc:
  each_registered_block(:server) { |block| block.call(app) }
end

def run_tasks_blocks(app) # :nodoc:

:nodoc:
def run_tasks_blocks(app) # :nodoc:
  extend Rake::DSL
  each_registered_block(:rake_tasks) { |block| instance_exec(app, &block) }
end

def runner(&blk)

def runner(&blk)
  register_block_for(:runner, &blk)
end

def server(&blk)

def server(&blk)
  register_block_for(:server, &blk)
end

def subclasses

def subclasses
  super.reject(&:abstract_railtie?).sort
end