From 40c8ca5b094dfbdc3b4131492a7a82ea7a17488f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Mon, 4 Jun 2012 01:03:39 +0200 Subject: [PATCH 01/34] remove unnecessary text --- guides/source/layouts_and_rendering.textile | 2 -- 1 file changed, 2 deletions(-) diff --git a/guides/source/layouts_and_rendering.textile b/guides/source/layouts_and_rendering.textile index b0a87a5981..55bd521419 100644 --- a/guides/source/layouts_and_rendering.textile +++ b/guides/source/layouts_and_rendering.textile @@ -23,8 +23,6 @@ From the controller's point of view, there are three ways to create an HTTP resp * Call +redirect_to+ to send an HTTP redirect status code to the browser * Call +head+ to create a response consisting solely of HTTP headers to send back to the browser -I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all. - h4. Rendering by Default: Convention Over Configuration in Action You've heard that Rails promotes "convention over configuration". Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your +BooksController+ class: From cba59f1ed1ea484cbbb5729b8ba57e4b0128cdac Mon Sep 17 00:00:00 2001 From: John Yani Date: Mon, 4 Jun 2012 13:12:24 +0300 Subject: [PATCH 02/34] bundle exec is needed even if running a single test. --- guides/source/contributing_to_ruby_on_rails.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index acf75d41cd..0da8a84b92 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -109,7 +109,7 @@ You can run any single test separately too: $ cd actionpack -$ ruby -Itest test/template/form_helper_test.rb +$ bundle exec ruby -Itest test/template/form_helper_test.rb h4. Warnings From d63b69ba7f2b9111aeb04b50884949c2a4b6e584 Mon Sep 17 00:00:00 2001 From: John Yani Date: Tue, 5 Jun 2012 02:07:21 +0300 Subject: [PATCH 03/34] Add a note about JavaScript runtime --- guides/source/contributing_to_ruby_on_rails.textile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index 0da8a84b92..eb049aa357 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -72,6 +72,13 @@ Also, SQLite3 and its development files for the +sqlite3-ruby+ gem -- in Ubuntu $ sudo apt-get install sqlite3 libsqlite3-dev +Some tests use "execjs":https://github.com/sstephenson/execjs and require JavaScript runtime to be installed. Node.js is the easiest one to install: + + +$ sudo apt-get install nodejs + + + Get a recent version of "Bundler":http://gembundler.com/: From 4072de266a74edf4f432a200a322cf1ffd38d78e Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 4 Jun 2012 23:21:48 -0500 Subject: [PATCH 04/34] remove double hyphen that doesn't allow properly parsing --- activesupport/lib/active_support/core_ext/object/try.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 30c835f5cd..16a799ec03 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -11,8 +11,6 @@ class Object # subclasses of +BasicObject+. For example, using try with +SimpleDelegator+ will # delegate +try+ to target instead of calling it on delegator itself. # - # ==== Examples - # # Without +try+ # @person && @person.name # or @@ -27,7 +25,7 @@ class Object # # Without a method argument try will yield to the block unless the receiver is nil. # @person.try { |p| "#{p.first_name} #{p.last_name}" } - #-- + # # +try+ behaves like +Object#public_send+, unless called on +NilClass+. def try(*a, &b) if a.empty? && block_given? @@ -42,8 +40,6 @@ class NilClass # Calling +try+ on +nil+ always returns +nil+. # It becomes specially helpful when navigating through associations that may return +nil+. # - # === Examples - # # nil.try(:name) # => nil # # Without +try+ From 20ded911c0ce7647c73822491eaf8db10e615844 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 4 Jun 2012 23:43:43 -0500 Subject: [PATCH 05/34] add example to AS::Gzip --- activesupport/lib/active_support/gzip.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/gzip.rb b/activesupport/lib/active_support/gzip.rb index 420b965c87..6ef33ab683 100644 --- a/activesupport/lib/active_support/gzip.rb +++ b/activesupport/lib/active_support/gzip.rb @@ -2,7 +2,14 @@ require 'zlib' require 'stringio' module ActiveSupport - # A convenient wrapper for the zlib standard library that allows compression/decompression of strings with gzip. + # A convenient wrapper for the zlib standard library that allows + # compression/decompression of strings with gzip. + # + # gzip = ActiveSupport::Gzip.compress('compress me!') + # # => "\x1F\x8B\b\x00o\x8D\xCDO\x00\x03K\xCE\xCF-(J-.V\xC8MU\x04\x00R>n\x83\f\x00\x00\x00" + # + # ActiveSupport::Gzip.decompress(gzip) + # # => "compress me!" module Gzip class Stream < StringIO def initialize(*) From 1790b234e4354c8aa2f1704bd80300c511ffd1ca Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Tue, 5 Jun 2012 15:53:20 -0500 Subject: [PATCH 06/34] add :nodoc: to AS::NumberHelper private methods [ci skip] --- .../lib/active_support/number_helper.rb | 189 +++++++++--------- 1 file changed, 95 insertions(+), 94 deletions(-) diff --git a/activesupport/lib/active_support/number_helper.rb b/activesupport/lib/active_support/number_helper.rb index fc97782697..c736041066 100644 --- a/activesupport/lib/active_support/number_helper.rb +++ b/activesupport/lib/active_support/number_helper.rb @@ -24,17 +24,17 @@ module ActiveSupport # number. # ==== Examples # - # number_to_phone(5551234) # => 555-1234 - # number_to_phone("5551234") # => 555-1234 - # number_to_phone(1235551234) # => 123-555-1234 - # number_to_phone(1235551234, :area_code => true) # => (123) 555-1234 - # number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234 - # number_to_phone(1235551234, :area_code => true, :extension => 555) # => (123) 555-1234 x 555 - # number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234 - # number_to_phone("123a456") # => 123a456 + # number_to_phone(5551234) # => 555-1234 + # number_to_phone("5551234") # => 555-1234 + # number_to_phone(1235551234) # => 123-555-1234 + # number_to_phone(1235551234, area_code: true) # => (123) 555-1234 + # number_to_phone(1235551234, delimiter: ' ') # => 123 555 1234 + # number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555 + # number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234 + # number_to_phone("123a456") # => 123a456 # - # number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".") - # # => +1.123.555.1234 x 1343 + # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.') + # # => +1.123.555.1234 x 1343 def number_to_phone(number, options = {}) return unless number options = options.symbolize_keys @@ -85,18 +85,18 @@ module ActiveSupport # # ==== Examples # - # number_to_currency(1234567890.50) # => $1,234,567,890.50 - # number_to_currency(1234567890.506) # => $1,234,567,890.51 - # number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506 - # number_to_currency(1234567890.506, :locale => :fr) # => 1 234 567 890,51 € - # number_to_currency("123a456") # => $123a456 + # number_to_currency(1234567890.50) # => $1,234,567,890.50 + # number_to_currency(1234567890.506) # => $1,234,567,890.51 + # number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506 + # number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51 € + # number_to_currency('123a456') # => $123a456 # - # number_to_currency(-1234567890.50, :negative_format => "(%u%n)") - # # => ($1,234,567,890.50) - # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "") - # # => £1234567890,50 - # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u") - # # => 1234567890,50 £ + # number_to_currency(-1234567890.50, negative_format: '(%u%n)') + # # => ($1,234,567,890.50) + # number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '') + # # => £1234567890,50 + # number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '', format: '%n %u') + # # => 1234567890,50 £ def number_to_currency(number, options = {}) return unless number options = options.symbolize_keys @@ -144,15 +144,14 @@ module ActiveSupport # # ==== Examples # - # number_to_percentage(100) # => 100.000% - # number_to_percentage("98") # => 98.000% - # number_to_percentage(100, :precision => 0) # => 100% - # number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000% - # number_to_percentage(302.24398923423, :precision => 5) # => 302.24399% - # number_to_percentage(1000, :locale => :fr) # => 1 000,000% - # number_to_percentage("98a") # => 98a% - # number_to_percentage(100, :format => "%n %") # => 100 % - # + # number_to_percentage(100) # => 100.000% + # number_to_percentage('98') # => 98.000% + # number_to_percentage(100, precision: 0) # => 100% + # number_to_percentage(1000, delimiter: '.', separator: ,') # => 1.000,000% + # number_to_percentage(302.24398923423, precision: 5) # => 302.24399% + # number_to_percentage(1000, :locale => :fr) # => 1 000,000% + # number_to_percentage('98a') # => 98a% + # number_to_percentage(100, format: '%n %') # => 100 % def number_to_percentage(number, options = {}) return unless number options = options.symbolize_keys @@ -181,16 +180,16 @@ module ActiveSupport # # ==== Examples # - # number_to_delimited(12345678) # => 12,345,678 - # number_to_delimited("123456") # => 123,456 - # number_to_delimited(12345678.05) # => 12,345,678.05 - # number_to_delimited(12345678, :delimiter => ".") # => 12.345.678 - # number_to_delimited(12345678, :delimiter => ",") # => 12,345,678 - # number_to_delimited(12345678.05, :separator => " ") # => 12,345,678 05 - # number_to_delimited(12345678.05, :locale => :fr) # => 12 345 678,05 - # number_to_delimited("112a") # => 112a - # number_to_delimited(98765432.98, :delimiter => " ", :separator => ",") - # # => 98 765 432,98 + # number_to_delimited(12345678) # => 12,345,678 + # number_to_delimited('123456') # => 123,456 + # number_to_delimited(12345678.05) # => 12,345,678.05 + # number_to_delimited(12345678, delimiter: '.') # => 12.345.678 + # number_to_delimited(12345678, delimiter: ',') # => 12,345,678 + # number_to_delimited(12345678.05, separator: ' ') # => 12,345,678 05 + # number_to_delimited(12345678.05, locale: :fr) # => 12 345 678,05 + # number_to_delimited('112a') # => 112a + # number_to_delimited(98765432.98, delimiter: ' ', separator: ',') + # # => 98 765 432,98 def number_to_delimited(number, options = {}) options = options.symbolize_keys @@ -227,21 +226,21 @@ module ActiveSupport # # ==== Examples # - # number_to_rounded(111.2345) # => 111.235 - # number_to_rounded(111.2345, :precision => 2) # => 111.23 - # number_to_rounded(13, :precision => 5) # => 13.00000 - # number_to_rounded(389.32314, :precision => 0) # => 389 - # number_to_rounded(111.2345, :significant => true) # => 111 - # number_to_rounded(111.2345, :precision => 1, :significant => true) # => 100 - # number_to_rounded(13, :precision => 5, :significant => true) # => 13.000 - # number_to_rounded(111.234, :locale => :fr) # => 111,234 + # number_to_rounded(111.2345) # => 111.235 + # number_to_rounded(111.2345, precision: 2) # => 111.23 + # number_to_rounded(13, precision: 5) # => 13.00000 + # number_to_rounded(389.32314, precision: 0) # => 389 + # number_to_rounded(111.2345, significant: true) # => 111 + # number_to_rounded(111.2345, precision: 1, significant: true) # => 100 + # number_to_rounded(13, precision: 5, significant: true) # => 13.000 + # number_to_rounded(111.234, locale: :fr) # => 111,234 # - # number_to_rounded(13, :precision => 5, :significant => true, :strip_insignificant_zeros => true) - # # => 13 + # number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true) + # # => 13 # - # number_to_rounded(389.32314, :precision => 4, :significant => true) # => 389.3 - # number_to_rounded(1111.2345, :precision => 2, :separator => ',', :delimiter => '.') - # # => 1.111,23 + # number_to_rounded(389.32314, precision: 4, significant: true) # => 389.3 + # number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.') + # # => 1.111,23 def number_to_rounded(number, options = {}) options = options.symbolize_keys @@ -309,21 +308,21 @@ module ActiveSupport # # ==== Examples # - # number_to_human_size(123) # => 123 Bytes - # number_to_human_size(1234) # => 1.21 KB - # number_to_human_size(12345) # => 12.1 KB - # number_to_human_size(1234567) # => 1.18 MB - # number_to_human_size(1234567890) # => 1.15 GB - # number_to_human_size(1234567890123) # => 1.12 TB - # number_to_human_size(1234567, :precision => 2) # => 1.2 MB - # number_to_human_size(483989, :precision => 2) # => 470 KB - # number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,2 MB + # number_to_human_size(123) # => 123 Bytes + # number_to_human_size(1234) # => 1.21 KB + # number_to_human_size(12345) # => 12.1 KB + # number_to_human_size(1234567) # => 1.18 MB + # number_to_human_size(1234567890) # => 1.15 GB + # number_to_human_size(1234567890123) # => 1.12 TB + # number_to_human_size(1234567, precision: 2) # => 1.2 MB + # number_to_human_size(483989, precision: 2) # => 470 KB + # number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB # - # Non-significant zeros after the fractional separator are - # stripped out by default (set - # :strip_insignificant_zeros to +false+ to change that): - # number_to_human_size(1234567890123, :precision => 5) # => "1.1229 TB" - # number_to_human_size(524288000, :precision => 5) # => "500 MB" + # Non-significant zeros after the fractional separator are stripped out by + # default (set :strip_insignificant_zeros to +false+ to change that): + # + # number_to_human_size(1234567890123, precision: 5) # => "1.1229 TB" + # number_to_human_size(524288000, precision: 5) # => "500 MB" def number_to_human_size(number, options = {}) options = options.symbolize_keys @@ -407,27 +406,28 @@ module ActiveSupport # # ==== Examples # - # number_to_human(123) # => "123" - # number_to_human(1234) # => "1.23 Thousand" - # number_to_human(12345) # => "12.3 Thousand" - # number_to_human(1234567) # => "1.23 Million" - # number_to_human(1234567890) # => "1.23 Billion" - # number_to_human(1234567890123) # => "1.23 Trillion" - # number_to_human(1234567890123456) # => "1.23 Quadrillion" - # number_to_human(1234567890123456789) # => "1230 Quadrillion" - # number_to_human(489939, :precision => 2) # => "490 Thousand" - # number_to_human(489939, :precision => 4) # => "489.9 Thousand" - # number_to_human(1234567, :precision => 4, - # :significant => false) # => "1.2346 Million" - # number_to_human(1234567, :precision => 1, - # :separator => ',', - # :significant => false) # => "1,2 Million" + # number_to_human(123) # => "123" + # number_to_human(1234) # => "1.23 Thousand" + # number_to_human(12345) # => "12.3 Thousand" + # number_to_human(1234567) # => "1.23 Million" + # number_to_human(1234567890) # => "1.23 Billion" + # number_to_human(1234567890123) # => "1.23 Trillion" + # number_to_human(1234567890123456) # => "1.23 Quadrillion" + # number_to_human(1234567890123456789) # => "1230 Quadrillion" + # number_to_human(489939, precision: 2) # => "490 Thousand" + # number_to_human(489939, precision: 4) # => "489.9 Thousand" + # number_to_human(1234567, precision: 4, + # significant: false) # => "1.2346 Million" + # number_to_human(1234567, precision: 1, + # separator: ',', + # significant: false) # => "1,2 Million" # # Non-significant zeros after the decimal separator are stripped # out by default (set :strip_insignificant_zeros to # +false+ to change that): - # number_to_human(12345012345, :significant_digits => 6) # => "12.345 Billion" - # number_to_human(500000000, :precision => 5) # => "500 Million" + # + # number_to_human(12345012345, significant_digits: 6) # => "12.345 Billion" + # number_to_human(500000000, precision: 5) # => "500 Million" # # ==== Custom Unit Quantifiers # @@ -435,6 +435,7 @@ module ActiveSupport # number_to_human(500000, :units => {:unit => "ml", :thousand => "lt"}) # => "500 lt" # # If in your I18n locale you have: + # # distance: # centi: # one: "centimeter" @@ -449,12 +450,12 @@ module ActiveSupport # # Then you could do: # - # number_to_human(543934, :units => :distance) # => "544 kilometers" - # number_to_human(54393498, :units => :distance) # => "54400 kilometers" - # number_to_human(54393498000, :units => :distance) # => "54.4 gazillion-distance" - # number_to_human(343, :units => :distance, :precision => 1) # => "300 meters" - # number_to_human(1, :units => :distance) # => "1 meter" - # number_to_human(0.34, :units => :distance) # => "34 centimeters" + # number_to_human(543934, :units => :distance) # => "544 kilometers" + # number_to_human(54393498, :units => :distance) # => "54400 kilometers" + # number_to_human(54393498000, :units => :distance) # => "54.4 gazillion-distance" + # number_to_human(343, :units => :distance, :precision => 1) # => "300 meters" + # number_to_human(1, :units => :distance) # => "1 meter" + # number_to_human(0.34, :units => :distance) # => "34 centimeters" def number_to_human(number, options = {}) options = options.symbolize_keys @@ -505,22 +506,22 @@ module ActiveSupport end private_class_method :private_module_and_instance_method - def format_translations(namespace, locale) + def format_translations(namespace, locale) #:nodoc: defaults_translations(locale).merge(translations_for(namespace, locale)) end private_module_and_instance_method :format_translations - def defaults_translations(locale) + def defaults_translations(locale) #:nodoc: I18n.translate(:'number.format', :locale => locale, :default => {}) end private_module_and_instance_method :defaults_translations - def translations_for(namespace, locale) + def translations_for(namespace, locale) #:nodoc: I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {}) end private_module_and_instance_method :translations_for - def valid_float?(number) + def valid_float?(number) #:nodoc: Float(number) rescue ArgumentError, TypeError false From 5907b0b7f28132eb27a084536ee7766914bcc3a1 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Tue, 5 Jun 2012 20:42:08 -0700 Subject: [PATCH 07/34] Document ActiveSupport::JSON.parse_error [ci skip] --- activesupport/lib/active_support/json/decoding.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 72fd97ceee..e44939e78a 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -39,6 +39,14 @@ module ActiveSupport self.backend = old_backend end + # Returns the class of the error that will be raised when there is an error in decoding JSON. + # Using this method means you won't directly depend on the ActiveSupport's JSON implementation, in case it changes in the future. + # + # begin + # obj = ActiveSupport::JSON.decode(some_string) + # rescue ActiveSupport::JSON.parse_error + # Rails.logger.warn("Attempted to decode invalid JSON: #{some_string}") + # end def parse_error MultiJson::DecodeError end From 39856627e0e3d50db4eb400bdfaca3bc0958d211 Mon Sep 17 00:00:00 2001 From: Daniel Lopes Date: Thu, 7 Jun 2012 15:19:49 -0300 Subject: [PATCH 08/34] Document the CSRF whitelisting on get requests --- .../metal/request_forgery_protection.rb | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index 95b0e99ed5..eb7057d278 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -8,11 +8,22 @@ module ActionController #:nodoc: # Controller actions are protected from Cross-Site Request Forgery (CSRF) attacks # by including a token in the rendered html for your application. This token is # stored as a random string in the session, to which an attacker does not have - # access. When a request reaches your application, \Rails verifies the received - # token with the token in the session. Only HTML and JavaScript requests are checked, - # so this will not protect your XML API (presumably you'll have a different - # authentication scheme there anyway). Also, GET requests are not protected as these - # should be idempotent. + # access. When a request reaches your application, Rails verifies the received + # token with the token in the session. All requests are checked except GET requests + # as these should be idempotent. It's is important to remember that XML or JSON + # requests are also affected and if you're building an API you'll need + # something like that: + # + # class ApplicationController < ActionController::Base + # protect_from_forgery + # skip_before_filter :verify_authenticity_token, :if => json_request? + # + # protected + # + # def json_request? + # request.format.json? + # end + # end # # CSRF protection is turned on with the protect_from_forgery method, # which checks the token and resets the session if it doesn't match what was expected. From faf27445d0f3bccdde6624ac0c7e156fdb263e5b Mon Sep 17 00:00:00 2001 From: Daniel Lopes Date: Thu, 7 Jun 2012 15:33:38 -0300 Subject: [PATCH 09/34] fix typos on the CSRF whitelisting doc --- .../action_controller/metal/request_forgery_protection.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index eb7057d278..c99fed9212 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -8,10 +8,10 @@ module ActionController #:nodoc: # Controller actions are protected from Cross-Site Request Forgery (CSRF) attacks # by including a token in the rendered html for your application. This token is # stored as a random string in the session, to which an attacker does not have - # access. When a request reaches your application, Rails verifies the received + # access. When a request reaches your application, \Rails verifies the received # token with the token in the session. All requests are checked except GET requests - # as these should be idempotent. It's is important to remember that XML or JSON - # requests are also affected and if you're building an API you'll need + # as these should be idempotent. It's important to remember that XML or JSON + # requests are also affected and if you're building an API you'll need # something like that: # # class ApplicationController < ActionController::Base From 5c17aa510f155ff0833422c3342d14d6044153d9 Mon Sep 17 00:00:00 2001 From: Daniel Lopes Date: Thu, 7 Jun 2012 15:43:13 -0300 Subject: [PATCH 10/34] on CSRF whitelisting the argument for :if must be a symbol --- .../lib/action_controller/metal/request_forgery_protection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index c99fed9212..736f70af4c 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -16,7 +16,7 @@ module ActionController #:nodoc: # # class ApplicationController < ActionController::Base # protect_from_forgery - # skip_before_filter :verify_authenticity_token, :if => json_request? + # skip_before_filter :verify_authenticity_token, :if => :json_request? # # protected # From b1f634a3797003ab6276ee47fe187dd56e52c3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Britto?= Date: Fri, 8 Jun 2012 15:23:12 -0300 Subject: [PATCH 11/34] A blank line is required before code blocks. The generated output breaks otherwise. --- guides/source/active_support_core_extensions.textile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index 011a702901..ad3024d30d 100644 --- a/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -1857,6 +1857,7 @@ h4. Formatting Enables the formatting of numbers in a variety of ways. Produce a string representation of a number as a telephone number: + 5551234.to_s(:phone) # => 555-1234 1235551234.to_s(:phone) # => 123-555-1234 @@ -1867,6 +1868,7 @@ Produce a string representation of a number as a telephone number: Produce a string representation of a number as currency: + 1234567890.50.to_s(:currency) # => $1,234,567,890.50 1234567890.506.to_s(:currency) # => $1,234,567,890.51 @@ -1874,6 +1876,7 @@ Produce a string representation of a number as currency: Produce a string representation of a number as a percentage: + 100.to_s(:percentage) # => 100.000% 100.to_s(:percentage, :precision => 0) # => 100% @@ -1882,6 +1885,7 @@ Produce a string representation of a number as a percentage: Produce a string representation of a number in delimited form: + 12345678.to_s(:delimited) # => 12,345,678 12345678.05.to_s(:delimited) # => 12,345,678.05 @@ -1891,6 +1895,7 @@ Produce a string representation of a number in delimited form: Produce a string representation of a number rounded to a precision: + 111.2345.to_s(:rounded) # => 111.235 111.2345.to_s(:rounded, :precision => 2) # => 111.23 @@ -1900,6 +1905,7 @@ Produce a string representation of a number rounded to a precision: Produce a string representation of a number as a human-readable number of bytes: + 123.to_s(:human_size) # => 123 Bytes 1234.to_s(:human_size) # => 1.21 KB @@ -1910,6 +1916,7 @@ Produce a string representation of a number as a human-readable number of bytes: Produce a string representation of a number in human-readable words: + 123.to_s(:human) # => "123" 1234.to_s(:human) # => "1.23 Thousand" @@ -2469,6 +2476,7 @@ To do so, the method loops over the pairs and builds nodes that depend on the _v * If +value+ responds to +to_xml+ the method is invoked with +key+ as :root. * Otherwise, a node with +key+ as tag is created with a string representation of +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added. Unless the option :skip_types exists and is true, an attribute "type" is added as well according to the following mapping: + XML_TYPE_NAMES = { "Symbol" => "symbol", From f20208842ab1f5f6f44b08bda5beb35985c5dd5e Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Sat, 9 Jun 2012 16:04:01 -0300 Subject: [PATCH 12/34] Make observer enable/disable documentation more informative. * Moved the simplest case--enable/disable all on all--to the top. * Made clear what "ORM" means to avoid having to teach people how to solve "uninitialized constant ORM" errors in their test reports. --- activemodel/lib/active_model/observer_array.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/activemodel/lib/active_model/observer_array.rb b/activemodel/lib/active_model/observer_array.rb index 3d463885be..8de6918d18 100644 --- a/activemodel/lib/active_model/observer_array.rb +++ b/activemodel/lib/active_model/observer_array.rb @@ -17,6 +17,11 @@ module ActiveModel # Disables one or more observers. This supports multiple forms: # + # ORM.observers.disable :all + # # => disables all observers for all models subclassed from + # # an ORM base class that includes ActiveModel::Observing + # # e.g. ActiveRecord::Base + # # ORM.observers.disable :user_observer # # => disables the UserObserver # @@ -27,9 +32,6 @@ module ActiveModel # ORM.observers.disable :observer_1, :observer_2 # # => disables Observer1 and Observer2 for all models. # - # ORM.observers.disable :all - # # => disables all observers for all models. - # # User.observers.disable :all do # # all user observers are disabled for # # just the duration of the block @@ -40,6 +42,11 @@ module ActiveModel # Enables one or more observers. This supports multiple forms: # + # ORM.observers.enable :all + # # => enables all observers for all models subclassed from + # # an ORM base class that includes ActiveModel::Observing + # # e.g. ActiveRecord::Base + # # ORM.observers.enable :user_observer # # => enables the UserObserver # @@ -51,9 +58,6 @@ module ActiveModel # ORM.observers.enable :observer_1, :observer_2 # # => enables Observer1 and Observer2 for all models. # - # ORM.observers.enable :all - # # => enables all observers for all models. - # # User.observers.enable :all do # # all user observers are enabled for # # just the duration of the block From 2f6e8099e3cfd025fdca7a13162a5b2087da6db7 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sat, 9 Jun 2012 12:20:47 -0700 Subject: [PATCH 13/34] Add missing requires for rails/commands/server --- guides/source/initialization.textile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 48d4373afe..2699ffccda 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -224,7 +224,18 @@ when 'server' } -This file will change into the root of the directory (a path two directories back 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 requires +action_dispatch+ and sets up the +Rails::Server+ class. +This file will change into the root of the directory (a path two directories back 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. + + +require 'fileutils' +require 'optparse' +require 'action_dispatch' + +module Rails + class Server < ::Rack::Server + + +`fileutils` and `optparse` are standard Ruby libraries which provide helper functions for working with files and parsing options. h4. +actionpack/lib/action_dispatch.rb+ From 661fe76d5e6d61f997ba06ca236c6676d4813edd Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sat, 9 Jun 2012 12:26:17 -0700 Subject: [PATCH 14/34] Add files required by action_dispatch --- guides/source/initialization.textile | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 2699ffccda..8920859675 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -235,11 +235,23 @@ module Rails class Server < ::Rack::Server -`fileutils` and `optparse` are standard Ruby libraries which provide helper functions for working with files and parsing options. ++fileutils+ and +optparse+ are standard Ruby libraries which provide helper functions for working with files and parsing options. h4. +actionpack/lib/action_dispatch.rb+ -Action Dispatch is the routing component of the Rails framework. It depends on Active Support, +actionpack/lib/action_pack.rb+ and +Rack+ being available. The first thing required here is +active_support+. +Action Dispatch is the routing component of the Rails framework. This file begins by requiring several other files: + + +require 'active_support' +require 'active_support/dependencies/autoload' +require 'active_support/core_ext/module/attribute_accessors' + +require 'action_pack' +require 'active_model' +require 'rack' + + +The first thing required here is +active_support+. h4. +activesupport/lib/active_support.rb+ From 071170871c0cd973531eb55e5c5ee9560c7f7301 Mon Sep 17 00:00:00 2001 From: Tobi Lehman Date: Fri, 8 Jun 2012 17:38:09 -0700 Subject: [PATCH 15/34] Fix typo 'ActiveSpport'. --- guides/source/initialization.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 8920859675..9e97cb4194 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -281,7 +281,7 @@ The +active_support/lib/active_support.rb+ file simply defines the +ActiveSuppor h4. +actionpack/lib/action_dispatch.rb+ cont'd. -Now back to +action_pack/lib/action_dispatch.rb+. The next +require+ in this file is one for +action_pack+, which simply calls +action_pack/version.rb+ which defines +ActionPack::VERSION+ and the constants, much like +ActiveSpport+ does. +Now back to +action_pack/lib/action_dispatch.rb+. The next +require+ in this file is one for +action_pack+, which simply calls +action_pack/version.rb+ which defines +ActionPack::VERSION+ and the constants, much like +ActiveSupport+ does. After this line, there's a require to +active_model+ which simply defines autoloads for the +ActiveModel+ part of Rails and sets up the +ActiveModel+ module which is used later on. From fb5d92809f0be64f20b72baeda9bf595714598c1 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sun, 10 Jun 2012 17:55:12 -0700 Subject: [PATCH 16/34] Change initialization guide introduction We will no longer cover every single file that is loaded by Rails, as this would be too difficult and time consuming to maintain. We also expect the reader to know about rails server at this point. --- guides/source/initialization.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 9e97cb4194..1cf49adc0c 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -8,14 +8,14 @@ as of Rails 4. It is an extremely in-depth guide and recommended for advanced Ra endprologue. -This guide goes through every single file, class and method call that is +This guide goes through every method call that is required to boot up the Ruby on Rails stack for a default Rails 4 application, explaining each part in detail along the way. For this guide, we will be focusing on how the two most common methods (+rails server+ and Passenger) boot a Rails application. NOTE: Paths in this guide are relative to Rails or a Rails application unless otherwise specified. h3. Launch! -As of Rails 3, +script/server+ has become +rails server+. This was done to centralize all rails related commands to one common file. +A Rails application is usually started with the command +rails server+. h4. +bin/rails+ From 028f5b1c337a27096fe2689d62aa9e4933cd6487 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sun, 10 Jun 2012 18:07:04 -0700 Subject: [PATCH 17/34] Refactor Action Dispatch description to be more concise --- guides/source/initialization.textile | 53 +++------------------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 1cf49adc0c..45dd41f67a 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -239,55 +239,12 @@ module Rails h4. +actionpack/lib/action_dispatch.rb+ -Action Dispatch is the routing component of the Rails framework. This file begins by requiring several other files: +Action Dispatch is the routing component of the Rails framework. Other +than the rouing itself, it adds +functionalities like routing, session, and common middlewares. - -require 'active_support' -require 'active_support/dependencies/autoload' -require 'active_support/core_ext/module/attribute_accessors' - -require 'action_pack' -require 'active_model' -require 'rack' - - -The first thing required here is +active_support+. - -h4. +activesupport/lib/active_support.rb+ - -This file begins with requiring +active_support/lib/active_support/dependencies/autoload.rb+ which redefines Ruby's +autoload+ method to have a little more extra behaviour especially in regards to eager autoloading. Eager autoloading is the loading of all required classes and will happen when the +config.cache_classes+ setting is +true+. The required file also requires another file: +active_support/lazy_load_hooks+ - -h4. +activesupport/lib/active_support/lazy_load_hooks.rb+ - -This file defines the +ActiveSupport.on_load+ hook which is used to execute code when specific parts are loaded. We'll see this in use a little later on. - -This file begins with requiring +active_support/inflector/methods+. - -h4. +activesupport/lib/active_support/inflector/methods.rb+ - -The +methods.rb+ file is responsible for defining methods such as +camelize+, +underscore+ and +dasherize+ as well as a slew of others. The "+ActiveSupport::Inflector+ documentation":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html covers them all pretty decently. - -In this file there are a lot of lines such as this inside the +ActiveSupport+ module: - - -autoload :Inflector - - -Due to the overriding of the +autoload+ method, Ruby will know how to look for this file at +activesupport/lib/active_support/inflector.rb+ when the +Inflector+ class is first referenced. - -The +active_support/lib/active_support/version.rb+ that is also required here simply defines an +ActiveSupport::VERSION+ constant which defines a couple of constants inside this module, the main constant of this is +ActiveSupport::VERSION::STRING+ which returns the current version of ActiveSupport. - -The +active_support/lib/active_support.rb+ file simply defines the +ActiveSupport+ module and some autoloads (eager and of the normal variety) for it. - -h4. +actionpack/lib/action_dispatch.rb+ cont'd. - -Now back to +action_pack/lib/action_dispatch.rb+. The next +require+ in this file is one for +action_pack+, which simply calls +action_pack/version.rb+ which defines +ActionPack::VERSION+ and the constants, much like +ActiveSupport+ does. - -After this line, there's a require to +active_model+ which simply defines autoloads for the +ActiveModel+ part of Rails and sets up the +ActiveModel+ module which is used later on. - -The last of the requires is to +rack+, which like the +active_model+ and +active_support+ requires before it, sets up the +Rack+ module as well as the autoloads for constants within it. - -Finally in +action_dispatch.rb+ the +ActionDispatch+ module and *its* autoloads are declared. +Action Dispatch itself is also responsible for loading Active Support, Action +Pack, Active Model, and Rack. h4. +rails/commands/server.rb+ From 50d9781e435e64b56afb414abb1cd6073f3fd9d6 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sun, 10 Jun 2012 18:20:40 -0700 Subject: [PATCH 18/34] Rewrite Rails server initialization section --- guides/source/initialization.textile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 45dd41f67a..319354ea79 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -343,7 +343,10 @@ def parse!(args) ... -This method will set up keys for the +options+ which Rails will then be able to use to determine how its server should run. After +initialize+ has finished, then the +start+ method will launch the server. +This method will set up keys for the +options+ which Rails will then be +able to use to determine how its server should run. After +initialize+ +has finished, we jump back into +rails/server+ where +APP_PATH+ (which was +set earlier) is required. h4. +Rails::Server#start+ From 3c025dec4cb756e84f526f1a1a6bf1370608f78e Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sun, 10 Jun 2012 18:26:54 -0700 Subject: [PATCH 19/34] Add config/application to initialization guide --- guides/source/initialization.textile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 319354ea79..ec9d3c8052 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -348,9 +348,16 @@ able to use to determine how its server should run. After +initialize+ has finished, we jump back into +rails/server+ where +APP_PATH+ (which was set earlier) is required. +h4. +config/application+ + +When +require APP_PATH+ is executed, +config/application.rb+ is loaded. +This is a file exists in your app and it's free for you to change based +on your needs. Among other things, inside this file you load gems with +bundler, and create your application namespace. + h4. +Rails::Server#start+ -This method is defined like this: +After +congif/application+ is loaded, +server.start+ is called. This method is defined like this: def start From 57170af5c33592b92d340a96bc1286cb8e678ae8 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Sun, 10 Jun 2012 18:45:03 -0700 Subject: [PATCH 20/34] Remove initialization explanation for each framework We no longer go through all the files and frameworks that are required by Rails. Instead we will only focus on the code that gets executed as well as important concepts. --- guides/source/initialization.textile | 650 +-------------------------- 1 file changed, 10 insertions(+), 640 deletions(-) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index ec9d3c8052..a43ab9bd58 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -395,7 +395,7 @@ method creates a trap for +INT+ signals, so if you +CTRL-C+ the server, it will exit the process. As we can see from the code here, it will create the +tmp/cache+, +tmp/pids+, +tmp/sessions+ and +tmp/sockets+ directories. It then calls +wrapped_app+ which is responsible for -creating the Rack app, before creating and assignig an +creating the Rack app, before creating and assigning an instance of +ActiveSupport::Logger+. The +super+ method will call +Rack::Server.start+ which begins its definition like this: @@ -445,7 +445,8 @@ end The interesting part for a Rails app is the last line, +server.run+. Here we encounter the +wrapped_app+ method again, which this time -we're going to explore more. +we're going to explore more (even though it was executed before, and +thus memoized by now). @wrapped_app ||= build_app app @@ -484,7 +485,7 @@ app = eval "Rack::Builder.new {( " cfgfile "\n )}.to_app", TOPLEVEL_BINDING, config -The +initialize+ method will take the block here and execute it within an instance of +Rack::Builder+. This is where the majority of the initialization process of Rails happens. The chain of events that this simple line sets off will be the focus of a large majority of this guide. The +require+ line for +config/environment.rb+ in +config.ru+ is the first to run: +The +initialize+ method of +Rack::Builder+ will take the block here and execute it within an instance of +Rack::Builder+. This is where the majority of the initialization process of Rails happens. The +require+ line for +config/environment.rb+ in +config.ru+ is the first to run: require ::File.expand_path('../config/environment', __FILE__) @@ -531,641 +532,10 @@ require "rails" end -First off the line is the +rails+ require itself. +This is where all the Rails frameworks are loaded and thus made +available to the application. We wont go into detail of what happens +inside each of those frameworks, but you're encouraged to try and +explore them on your own. -h4. +railties/lib/rails.rb+ - -This file is responsible for the initial definition of the +Rails+ -module and, rather than defining the autoloads like +ActiveSupport+, -+ActionDispatch+ and so on, it actually defines other functionality. -Such as the +root+, +env+ and +application+ methods which are extremely -useful in Rails 4 applications. - -However, before all that takes place the +rails/ruby_version_check+ file is required first. - -h4. +railties/lib/rails/ruby_version_check.rb+ - -This file simply checks if the Ruby version is less than 1.9.3 and -raises an error if that is the case. Rails 4 simply will not run on -earlier versions of Ruby. - -NOTE: You should always endeavor to run the latest version of Ruby with your Rails applications. The benefits are many, including security fixes and the like, and very often there is a speed increase associated with it. The caveat is that you could have code that potentially breaks on the latest version, which should be fixed to work on the latest version rather than kept around as an excuse not to upgrade. - -h4. +active_support/core_ext/kernel/reporting.rb+ - -This is the first of the many Active Support core extensions that come with Rails. This one in particular defines methods in the +Kernel+ module which is mixed in to the +Object+ class so the methods are available on +main+ and can therefore be called like this: - - -silence_warnings do - # some code -end - - -These methods can be used to silence STDERR responses and the +silence_stream+ allows you to also silence other streams. Additionally, this mixin allows you to suppress exceptions and capture streams. For more information see the "Silencing Warnings, Streams, and Exceptions":active_support_core_extensions.html#silencing-warnings-streams-and-exceptions section from the Active Support Core Extensions Guide. - -h4. +active_support/core_ext/array/extract_options.rb+ - -The next file that is required is another Active Support core extension, -this time to the +Array+ and +Hash+ classes. This file defines an -+extract_options!+ method which Rails uses to extract options from -parameters. - -h4. +railties/lib/rails/application.rb+ - -The next file required by +railties/lib/rails.rb+ is +application.rb+. -This file defines the +Rails::Application+ constant which the -application's class defined in +config/application.rb+ in a standard -Rails application depends on. - -Before the +Rails::Application+ class is -defined however, +rails/engine+ is also loaded, which is responsible for -handling the behavior and definitions of Rails engines. - -TIP: You can read more about engines in the "Getting Started with Engines":engines.html guide. - -Among other things, Rails Engine is also responsible for loading the -Railtie class. - -h4. +railties/lib/rails/railtie.rb+ - -The +rails/railtie.rb+ file is responsible for defining +Rails::Railtie+, the underlying class for all ties to Rails now. Gems that want to have their own initializers or rake tasks and hook into Rails should have a +GemName::Railtie+ class that inherits from +Rails::Railtie+. - -The "API documentation":http://api.rubyonrails.org/classes/Rails/Railtie.html for +Rails::Railtie+, much like +Rails::Engine+, explains this class exceptionally well. - -The first require in this file is +rails/initializable.rb+. - -h4. +railties/lib/rails/initializable.rb+ - -Now we reach the end of this particular rabbit hole as +rails/initializable.rb+ doesn't require any more Rails files, only +tsort+ from the Ruby standard library. - -This file defines the +Rails::Initializable+ module which contains the +Initializer+ class, the basis for all initializers in Rails. This module also contains a +ClassMethods+ class which will be included into the +Rails::Railtie+ class when these requires have finished. - -Now that +rails/initializable.rb+ has finished being required from +rails/railtie.rb+, the next require is for +rails/configuration+. - -h4. +railties/lib/rails/configuration.rb+ - -This file defines the +Rails::Configuration+ module, containing the +MiddlewareStackProxy+ class as well as the +Generators+ class. The +MiddlewareStackProxy+ class is used for managing the middleware stack for an application, which we'll see later on. The +Generators+ class provides the functionality used for configuring what generators an application uses through the "+config.generators+ option":configuring.html#configuring-generators. - -The first file required in this file is +activesupport/deprecation+. - -h4. +activesupport/lib/active_support/deprecation.rb+ - -This file, and the files it requires, define the basic deprecation warning features found in Rails. This file is responsible for setting defaults in the +ActiveSupport::Deprecation+ module for the +deprecation_horizon+, +silenced+ and +debug+ values. The files that are required before this happens are: - -* +active_support/deprecation/behaviors+ -* +active_support/deprecation/reporting+ -* +active_support/deprecation/method_wrappers+ -* +active_support/deprecation/proxy_wrappers+ - -h4. +activesupport/lib/active_support/deprecation/behaviors.rb+ - -This file defines the behavior of the +ActiveSupport::Deprecation+ module, setting up the +DEFAULT_BEHAVIORS+ hash constant which contains the three defaults to outputting deprecation warnings: +:stderr+, +:log+ and +:notify+. This file begins by requiring +activesupport/notifications+ and +activesupport/core_ext/array/wrap+. - -h4. +activesupport/lib/active_support/notifications.rb+ - -This file defines the +ActiveSupport::Notifications+ module. Notifications provides an instrumentation API for Ruby, shipping with a queue implementation that consumes and publish events to log subscribers in a thread. - -The "API documentation":http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html for +ActiveSupport::Notifications+ explains the usage of this module, including the methods that it defines. - -The file required in +active_support/notifications.rb+ is +active_support/core_ext/module/delegation+ which is documented in the "Active Support Core Extensions Guide":active_support_core_extensions.html#method-delegation. - -h4. +activesupport/core_ext/array/wrap+ - -As this file comprises of a core extension, it is covered exclusively in "the Active Support Core Extensions guide":active_support_core_extensions.html#wrapping - -h4. +activesupport/lib/active_support/deprecation/reporting.rb+ - -This file is responsible for defining the +warn+ and +silence+ methods for +ActiveSupport::Deprecation+ as well as additional private methods for this module. - -h4. +activesupport/lib/active_support/deprecation/method_wrappers.rb+ - -This file defines a +deprecate_methods+ which is primarily used by the +module/deprecation+ core extension required by the first line of this file. Other core extensions required by this file are the +module/aliasing+ and +array/extract_options+ files. - -h4. +activesupport/lib/active_support/deprecation/proxy_wrappers.rb+ - -+proxy_wrappers.rb+ defines deprecation wrappers for methods, instance variables and constants. Previously, this was used for the +RAILS_ENV+ and +RAILS_ROOT+ constants for 3.0 but since then these constants have been removed. The deprecation message that would be raised from these would be something like: - - -BadConstant is deprecated! Use GoodConstant instead. - - -h4. +active_support/ordered_options+ - -This file is the next file required from +rails/configuration.rb+ is the file that defines +ActiveSupport::OrderedOptions+ which is used for configuration options such as +config.active_support+ and the like. - -The next file required is +active_support/core_ext/hash/deep_dup+ which is covered in "Active Support Core Extensions guide":active_support_core_extensions.html#deep_dup - -h4. +active_support/core_ext/object+ - -This file is responsible for requiring many more Active Support core extensions: - - -require 'active_support/core_ext/object/acts_like' -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/duplicable' -require 'active_support/core_ext/object/deep_dup' -require 'active_support/core_ext/object/try' -require 'active_support/core_ext/object/inclusion' - -require 'active_support/core_ext/object/conversions' -require 'active_support/core_ext/object/instance_variables' - -require 'active_support/core_ext/object/to_json' -require 'active_support/core_ext/object/to_param' -require 'active_support/core_ext/object/to_query' -require 'active_support/core_ext/object/with_options' - - -The Rails API documentation covers them in great detail, so we're not going to explain each of them. - -The file that is required next from +rails/configuration+ is +rails/paths+. - -h4. +railties/lib/rails/paths.rb+ - -This file defines the +Rails::Paths+ module which allows paths to be configured for a Rails application or engine. Later on in this guide when we cover Rails configuration during the initialization process we'll see this used to set up some default paths for Rails and some of them will be configured to be eager loaded. - -h4. +railties/lib/rails/rack.rb+ - -The final file to be loaded by +railties/lib/rails/configuration.rb+ is +rails/rack+ which defines some simple autoloads: - - -module Rails - module Rack - autoload :Debugger, "rails/rack/debugger" - autoload :Logger, "rails/rack/logger" - autoload :LogTailer, "rails/rack/log_tailer" - end -end - - -Once this file is finished loading, then the +Rails::Configuration+ class is initialized. This completes the loading of +railties/lib/rails/configuration.rb+ and now we jump back to the loading of +railties/lib/rails/railtie.rb+, where the next file loaded is +active_support/inflector+. - -h4. +activesupport/lib/active_support/inflector.rb+ - -+active_support/inflector.rb+ requires a series of file which are responsible for setting up the basics for knowing how to pluralize and singularize words. These files are: - - -require 'active_support/inflector/inflections' -require 'active_support/inflector/transliterate' -require 'active_support/inflector/methods' - -require 'active_support/inflections' -require 'active_support/core_ext/string/inflections' - - -The +active_support/inflector/methods+ file has already been required by +active_support/autoload+ and so won't be loaded again here. The +activesupport/lib/active_support/inflector/inflections.rb+ is required by +active_support/inflector/methods+. - -h4. +active_support/inflections+ - -This file references the +ActiveSupport::Inflector+ constant which isn't loaded by this point. But there were autoloads set up in +activesupport/lib/active_support.rb+ which will load the file which loads this constant and so then it will be defined. Then this file defines pluralization and singularization rules for words in Rails. This is how Rails knows how to pluralize "tomato" to "tomatoes". - - -inflect.irregular('zombie', 'zombies') - - -h4. +activesupport/lib/active_support/inflector/transliterate.rb+ - -This is the file that defines the "+transliterate+":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate and "+parameterize+":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize methods. - -h4. +active_support/core_ext/module/introspection+ - -The next file loaded by +rails/railtie+ is the introspection core -extension, which extends +Module+ with methods like +parent_name+, +parent+ and -+parents+. - -h4. +active_support/core_ext/module/delegation+ - -The final file loaded by +rails/railtie+ is the delegation core extension, which defines the "+delegate+":http://api.rubyonrails.org/classes/Module.html#method-i-delegate method. - -h4. Back to +railties/lib/rails/railtie.rb+ - -Once the inflector files have been loaded, the +Rails::Railtie+ class is defined. This class includes a module called +Initializable+, which is actually +Rails::Initializable+. This module includes the +initializer+ method which is used later on for setting up initializers, amongst other methods. - -h4. +railties/lib/rails/initializable.rb+ - -When the module from this file (+Rails::Initializable+) is included, it extends the class it's included into with the +ClassMethods+ module inside of it. This module defines the +initializer+ method which is used to define initializers throughout all of the railties. This file completes the loading of +railties/lib/rails/railtie.rb+. Now we go back to +rails/engine.rb+. - -h4. +railties/lib/rails/engine.rb+ - -The next file required in +rails/engine.rb+ is +active_support/core_ext/module/delegation+ which is documented in the "Active Support Core Extensions Guide":active_support_core_extensions.html#method-delegation. - -The next two files after this are Ruby standard library files: +pathname+ and +rbconfig+. The file after these is +rails/engine/railties+. - -h4. +railties/lib/rails/engine/railties.rb+ - -This file defines the +Rails::Engine::Railties+ class which provides the +engines+ and +railties+ methods which are used later on for defining rake tasks and other functionality for engines and railties. - -h4. Back to +railties/lib/rails/engine.rb+ - -Once +rails/engine/railties.rb+ has finished loading the +Rails::Engine+ class gets its basic functionality defined, such as the +inherited+ method which will be called when this class is inherited from. - -Once this file has finished loading we jump back to +railties/lib/rails/plugin.rb+ - -h4. Back to +railties/lib/rails/plugin.rb+ - -The next file required in this is a core extension from Active Support called +array/conversions+ which is covered in "this section":active_support_core_extensions.html#array-conversions of the Active Support Core Extensions Guide. - -Once that file has finished loading, the +Rails::Plugin+ class is defined. - -h4. Back to +railties/lib/rails/application.rb+ - -Jumping back to +rails/application.rb+ now. This file defines the +Rails::Application+ class where the application's class inherits from. This class (and its superclasses) define the basic behaviour on the application's constant such as the +config+ method used for configuring the application. - -Once this file's done then we go back to the +railties/lib/rails.rb+ file, which next requires +rails/version+. - -h4. +railties/lib/rails/version.rb+ - -Much like +active_support/version+, this file defines the +VERSION+ constant which has a +STRING+ constant on it which returns the current version of Rails. - -Once this file has finished loading we go back to +railties/lib/rails.rb+ which then requires +active_support/railtie.rb+. - -h4. +activesupport/lib/active_support/railtie.rb+ - -This file requires +active_support+ and +rails+ which have already been required so these two lines are effectively ignored. The third require in this file is to +active_support/i18n_railtie.rb+. - -h4. +activesupport/lib/active_support/i18n_railtie.rb+ - -This file is the first file that sets up configuration with these lines inside the class: - - -class Railtie < Rails::Railtie - config.i18n = ActiveSupport::OrderedOptions.new - config.i18n.railties_load_path = [] - config.i18n.load_path = [] - config.i18n.fallbacks = ActiveSupport::OrderedOptions.new - - -By inheriting from +Rails::Railtie+ the +Rails::Railtie#inherited+ method is called: - - -def inherited(base) - unless base.abstract_railtie? - base.send(:include, Railtie::Configurable) - subclasses << base - end -end - - -This first checks if the Railtie that's inheriting it is a component of Rails itself: - - -ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Plugin Rails::Engine Rails::Application) - -... - -def abstract_railtie? - ABSTRACT_RAILTIES.include?(name) -end - - -Because +I18n::Railtie+ isn't in this list, +abstract_railtie?+ returns +false+. Therefore the +Railtie::Configurable+ module is included into this class and the +subclasses+ method is called and +I18n::Railtie+ is added to this new array. - - -def subclasses - @subclasses ||= [] -end - - -The +config+ method used at the top of +I18n::Railtie+ is defined on +Rails::Railtie+ and is defined like this: - - -def config - @config ||= Railtie::Configuration.new -end - - -At this point, that +Railtie::Configuration+ constant is automatically loaded which causes the +rails/railties/configuration+ file to be loaded. The line for this is this particular line in +railties/lib/rails/railtie.rb+: - - -autoload :Configuration, "rails/railtie/configuration" - - -h4. +railties/lib/rails/railtie/configuration.rb+ - -This file begins with a require out to +rails/configuration+ which has already been required earlier in the process and so isn't required again. - -This file defines the +Rails::Railtie::Configuration+ class which is responsible for providing a way to easily configure railties and it's the +initialize+ method here which is called by the +config+ method back in the +i18n_railtie.rb+ file. The methods on this object don't exist, and so are rescued by the +method_missing+ defined further down in +configuration.rb+: - - -def method_missing(name, *args, &blk) - if name.to_s =~ /=$/ - @@options[$`.to_sym] = args.first - elsif @@options.key?(name) - @@options[name] - else - super - end -end - - -So therefore when an option is referred to it simply stores the value as the key if it's used in a setter context, or retrieves it if used in a getter context. Nothing fancy going on there. - -h4. Back to +activesupport/lib/active_support/i18n_railtie.rb+ - -After the configuration method the +reloader+ method is defined, and then the first of of Railties' initializers is defined: +i18n.callbacks+. - - -initializer "i18n.callbacks" do - ActionDispatch::Reloader.to_prepare do - I18n::Railtie.reloader.execute_if_updated - end -end - - -The +initializer+ method (from the +Rails::Initializable+ module) here doesn't run the block, but rather stores it to be run later on: - - -def initializer(name, opts = {}, &blk) - raise ArgumentError, "A block must be passed when defining an initializer" unless blk - opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] } - initializers << Initializer.new(name, nil, opts, &blk) -end - - -An initializer can be configured to run before or after another initializer, which we'll see a couple of times throughout this initialization process. Anything that inherits from +Rails::Railtie+ may also make use of the +initializer+ method, something which is covered in the "Configuration guide":configuring.html#rails-railtie-initializer. - -The +Initializer+ class here is defined within the +Rails::Initializable+ module and its +initialize+ method is defined to just set up a couple of variables: - - -def initialize(name, context, options, &block) - @name, @context, @options, @block = name, context, options, block -end - - -Once this +initialize+ method is finished, the object is added to the object the +initializers+ method returns: - - -def initializers - @initializers ||= self.class.initializers_for(self) -end - - -If +@initializers+ isn't set (which it won't be at this point), the +intializers_for+ method will be called for this class. - - -def initializers_for(binding) - Collection.new(initializers_chain.map { |i| i.bind(binding) }) -end - - -The +Collection+ class in +railties/lib/rails/initializable.rb+ inherits from +Array+ and includes the +TSort+ module which is used to sort out the order of the initializers based on the order they are placed in. - -The +initializers_chain+ method referenced in the +initializers_for+ method is defined like this: - - -def initializers_chain - initializers = Collection.new - ancestors.reverse_each do |klass| - next unless klass.respond_to?(:initializers) - initializers = initializers + klass.initializers - end - initializers -end - - -This method collects the initializers from the ancestors of this class and adds them to a new +Collection+ object using the + method which is defined like this for the Collection class: - - -def +(other) - Collection.new(to_a + other.to_a) -end - - -So this + method is overridden to return a new collection comprising of the existing collection as an array and then using the Array#+ method combines these two collections, returning a "super" +Collection+ object. In this case, the only initializer that's going to be in this new +Collection+ object is the +i18n.callbacks+ initializer. - -The next method to be called after this +initializer+ method is the +after_initialize+ method on the +config+ object, which is defined like this: - - -def after_initialize(&block) - ActiveSupport.on_load(:after_initialize, :yield => true, &block) -end - - -The +on_load+ method here is provided by the +active_support/lazy_load_hooks+ file which was required earlier and is defined like this: - - -def self.on_load(name, options = {}, &block) - if base = @loaded[name] - execute_hook(base, options, block) - else - @load_hooks[name] << [block, options] - end -end - - -The +@loaded+ variable here is a hash containing elements representing the different components of Rails that have been loaded at this stage. Currently, this hash is empty. So the +else+ is executed here, using the +@load_hooks+ variable defined in +active_support/lazy_load_hooks+: - - -@load_hooks = Hash.new {|h,k| h[k] = [] } - - -This defines a new hash which has keys that default to empty arrays. This saves Rails from having to do something like this instead: - - -@load_hooks[name] = [] -@load_hooks[name] << [block, options] - - -The value added to this array here consists of the block and options passed to +after_initialize+. - -We'll see these +@load_hooks+ used later on in the initialization process. - -This rest of +i18n_railtie.rb+ defines the protected class methods +include_fallback_modules+, +init_fallbacks+ and +validate_fallbacks+. - -h4. Back to +activesupport/lib/active_support/railtie.rb+ - -This file defines the +ActiveSupport::Railtie+ constant which like the +I18n::Railtie+ constant just defined, inherits from +Rails::Railtie+ meaning the +inherited+ method would be called again here, including +Rails::Configurable+ into this class. This class makes use of +Rails::Railtie+'s +config+ method again, setting up the configuration options for Active Support. - -Then this Railtie sets up three more initializers: - -* +active_support.deprecation_behavior+ -* +active_support.initialize_time_zone+ -* +active_support.set_configs+ - -We will cover what each of these initializers do when they run. - -Once the +active_support/railtie+ file has finished loading the next file required from +railties/lib/rails.rb+ is the +action_dispatch/railtie+. - -h4. +actionpack/lib/action_dispatch/railtie.rb+ - -This file defines the +ActionDispatch::Railtie+ class, but not before requiring +action_dispatch+. - -h4. +actionpack/lib/action_dispatch.rb+ - -This file starts off with the following requires: - - -require 'active_support' -require 'active_support/dependencies/autoload' -require 'active_support/core_ext/module/attribute_accessors' - - -The following require is to +action_pack+ (+actionpack/lib/action_pack.rb+) which contains a simple require to +action_pack/version+. This file, like other +version.rb+ files before it, defines the +ActionPack::VERSION+ constant: - - -module ActionPack - module VERSION #:nodoc: - MAJOR = 4 - MINOR = 0 - TINY = 0 - PRE = "beta" - - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') - end -end - - -Once +action_pack+ is finished, then +active_model+ is required. - -h4. +activemodel/lib/active_model.rb+ - -This file makes a require to +active_model/version+ which defines the version for Active Model: - - -module ActiveModel - module VERSION #:nodoc: - MAJOR = 4 - MINOR = 0 - TINY = 0 - PRE = "beta" - - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') - end -end - - -Once the +version.rb+ file is loaded, the +ActiveModel+ module has its autoloaded constants defined as well as a sub-module called +ActiveModel::Serializers+ which has autoloads of its own. When the +ActiveModel+ module is closed the +active_support/i18n+ file is required. - -h4. +activesupport/lib/active_support/i18n.rb+ - -This is where the +i18n+ gem is required and first configured: - - -begin - require 'i18n' - require 'active_support/lazy_load_hooks' -rescue LoadError => e - $stderr.puts "You don't have i18n installed in your application. Please add it to your Gemfile and run bundle install" - raise e -end - -I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml" - - -In effect, the +I18n+ module first defined by +i18n_railtie+ is extended by the +i18n+ gem, rather than the other way around. This has no ill effect. They both work on the same way. - -This is another spot where +active_support/lazy_load_hooks+ is required, but it has already been required so it's not loaded again. - -If +i18n+ cannot be loaded, the user is presented with an error which says that it cannot be loaded and recommends that it's added to the +Gemfile+. However, in a normal Rails application this gem would be loaded. - -Once it has finished loading, the +I18n.load_path+ method is used to add the +activesupport/lib/active_support/locale/en.yml+ file to I18n's load path. When the translations are loaded in the initialization process, this is one of the files where they will be sourced from. - -The loading of this file finishes the loading of +active_model+ and so we go back to +action_dispatch+. - -h4. Back to +actionpack/lib/action_dispatch.rb+ - -The remainder of this file requires the +rack+ file from the Rack gem which defines the +Rack+ module. After +rack+, there's autoloads defined for the +Rack+, +ActionDispatch+, +ActionDispatch::Http+, +ActionDispatch::Session+. A new method called +autoload_under+ is used here, and this simply prefixes the files where the modules are autoloaded from with the path specified. For example here: - - -autoload_under 'testing' do - autoload :Assertions -... - - -The +Assertions+ module is in the +action_dispatch/testing+ folder rather than simply +action_dispatch+. - -Finally, this file defines a top-level autoload, the +Mime+ constant. - -h4. Back to +actionpack/lib/action_dispatch/railtie.rb+ - -After +action_dispatch+ is required in this file, the +ActionDispatch::Railtie+ class is defined and is yet another class that inherits from +Rails::Railtie+. This class defines some initial configuration option defaults for +config.action_dispatch+ before setting up a single initializer called +action_dispatch.configure+. - -With +action_dispatch/railtie+ now complete, we go back to +railties/lib/rails.rb+. - -h4. Back to +railties/lib/rails.rb+ - -With the Active Support and Action Dispatch railties now both loaded, the rest of this file deals with setting up UTF-8 to be the default encoding for Rails and then finally setting up the +Rails+ module. This module defines useful methods such as +Rails.logger+, +Rails.application+, +Rails.env+, and +Rails.root+. - -h4. Back to +railties/lib/rails/all.rb+ - -Now that +rails.rb+ is required, the remaining railties are loaded next, beginning with +active_record/railtie+. - -h4. +activerecord/lib/active_record/railtie.rb+ - -Before this file gets into the swing of defining the +ActiveRecord::Railtie+ class, there are a couple of files that are required first. The first one of these is +active_record+. - -h4. +activerecord/lib/active_record.rb+ - -This file begins by detecting if the +lib+ directories of +active_support+ and +active_model+ are not in the load path and if they aren't then adds them. As we saw back in +action_dispatch.rb+, these directories are already there. - -The first couple of requires have already been done by other files and so aren't loaded here, but the next one to +arel+ will require the file provided by the Arel gem, which defines the +Arel+ module. - - -require 'active_support' -require 'active_model' -require 'arel' - - -The file required next is +active_record/version+ which defines the +ActiveRecord::VERSION+ constant: - - -module ActiveRecord - module VERSION #:nodoc: - MAJOR = 4 - MINOR = 0 - TINY = 0 - PRE = "beta" - - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') - end -end - - -Once these requires are finished, the base for the +ActiveRecord+ module is defined along with its autoloads. - -Near the end of the file, we see this line: - - -ActiveSupport.on_load(:active_record) do - Arel::Table.engine = self -end - - -This will set the engine for +Arel::Table+ to be +ActiveRecord::Base+. - -The file then finishes with this line: - - -ActiveSupport.on_load(:i18n) do - I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' -end - - -This will add the translations from +activerecord/lib/active_record/locale/en.yml+ to the load path for +I18n+, with this file being parsed when all the translations are loaded. - -h4. Back to +activerecord/lib/active_record/railtie.rb+ - -The next two requires in this file aren't run because their files are already required, with +rails+ being required by +rails/all+ and +active_model/railtie+ being required from +action_dispatch+. - - -require "rails" -require "active_model/railtie" - - -The next +require+ in this file is to +action_controller/railtie+. - -h4. +actionpack/lib/action_controller/railtie.rb+ - -This file begins with a couple more requires to files that have already been loaded: - - -require "rails" -require "action_controller" -require "action_dispatch/railtie" - - -However the require after these is to a file that hasn't yet been loaded, +action_view/railtie+, which begins by requiring +action_view+. - -h4. +actionpack/lib/action_view.rb+ - -+action_view.rb+ +For now, just keep in mind that common functionality like Rails engines, +I18n and Rails configuration is all bein defined here. From a6e4c0f3e4d835c84ba2b504648181e0670cff40 Mon Sep 17 00:00:00 2001 From: chrismcc Date: Mon, 11 Jun 2012 09:54:54 -0300 Subject: [PATCH 21/34] Fixed typo. --- activerecord/lib/active_record/store.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb index d70e02e379..542cb3187a 100644 --- a/activerecord/lib/active_record/store.rb +++ b/activerecord/lib/active_record/store.rb @@ -2,7 +2,7 @@ require 'active_support/core_ext/hash/indifferent_access' module ActiveRecord # Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column. - # It's like a simple key/value store backed into your record when you don't care about being able to + # It's like a simple key/value store baked into your record when you don't care about being able to # query that store outside the context of a single record. # # You can then declare accessors to this store that are then accessible just like any other attribute From 588bb6b987bd3f10b97ce27263e8d7702605a187 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 12 Jun 2012 02:29:21 +0900 Subject: [PATCH 22/34] Range#cover? is not implemented in AS now --- activemodel/lib/active_model/validations/exclusion.rb | 3 +-- activemodel/lib/active_model/validations/inclusion.rb | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 4f09679541..edd42d85f2 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -30,8 +30,7 @@ module ActiveModel # * :in - An enumerable object of items that the value shouldn't be # part of. This can be supplied as a proc or lambda which returns an # enumerable. If the enumerable is a range the test is performed with - # Range#cover? (backported in Active Support for 1.8), otherwise - # with include?. + # Range#cover?, otherwise with include?. # * :message - Specifies a custom error message (default is: "is # reserved"). # * :allow_nil - If set to true, skips this validation if the attribute diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index ffdbed0fc1..8810f2a3c1 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -28,8 +28,8 @@ module ActiveModel # Configuration options: # * :in - An enumerable object of available items. This can be # supplied as a proc or lambda which returns an enumerable. If the enumerable - # is a range the test is performed with Range#cover? - # (backported in Active Support for 1.8), otherwise with include?. + # is a range the test is performed with Range#cover?, otherwise with + # include?. # * :message - Specifies a custom error message (default is: "is not # included in the list"). # * :allow_nil - If set to true, skips this validation if the attribute From 07d346646f61dbf0ef020eefc4dc11e140b55baf Mon Sep 17 00:00:00 2001 From: Sam Oliver Date: Mon, 11 Jun 2012 23:29:59 +0100 Subject: [PATCH 23/34] Fix cache_store configuration example --- guides/source/caching_with_rails.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.textile index 34a100cd3a..3ee36ae971 100644 --- a/guides/source/caching_with_rails.textile +++ b/guides/source/caching_with_rails.textile @@ -332,7 +332,7 @@ h4. ActiveSupport::Cache::MemoryStore This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the +:size+ options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed. -config.cache_store = :memory_store, :size => 64.megabytes +config.cache_store = :memory_store, { :size => 64.megabytes } If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then your Rails server process instances won't be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments. From 595b8bc8f87d4400b1f83882bf079cacd265a9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ku=C5=BAma?= Date: Tue, 12 Jun 2012 13:00:17 +0200 Subject: [PATCH 24/34] fixed http token authentication formatting --- .../metal/http_authentication.rb | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 57bb0e2a32..76f380e583 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -401,16 +401,21 @@ module ActionController end end - # If token Authorization header is present, call the login procedure with - # the present token and options. + # If token Authorization header is present, call the login + # procedure with the present token and options. # - # controller - ActionController::Base instance for the current request. - # login_procedure - Proc to call if a token is present. The Proc should - # take 2 arguments: - # authenticate(controller) { |token, options| ... } + # [controller] + # ActionController::Base instance for the current request. # - # Returns the return value of `&login_procedure` if a token is found. - # Returns nil if no token is found. + # [login_procedure] + # Proc to call if a token is present. The Proc should take 2 + # arguments: + # + # authenticate(controller) { |token, options| ... } + # + # Returns the return value of login_procedure if a + # token is found. Returns nil if no token is found. + def authenticate(controller, &login_procedure) token, options = token_and_options(controller.request) unless token.blank? From 5dd4358f1d1ef6658193065166fa64cb384a0bb8 Mon Sep 17 00:00:00 2001 From: Andrew Hooker Date: Tue, 12 Jun 2012 13:55:00 -0300 Subject: [PATCH 25/34] Adding note about cosmetic changes not being accepted --- guides/source/contributing_to_ruby_on_rails.textile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index eb049aa357..128a968f69 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -345,6 +345,10 @@ You should not be the only person who looks at the code before you submit it. Yo You might want also to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help you check your code when you're writing your first patches. +h4. Cosmetic Changes + +Changes that are cosmetic in nature and do not improve the stability, functionality, or testability of rails will generally not be accepted. + h4. Commit Your Changes When you're happy with the code on your computer, you need to commit the changes to git: From 81fd371f138b92f4783ecd42ff0d656715ed118d Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 13 Jun 2012 08:34:48 -0700 Subject: [PATCH 26/34] Close explanation of config/environment --- guides/source/initialization.textile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index a43ab9bd58..7d78f0a94c 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -539,3 +539,11 @@ explore them on your own. For now, just keep in mind that common functionality like Rails engines, I18n and Rails configuration is all bein defined here. + +h4. Back to +config/environment.rb+ + +When +config/application.rb+ has finished loading Rails, and defined +your application namespace, you go back to +config/environment.rb+, +where your application is initialized. For example, if you application was called ++Blog+, here you would find +Blog::Application.initialize!+, which is +defined in +rails/application.rb+ From 1f07ff97ab416ba54b4cecde2be1a22341dfa09e Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 13 Jun 2012 08:48:45 -0700 Subject: [PATCH 27/34] Add initialize! explanation --- guides/source/initialization.textile | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 7d78f0a94c..bf901508c8 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -547,3 +547,28 @@ your application namespace, you go back to +config/environment.rb+, where your application is initialized. For example, if you application was called +Blog+, here you would find +Blog::Application.initialize!+, which is defined in +rails/application.rb+ + +h4. +railties/lib/rails/application.rb+ + +The +initialize!+ method looks like this: + + +def initialize!(group=:default) #:nodoc: + raise "Application has been already initialized." if @initialized + run_initializers(group, self) + @initialized = true + self +end + + +As you can see, you can only initialize an app once. This is also where the initializers are run. + +TODO: review this + +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. + +After this is done we go back to +Rack::Server+ + From 18faa5b3375daa2b090a360e4a24649f9a4e03d2 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 13 Jun 2012 08:59:14 -0700 Subject: [PATCH 28/34] Show when Rack middlewares are executed --- guides/source/initialization.textile | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index bf901508c8..1f94dc1419 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -572,3 +572,43 @@ all the engines available by providing the +initializers+ method. After this is done we go back to +Rack::Server+ +h4. Rack: lib/rack/server.rb + +Last time we left when the +app+ method was being defined: + + +def app + @app ||= begin + if !::File.exist? options[:config] + abort "configuration #{options[:config]} not found" + end + + app, options = Rack::Builder.parse_file(self.options[:config], opt_parser) + self.options.merge! options + app + end +end + + +At this point +app+ is the Rails app itself (a middleware), and what +happens next is Rack will call all the provided middlewares: + + +def build_app(app) + middleware[options[:environment]].reverse_each do |middleware| + middleware = middleware.call(self) if middleware.respond_to?(:call) + next unless middleware + klass = middleware.shift + app = klass.new(app, *middleware) + end + app +end + + +Remember, +build_app+ was called (by wrapped_app) in the last line of +Server#start+. +Here's how it looked like when we left: + + +server.run wrapped_app, options, &blk + + From f48c576fc85ab947e91ec93523951de4a2ff21bf Mon Sep 17 00:00:00 2001 From: Nathan Long Date: Wed, 13 Jun 2012 15:49:31 -0400 Subject: [PATCH 29/34] Show how to add attributes to options --- guides/source/form_helpers.textile | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/guides/source/form_helpers.textile b/guides/source/form_helpers.textile index 8106de6f9d..1851aceff8 100644 --- a/guides/source/form_helpers.textile +++ b/guides/source/form_helpers.textile @@ -419,6 +419,18 @@ TIP: The second argument to +options_for_select+ must be exactly equal to the de WARNING: when +:inlude_blank+ or +:prompt:+ are not present, +:include_blank+ is forced true if the select attribute +required+ is true, display +size+ is one and +multiple+ is not true. +You can add arbitrary attributes to the options using hashes: + + +<%= options_for_select([['Lisbon', 1, :'data-size' => '2.8 million'], ['Madrid', 2, :'data-size' => '3.2 million']], 2) %> + +output: + + + +... + + h4. Select Boxes for Dealing with Models In most cases form controls will be tied to a specific database model and as you might expect Rails provides helpers tailored for that purpose. Consistent with other form helpers, when dealing with models you drop the +_tag+ suffix from +select_tag+: From 32664e746377113174cfe24ca3627e983c4b2972 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Thu, 14 Jun 2012 09:05:10 -0700 Subject: [PATCH 30/34] Add server.run example This is the last chapter before I go back and make sure the guide is complete/review --- guides/source/initialization.textile | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile index 1f94dc1419..0638bbed10 100644 --- a/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -612,3 +612,48 @@ Here's how it looked like when we left: server.run wrapped_app, options, &blk +At this point, the implementation of +server.run+ will depend on the +server you're using. For example, if you were using Mongrel, here's what +the +run+ method would look like: + + +def self.run(app, options={}) + server = ::Mongrel::HttpServer.new( + options[:Host] || '0.0.0.0', + options[:Port] || 8080, + options[:num_processors] || 950, + options[:throttle] || 0, + options[:timeout] || 60) + # Acts like Rack::URLMap, utilizing Mongrel's own path finding methods. + # Use is similar to #run, replacing the app argument with a hash of + # { path=>app, ... } or an instance of Rack::URLMap. + if options[:map] + if app.is_a? Hash + app.each do |path, appl| + path = '/'+path unless path[0] == ?/ + server.register(path, Rack::Handler::Mongrel.new(appl)) + end + elsif app.is_a? URLMap + app.instance_variable_get(:@mapping).each do |(host, path, appl)| + next if !host.nil? && !options[:Host].nil? && options[:Host] != host + path = '/'+path unless path[0] == ?/ + server.register(path, Rack::Handler::Mongrel.new(appl)) + end + else + raise ArgumentError, "first argument should be a Hash or URLMap" + end + else + server.register('/', Rack::Handler::Mongrel.new(app)) + end + yield server if block_given? + server.run.join +end + + +We wont dig into the server configuration itself, but this is +the last piece of our journey in the Rails initialization process. + +This high level overview will help you understand when you code is +executed and how, and overall become a better Rails developer. If you +still want to know more, the Rails source code itself is probably the +best place to go next. From 3b55b7fe7c6102ac705246eff229d5c7f96536f7 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 14 Jun 2012 22:15:55 +0530 Subject: [PATCH 31/34] Revert "Add a note about JavaScript runtime" This reverts commit d63b69ba7f2b9111aeb04b50884949c2a4b6e584. --- guides/source/contributing_to_ruby_on_rails.textile | 7 ------- 1 file changed, 7 deletions(-) diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index 128a968f69..6416b75185 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -72,13 +72,6 @@ Also, SQLite3 and its development files for the +sqlite3-ruby+ gem -- in Ubuntu $ sudo apt-get install sqlite3 libsqlite3-dev -Some tests use "execjs":https://github.com/sstephenson/execjs and require JavaScript runtime to be installed. Node.js is the easiest one to install: - - -$ sudo apt-get install nodejs - - - Get a recent version of "Bundler":http://gembundler.com/: From 6efc5bf4044a44bd8b207fc3197195f1a38c55db Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 14 Jun 2012 22:37:17 +0530 Subject: [PATCH 32/34] copy editing [ci skip] --- .../metal/request_forgery_protection.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index 736f70af4c..53534c0307 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -9,10 +9,13 @@ module ActionController #:nodoc: # by including a token in the rendered html for your application. This token is # stored as a random string in the session, to which an attacker does not have # access. When a request reaches your application, \Rails verifies the received - # token with the token in the session. All requests are checked except GET requests - # as these should be idempotent. It's important to remember that XML or JSON - # requests are also affected and if you're building an API you'll need - # something like that: + # token with the token in the session. Only HTML and JavaScript requests are checked, + # so this will not protect your XML API (presumably you'll have a different + # authentication scheme there anyway). Also, GET requests are not protected as these + # should be idempotent. + # + # It's important to remember that XML or JSON requests are also affected and if + # you're building an API you'll need something like: # # class ApplicationController < ActionController::Base # protect_from_forgery From 0a83e89222e817f9b1fa041cc286a83461533c0c Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 14 Jun 2012 22:45:42 +0530 Subject: [PATCH 33/34] moar copy edits [ci skip] --- actionpack/lib/action_controller/metal/http_authentication.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 76f380e583..a0d1064094 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -408,8 +408,7 @@ module ActionController # ActionController::Base instance for the current request. # # [login_procedure] - # Proc to call if a token is present. The Proc should take 2 - # arguments: + # Proc to call if a token is present. The Proc should take two arguments: # # authenticate(controller) { |token, options| ... } # From 5795efa9d8c330853c9ce4507abc3d5b8baa65e1 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 14 Jun 2012 22:51:29 +0530 Subject: [PATCH 34/34] move the info about cosmetic changes to a diff location [ci skip] --- guides/source/contributing_to_ruby_on_rails.textile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index 6416b75185..8e14297235 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -317,6 +317,8 @@ Now get busy and add or edit code. You’re on your branch now, so you can write * Include tests that fail without your code, and pass with it. * Update the (surrounding) documentation, examples elsewhere, and the guides: whatever is affected by your contribution. +TIP: Changes that are cosmetic in nature and do not add anything substantial to the stability, functionality, or testability of Rails will generally not be accepted. + h4. Follow the Coding Conventions Rails follows a simple set of coding style conventions. @@ -338,10 +340,6 @@ You should not be the only person who looks at the code before you submit it. Yo You might want also to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help you check your code when you're writing your first patches. -h4. Cosmetic Changes - -Changes that are cosmetic in nature and do not improve the stability, functionality, or testability of rails will generally not be accepted. - h4. Commit Your Changes When you're happy with the code on your computer, you need to commit the changes to git: