From 2c8bc2cdcdb7b144b7a583d24922fd8064bcff3d Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Fri, 6 Sep 2013 21:41:04 +0300 Subject: Use Ruby on Rails Coding Conventions for code examples in the guides * Indent after private/protected * Ruby >= 1.9 syntax for hashes * Prefer method { do_stuff } instead of method{do_stuff} for single-line blocks. [ci skip] --- guides/source/initialization.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index c78eef5bf0..91d12b4432 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -217,12 +217,12 @@ With the `default_options` set to this: ```ruby def default_options { - :environment => ENV['RACK_ENV'] || "development", - :pid => nil, - :Port => 9292, - :Host => "0.0.0.0", - :AccessLog => [], - :config => "config.ru" + environment: ENV['RACK_ENV'] || "development", + pid: nil, + Port: 9292, + Host: "0.0.0.0", + AccessLog: [], + config: "config.ru" } end ``` -- cgit v1.2.3 From 7a1e7b38f2bad0234d5137d431fda4972a9c1932 Mon Sep 17 00:00:00 2001 From: Cory Foy Date: Wed, 9 Oct 2013 16:16:12 -0400 Subject: Modifies the initialization guide to make the initializers process clearer and remove a TODO. [ci skip] --- guides/source/initialization.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 91d12b4432..fe6b1ad906 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -468,14 +468,24 @@ def initialize!(group=:default) #:nodoc: end ``` -As you can see, you can only initialize an app once. This is also where the initializers are run. +As you can see, you can only initialize an app once. The initializers are run through +the `run_initializers` method which is defined in `railties/lib/rails/initializable.rb` -TODO: review this +```ruby +def run_initializers(group=:default, *args) + return if instance_variable_defined?(:@ran) + initializers.tsort_each do |initializer| + initializer.run(*args) if initializer.belongs_to?(group) + end + @ran = true +end +``` -The initializers code itself is tricky. What Rails is doing here is it -traverses all the class ancestors looking for an `initializers` method, -sorting them and running them. For example, the `Engine` class will make -all the engines available by providing the `initializers` method. +The run_initializers code itself is tricky. What Rails is doing here is +traversing all the class ancestors looking for those that respond to an +`initializers` method. It then sorts the ancestors by name, and runs them. +For example, the `Engine` class will make all the engines available by +providing an `initializers` method on them. The `Rails::Application` class, as defined in `railties/lib/rails/application.rb` defines `bootstrap`, `railtie`, and `finisher` initializers. The `bootstrap` initializers -- cgit v1.2.3 From 49062c12edc4b5397ea3bc6680eb6a13bdec2164 Mon Sep 17 00:00:00 2001 From: Rashmi Yadav Date: Fri, 1 Nov 2013 18:07:27 +0100 Subject: exists? is deprecated in ruby trunk [ci skip] see here 4d4ff531b8807ee88a3fc46875c7e76f613956fb --- guides/source/initialization.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index fe6b1ad906..7e9cb5a47d 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -54,7 +54,7 @@ The `APP_PATH` constant will be used later in `rails/commands`. The `config/boot # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) -require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) ``` In a standard Rails application, there's a `Gemfile` which declares all @@ -121,7 +121,7 @@ when 'server' # Change to the application's path if there is no config.ru file in current directory. # This allows us to run `rails server` from other directories, but still get # the main config.ru and properly set the tmp directory. - Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exists?(File.expand_path("config.ru")) + Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru")) require 'rails/commands/server' Rails::Server.new.tap do |server| -- cgit v1.2.3 From cb632188b33c2eaf7505c3df85e5ced354cbe2d2 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Wed, 6 Nov 2013 18:24:22 +0530 Subject: Update Rails initialization guide to explain how bin/rails is executed --- guides/source/initialization.md | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 7e9cb5a47d..33eb74dcd9 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -29,9 +29,42 @@ quickly. Launch! ------- -Let's start to boot and initialize the app. It all begins with your app's -`bin/rails` executable. A Rails application is usually started by running -`rails console` or `rails server`. +Let's start to boot and initialize the app. A Rails application is usually +started by running `rails console` or `rails server`. + +### `railties/bin/rails` + +The `rails` in the command `rails server` is a ruby executable in your load +path. This executable contains the following lines: + +```ruby +version = ">= 0" +load Gem.bin_path('railties', 'rails', version) +``` + +If you try out this command in a Rails console, you would see that this loads +`railties/bin/rails`. A part of the file `railties/bin/rails.rb` has the +following code: + +```ruby +require "rails/cli" +``` + +The file `railties/lib/rails/cli` in turn calls +`Rails::AppRailsLoader.exec_app_rails`. + +### `railties/lib/rails/app_rails_loader.rb` + +The primary goal of the function `exec_app_rails` is to execute your app's +`bin/rails`. If the current directory does not have a `bin/rails`, it will +navigate upwards until it finds a `bin/rails` executable. Thus one can invoke a +`rails` command from anywhere inside a rails application. + +For `rails server` the equivalent of the following command is executed: + +```bash +$ exec ruby bin/rails server +``` ### `bin/rails` -- cgit v1.2.3 From 3d449dee5ed60b4d6c688c919c527dd70f528043 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Fri, 8 Nov 2013 15:07:38 +0530 Subject: Update commands.rb content in initialization guide [ci skip] --- guides/source/initialization.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 33eb74dcd9..29c7294145 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -126,7 +126,7 @@ A standard Rails application depends on several gems, specifically: ### `rails/commands.rb` -Once `config/boot.rb` has finished, the next file that is required is `rails/commands` which will execute a command based on the arguments passed in. In this case, the `ARGV` array simply contains `server` which is extracted into the `command` variable using these lines: +Once `config/boot.rb` has finished, the next file that is required is `rails/commands`, which helps in expanding aliases. In the current case, the `ARGV` array simply contains `server` which will be passed over to `rails/commands_tasks`. ```ruby ARGV << '--help' if ARGV.empty? @@ -142,12 +142,18 @@ aliases = { command = ARGV.shift command = aliases[command] || command + +require 'rails/commands/commands_tasks' + +Rails::CommandsTasks.new(ARGV).run_command!(command) ``` TIP: As you can see, an empty ARGV list will make Rails show the help snippet. -If we used `s` rather than `server`, Rails will use the `aliases` defined in the file and match them to their respective commands. With the `server` command, Rails will run this code: +If we had used `s` rather than `server`, Rails would have used the `aliases` defined here to find the matching command. + +With the `server` command, Rails will run this code: ```ruby when 'server' -- cgit v1.2.3 From 83271a64f961e8138bf1e125251860333ba88f72 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Fri, 8 Nov 2013 15:13:59 +0530 Subject: Add section for command_tasks in initialization guide [ci skip] --- guides/source/initialization.md | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 29c7294145..0cd0492cab 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -126,7 +126,10 @@ A standard Rails application depends on several gems, specifically: ### `rails/commands.rb` -Once `config/boot.rb` has finished, the next file that is required is `rails/commands`, which helps in expanding aliases. In the current case, the `ARGV` array simply contains `server` which will be passed over to `rails/commands_tasks`. +Once `config/boot.rb` has finished, the next file that is required is +`rails/commands`, which helps in expanding aliases. In the current case, the +`ARGV` array simply contains `server` which will be passed over to +`rails/commands_tasks`. ```ruby ARGV << '--help' if ARGV.empty? @@ -151,28 +154,55 @@ Rails::CommandsTasks.new(ARGV).run_command!(command) TIP: As you can see, an empty ARGV list will make Rails show the help snippet. -If we had used `s` rather than `server`, Rails would have used the `aliases` defined here to find the matching command. +If we had used `s` rather than `server`, Rails would have used the `aliases` +defined here to find the matching command. -With the `server` command, Rails will run this code: +### `rails/commands/command_tasks.rb` + +When one types an incorrect rails command, the `run_command` is responsible for +throwing an error message. If the command is valid, a function of the same name +is called. + +```ruby +COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help) + +def run_command!(command) + if COMMAND_WHITELIST.include?(command) + send(command) + else + write_error_message(command) + end +end +``` + +With the `server` command, Rails will further run the following code: ```ruby -when 'server' - # Change to the application's path if there is no config.ru file in current directory. - # This allows us to run `rails server` from other directories, but still get - # the main config.ru and properly set the tmp directory. - Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru")) +def set_application_directory! + Dir.chdir(File.expand_path('../../', APP_PATH)) unless + File.exist?(File.expand_path("config.ru")) +end + +def server + set_application_directory! + require_command!("server") - require 'rails/commands/server' Rails::Server.new.tap do |server| - # We need to require application after the server sets environment, - # otherwise the --environment option given to the server won't propagate. require APP_PATH Dir.chdir(Rails.application.root) server.start end +end + +def require_command!(command) + require "rails/commands/#{command}" +end ``` -This file will change into the Rails root directory (a path two directories up from `APP_PATH` which points at `config/application.rb`), but only if the `config.ru` file isn't found. This then requires `rails/commands/server` which sets up the `Rails::Server` class. +This file will change into the Rails root directory (a path two directories up +from `APP_PATH` which points at `config/application.rb`), but only if the +`config.ru` file isn't found. This then requires `rails/commands/server` which +sets up the `Rails::Server` class. ```ruby require 'fileutils' -- cgit v1.2.3 From 5735a77def338125738c6de9c72961bb4df49513 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Tue, 12 Nov 2013 19:14:10 +0530 Subject: Fix RailsServer#start content in initialization guide [ci skip] --- guides/source/initialization.md | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 0cd0492cab..f1bbb5bb13 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -330,37 +330,43 @@ and it's free for you to change based on your needs. ### `Rails::Server#start` -After `config/application` is loaded, `server.start` is called. This method is defined like this: +After `config/application` is loaded, `server.start` is called. This method is +defined like this: ```ruby def start - url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" - puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" - puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}" - puts "=> Run `rails server -h` for more startup options" + print_boot_information trap(:INT) { exit } - puts "=> Ctrl-C to shutdown server" unless options[:daemonize] + create_tmp_directories + log_to_stdout if options[:log_stdout] - #Create required tmp directories if not found - %w(cache pids sessions sockets).each do |dir_to_make| - FileUtils.mkdir_p(Rails.root.join('tmp', dir_to_make)) + super + ... +end + +private + + def print_boot_information + ... + puts "=> Run `rails server -h` for more startup options" + puts "=> Ctrl-C to shutdown server" unless options[:daemonize] end - unless options[:daemonize] + def create_tmp_directories + %w(cache pids sessions sockets).each do |dir_to_make| + FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make)) + end + end + + def log_to_stdout wrapped_app # touch the app so the logger is set up console = ActiveSupport::Logger.new($stdout) console.formatter = Rails.logger.formatter + console.level = Rails.logger.level Rails.logger.extend(ActiveSupport::Logger.broadcast(console)) end - - super -ensure - # The '-h' option calls exit before @options is set. - # If we call 'options' with it unset, we get double help banners. - puts 'Exiting' unless @options && options[:daemonize] -end ``` This is where the first output of the Rails initialization happens. This -- cgit v1.2.3 From 28a6a7ea3bd627a8b6693a4cb8305b89467592b4 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 24 Nov 2013 19:55:46 +0100 Subject: a couple of copy-edits before merging [ci skip] --- guides/source/initialization.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'guides/source/initialization.md') diff --git a/guides/source/initialization.md b/guides/source/initialization.md index f1bbb5bb13..5e2e0ad3e3 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -128,8 +128,7 @@ A standard Rails application depends on several gems, specifically: Once `config/boot.rb` has finished, the next file that is required is `rails/commands`, which helps in expanding aliases. In the current case, the -`ARGV` array simply contains `server` which will be passed over to -`rails/commands_tasks`. +`ARGV` array simply contains `server` which will be passed over: ```ruby ARGV << '--help' if ARGV.empty? @@ -160,7 +159,7 @@ defined here to find the matching command. ### `rails/commands/command_tasks.rb` When one types an incorrect rails command, the `run_command` is responsible for -throwing an error message. If the command is valid, a function of the same name +throwing an error message. If the command is valid, a method of the same name is called. ```ruby -- cgit v1.2.3