Skip to content
This repository
Browse code

Cover more of the Rails initialization process, regarding the interna…

…ls of YourApp::Application inheritance from Rails::Application and more.
  • Loading branch information...
commit 22a32f4dd6c9f4d142f17c85949ea5d20534485e 1 parent 8ce954d
Ryan Bigg radar authored mikel committed

Showing 1 changed file with 63 additions and 3 deletions. Show diff stats Hide diff stats

  1. +63 3 railties/guides/source/initialization.textile
66 railties/guides/source/initialization.textile
Source Rendered
@@ -1819,7 +1819,7 @@ On the surface, this looks like a simple class inheritance. There's more underne
1819 1819 end
1820 1820 </ruby>
1821 1821
1822   -We do not already have a +Rails.application+, so instead this resorts to calling +super+. +Rails::Application+ descends from +Rails::Engine+ and so will call the +inherited+ method in +Rails::Engine+, but before that it's important to note that +called_from+ is defined an +attr_accessor+ on +Rails::Engine+:
  1822 +We do not already have a +Rails.application+, so instead this resorts to calling +super+. +Rails::Application+ descends from +Rails::Engine+ and so will call the +inherited+ method in +Rails::Engine+ (in _railties/lib/rails/engine.rb_), but before that it's important to note that +called_from+ is defined an +attr_accessor+ on +Rails::Engine+ and that +YourApp::Application+ is not an +abstract_railtie+:
1823 1823
1824 1824 <ruby>
1825 1825 def inherited(base)
@@ -1835,8 +1835,68 @@ We do not already have a +Rails.application+, so instead this resorts to calling
1835 1835 end
1836 1836 </ruby>
1837 1837
1838   -This +called_from+ setting looks a little overwhelming to begin with, but the short end of it is that it returns the route to your application's config directory, something like: _/home/you/yourapp/config_. After +called_from+ has been set, +super+ is again called and this means the +Rails::Railtie#inherited+ method.
1839   -
  1838 +This +called_from+ setting looks a little overwhelming to begin with, but the short end of it is that it returns the route to your application's config directory, something like: _/home/you/yourapp/config_. After +called_from+ has been set, +super+ is again called and this means the +Rails::Railtie#inherited+ method (in _railties/lib/rails/railtie.rb_):
  1839 +
  1840 +<ruby>
  1841 + def inherited(base)
  1842 + unless base.abstract_railtie?
  1843 + base.send(:include, self::Configurable)
  1844 + subclasses << base
  1845 + end
  1846 + end
  1847 +</ruby>
  1848 +
  1849 +Again, +YourApp::Application+ will return false for +abstract_railtie+ and so the code inside the +unless+ will be ran. The first line:
  1850 +
  1851 +<ruby>
  1852 + base.send(:include, self::Configurable)
  1853 +</ruby>
  1854 +
  1855 +includes the +self::Configurable+ module, with self being +Rails::Application+ in this context:
  1856 +
  1857 +<ruby>
  1858 + module Rails
  1859 + class Application
  1860 + module Configurable
  1861 + def self.included(base)
  1862 + base.extend ClassMethods
  1863 + end
  1864 +
  1865 + module ClassMethods
  1866 + def inherited(base)
  1867 + raise "You cannot inherit from a Rails::Application child"
  1868 + end
  1869 + end
  1870 +
  1871 + def config
  1872 + @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd))
  1873 + end
  1874 + end
  1875 + end
  1876 + end
  1877 +</ruby>
  1878 +
  1879 +The inclusion of the +Rails::Application::Configurable+ module triggers the +included+ method in here which extends +YourApp::Application+ with the +Rails::Application::Configurable::ClassMethods+.
  1880 +
  1881 +Now that the chain of +super+ calls is done, we'll go back to the original +inherited+ method in +Rails::Application+ and the final line in this method:
  1882 +
  1883 +<ruby>
  1884 + Rails.application = base.instance
  1885 +</ruby>
  1886 +
  1887 ++base+ in this case is +YourApp::Application+ and calling +instance+ on this will return an instance of +YourApp::Application+ through the +instance+ method defined here:
  1888 +
  1889 +<ruby>
  1890 + def instance
  1891 + if self == Rails::Application
  1892 + Rails.application
  1893 + else
  1894 + @@instance ||= new
  1895 + end
  1896 + end
  1897 +</ruby>
  1898 +
  1899 ++self+ in this case is +YourApp::Application+, so it won't match to +Rails::Application+ so instead the +new+ method is called which calls the +initialize+ method.
1840 1900
1841 1901
1842 1902

0 comments on commit 22a32f4

Please sign in to comment.
Something went wrong with that request. Please try again.