aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/4_0_release_notes.textile591
-rw-r--r--guides/source/active_model_basics.textile2
-rw-r--r--guides/source/active_support_core_extensions.textile100
-rw-r--r--guides/source/caching_with_rails.textile2
-rw-r--r--guides/source/contributing_to_ruby_on_rails.textile6
-rw-r--r--guides/source/form_helpers.textile12
-rw-r--r--guides/source/initialization.textile754
-rw-r--r--guides/source/layouts_and_rendering.textile2
-rw-r--r--guides/source/rails_on_rack.textile17
-rw-r--r--guides/source/routing.textile14
-rw-r--r--guides/source/security.textile33
-rw-r--r--guides/source/upgrading_ruby_on_rails.textile2
12 files changed, 847 insertions, 688 deletions
diff --git a/guides/source/4_0_release_notes.textile b/guides/source/4_0_release_notes.textile
new file mode 100644
index 0000000000..1ed6006b6b
--- /dev/null
+++ b/guides/source/4_0_release_notes.textile
@@ -0,0 +1,591 @@
+h2. Ruby on Rails 4.0 Release Notes
+
+Highlights in Rails 4.0:
+
+These release notes cover the major changes, but do not include each bug-fix and changes. If you want to see everything, check out the "list of commits":https://github.com/rails/rails/commits/master in the main Rails repository on GitHub.
+
+endprologue.
+
+h3. Upgrading to Rails 4.0
+
+TODO. This is a WIP guide.
+
+If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. Then take heed of the following changes:
+
+h4. Rails 4.0 requires at least Ruby 1.9.3
+
+Rails 4.0 requires Ruby 1.9.3 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible.
+
+h4. What to update in your apps
+
+* Update your Gemfile to depend on
+** <tt>rails = 4.0.0</tt>
+** <tt>sass-rails ~> 3.2.3</tt>
+** <tt>coffee-rails ~> 3.2.1</tt>
+** <tt>uglifier >= 1.0.3</tt>
+
+TODO: Update the versions above.
+
+* Rails 4.0 removes <tt>vendor/plugins</tt> completely. You have to replace these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>.
+
+TODO: Configuration changes in environment files
+
+h3. Creating a Rails 4.0 application
+
+<shell>
+# You should have the 'rails' rubygem installed
+$ rails new myapp
+$ cd myapp
+</shell>
+
+h4. Vendoring Gems
+
+Rails now uses a +Gemfile+ in the application root to determine the gems you require for your application to start. This +Gemfile+ is processed by the "Bundler":https://github.com/carlhuda/bundler gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
+
+More information: "Bundler homepage":http://gembundler.com
+
+h4. Living on the Edge
+
++Bundler+ and +Gemfile+ makes freezing your Rails application easy as pie with the new dedicated +bundle+ command. If you want to bundle straight from the Git repository, you can pass the +--edge+ flag:
+
+<shell>
+$ rails new myapp --edge
+</shell>
+
+If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag:
+
+<shell>
+$ ruby /path/to/rails/railties/bin/rails new myapp --dev
+</shell>
+
+h3. Major Features
+
+h3. Documentation
+
+h3. Railties
+
+* Add runner to <tt>Rails::Railtie</tt> as a hook called just after runner starts.
+
+* Add <tt>/rails/info/routes</tt> path which displays the same information as +rake routes+.
+
+* Improved +rake routes+ output for redirects.
+
+* Load all environments available in config.paths["config/environments"].
+
+* The application generator generates <tt>public/humans.txt</tt> with some basic data.
+
+* Add <tt>config.queue_consumer</tt> to allow the default consumer to be configurable.
+
+* Add <tt>Rails.queue</tt> as an interface with a default implementation that consumes jobs in a separate thread.
+
+* Remove <tt>Rack::SSL</tt> in favour of <tt>ActionDispatch::SSL</tt>.
+
+* Allow to set class that will be used to run as a console, other than IRB, with <tt>Rails.application.config.console=</tt>. It's best to add it to console block.
+
+<ruby>
+# it can be added to config/application.rb
+console do
+ # this block is called only when running console,
+ # so we can safely require pry here
+ require "pry"
+ config.console = Pry
+end
+</ruby>
+
+* Add convenience hide! method to Rails generators to hide current generator
+ namespace from showing when running rails generate.
+
+* Scaffold now uses +content_tag_for+ in <tt>index.html.erb</tt>.
+
+* <tt>Rails::Plugin</tt> is removed. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies.
+
+h4(#railties_deprecations). Deprecations
+
+h3. Action Mailer
+
+* No changes.
+
+h3. Action Pack
+
+h4. Action Controller
+
+* Extracted redirect logic from <tt>ActionController::ForceSSL::ClassMethods.force_ssl</tt> into <tt>ActionController::ForceSSL#force_ssl_redirect</tt>.
+
+* URL path parameters with invalid encoding now raise <tt>ActionController::BadRequest</tt>.
+
+* Malformed query and request parameter hashes now raise <tt>ActionController::BadRequest</tt>.
+
+* +respond_to+ and +respond_with+ now raise <tt>ActionController::UnknownFormat</tt> instead of directly returning head 406. The exception is rescued and converted to 406 in the exception handling middleware.
+
+* JSONP now uses mimetype <tt>application/javascript</tt> instead of <tt>application/json</tt>.
+
+* Session arguments passed to process calls in functional tests are now merged into the existing session, whereas previously they would replace the existing session. This change may break some existing tests if they are asserting the exact contents of the session but should not break existing tests that only assert individual keys.
+
+* Forms of persisted records use always PATCH (via the _method hack).
+
+* For resources, both PATCH and PUT are routed to the update action.
+
+* Don't ignore +force_ssl+ in development. This is a change of behavior - use a :if condition to recreate the old behavior.
+
+<ruby>
+class AccountsController < ApplicationController
+ force_ssl :if => :ssl_configured?
+
+ def ssl_configured?
+ !Rails.env.development?
+ end
+end
+</ruby>
+
+h5(#actioncontroller_deprecations). Deprecations
+
+* Deprecated ActionController::Integration in favour of ActionDispatch::Integration
+
+* Deprecated ActionController::IntegrationTest in favour of ActionDispatch::IntegrationTest
+
+* Deprecated ActionController::PerformanceTest in favour of ActionDispatch::PerformanceTest
+
+* Deprecated ActionController::AbstractRequest in favour of ActionDispatch::Request
+
+* Deprecated ActionController::Request in favour of ActionDispatch::Request
+
+* Deprecated ActionController::AbstractResponse in favour of ActionDispatch::Response
+
+* Deprecated ActionController::Response in favour of ActionDispatch::Response
+
+* Deprecated ActionController::Routing in favour of ActionDispatch::Routing
+
+h4. Action Dispatch
+
+* Added <tt>ActionDispatch::SSL</tt> middleware that when included force all the requests to be under HTTPS protocol.
+
+* Copy literal route constraints to defaults so that url generation know about them. The copied constraints are :protocol, :subdomain, :domain, :host and :port.
+
+* Allows +assert_redirected_to+ to match against a regular expression.
+
+* Add backtrace to development routing error page.
+
+* +assert_generates+, +assert_recognizes+, and +assert_routing+ all raise +Assertion+ instead of +RoutingError+.
+
+* Allows the route helper root to take a string argument. For example, <tt>root 'pages#main'</tt> as a shortcut for <tt>root to: 'pages#main'</tt>.
+
+* Adds support for the PATCH verb:
+ Request objects respond to patch?.
+ Routes have a new patch method, and understand :patch in the
+ existing places where a verb is configured, like :via.
+ New method patch available in functional tests.
+ If :patch is the default verb for updates, edits are
+ tunneled as PATCH rather than as PUT, and routing acts accordingly.
+ New method patch_via_redirect available in integration tests.
+
+* Integration tests support the OPTIONS method.
+
+* +expires_in+ accepts a +must_revalidate+ flag. If true, "must-revalidate" is added to the Cache-Control header.
+
+* Default responder will now always use your overridden block in respond_with to render your response.
+
+* Turn off verbose mode of rack-cache, we still have X-Rack-Cache to check that info.
+
+* Include mounted_helpers (helpers for accessing mounted engines) in <tt>ActionDispatch::IntegrationTest</tt> by default.
+
+h5(#actiondispatch_deprecations). Deprecations
+
+h4. Action View
+
+* Make current object and counter (when it applies) variables accessible when rendering templates with :object / :collection.
+
+* Allow to lazy load +default_form_builder+ by passing a String instead of a constant.
+
+* Add index method to +FormBuilder+ class.
+
+* Adds support for layouts when rendering a partial with a given collection.
+
+* Remove <tt>:disable_with</tt> in favor of <tt>data-disable-with</tt> option from +submit_tag+, +button_tag+ and +button_to+ helpers.
+
+* Remove <tt>:mouseover</tt> option from +image_tag+ helper.
+
+* Templates without a handler extension now raises a deprecation warning but still defaults to ERb. In future releases, it will simply return the template content.
+
+* Add divider option to +grouped_options_for_select+ to generate a separator optgroup automatically, and deprecate prompt as third argument, in favor of using an options hash.
+
+* Add +time_field+ and +time_field_tag+ helpers which render an <tt>input[type="time"]</tt> tag.
+
+* Removed old +text_helper+ apis for +highlight+, +excerpt+ and +word_wrap+.
+
+* Allow to use mounted_helpers (helpers for accessing mounted engines) in <tt>ActionView::TestCase</tt>.
+
+* Remove the leading \n added by textarea on +assert_select+.
+
+* Changed default value for <tt>config.action_view.embed_authenticity_token_in_remote_forms</tt> to false. This change breaks remote forms that need to work also without JavaScript, so if you need such behavior, you can either set it to true or explicitly pass <tt>:authenticity_token => true</tt> in form options.
+
+* Make possible to use a block in +button_to+ helper if button text is hard to fit into the name parameter:
+
+<ruby>
+<%= button_to [:make_happy, @user] do %>
+ Make happy <strong><%= @user.name %></strong>
+<% end %>
+# => "<form method="post" action="/users/1/make_happy" class="button_to">
+# <div>
+# <button type="submit">
+# Make happy <strong>Name</strong>
+# </button>
+# </div>
+# </form>"
+</ruby>
+
+* Replace +include_seconds+ boolean argument with <tt>:include_seconds => true</tt> option in +distance_of_time_in_words+ and +time_ago_in_words+ signature.
+
+* Remove +button_to_function+ and +link_to_function+ helpers.
+
+* truncate now always returns an escaped HTML-safe string. The option :escape can be used as false to not escape the result.
+
+* truncate now accepts a block to show extra content when the text is truncated.
+
+* Add +week_field+, +week_field_tag+, +month_field+, +month_field_tag+, +datetime_local_field+, +datetime_local_field_tag+, +datetime_field+ and +datetime_field_tag+ helpers.
+
+* Add +color_field+ and +color_field_tag+ helpers.
+
+* Add +include_hidden+ option to select tag. With <tt>:include_hidden => false</tt> select with multiple attribute doesn't generate hidden input with blank value.
+
+* Removed default size option from the +text_field+, +search_field+, +telephone_field+, +url_field+, +email_field+ helpers.
+
+* Removed default cols and rows options from the +text_area+ helper.
+
+* Adds +image_url+, +javascript_url+, +stylesheet_url+, +audio_url+, +video_url+, and +font_url+ to assets tag helper. These URL helpers will return the full path to your assets. This is useful when you are going to reference this asset from external host.
+
+* Allow +value_method+ and +text_method+ arguments from +collection_select+ and +options_from_collection_for_select+ to receive an object that responds to :call, such as a proc, to evaluate the option in the current element context. This works the same way with +collection_radio_buttons+ and +collection_check_boxes+.
+
+* Add +date_field+ and +date_field_tag+ helpers which render an <tt>input[type="date"]</tt> tag.
+
+* Add +collection_check_boxes+ form helper, similar to +collection_select+:
+
+<ruby>
+collection_check_boxes :post, :author_ids, Author.all, :id, :name
+# Outputs something like:
+<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" />
+<label for="post_author_ids_1">D. Heinemeier Hansson</label>
+<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
+<label for="post_author_ids_2">D. Thomas</label>
+<input name="post[author_ids][]" type="hidden" value="" />
+</ruby>
+
+The label/check_box pairs can be customized with a block.
+
+* Add +collection_radio_buttons+ form helper, similar to collection_select:
+
+<ruby>
+collection_radio_buttons :post, :author_id, Author.all, :id, :name
+# Outputs something like:
+<input id="post_author_id_1" name="post[author_id]" type="radio" value="1" />
+<label for="post_author_id_1">D. Heinemeier Hansson</label>
+<input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
+<label for="post_author_id_2">D. Thomas</label>
+</ruby>
+
+The label/radio_button pairs can be customized with a block.
+
+* +check_box+ with :form HTML5 attribute will now replicate the :form attribute to the hidden field as well.
+
+* label form helper accepts :for => nil to not generate the attribute.
+
+* Add :format option to +number_to_percentage+.
+
+* Add <tt>config.action_view.logger</tt> to configure logger for Action View.
+
+* +check_box+ helper with :disabled => true will generate a disabled hidden field to conform with the HTML convention where disabled fields are not submitted with the form. This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
+
+* +favicon_link_tag+ helper will now use the favicon in <tt>app/assets</tt> by default.
+
+* <tt>ActionView::Helpers::TextHelper#highlight</tt> now defaults to the HTML5 +mark+ element.
+
+h5(#actionview_deprecations). Deprecations
+
+h4. Sprockets
+
+Moved into a separate gem <tt>sprockets-rails</tt>.
+
+h3. Active Record
+
+* Allow blocks for count with <tt>ActiveRecord::Relation</tt>, to work similar as <tt>Array#count</tt>: <tt>Person.where("age > 26").count { |person| person.gender == 'female' }</tt>
+
+* Added support to <tt>CollectionAssociation#delete</tt> for passing fixnum or string values as record ids. This finds the records responding to the id and executes delete on them.
+
+<ruby>
+class Person < ActiveRecord::Base
+ has_many :pets
+end
+
+person.pets.delete("1") # => [#<Pet id: 1>]
+person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>]
+</ruby>
+
+* It's not possible anymore to destroy a model marked as read only.
+
+* Added ability to <tt>ActiveRecord::Relation#from</tt> to accept other <tt>ActiveRecord::Relation</tt> objects.
+
+* Added custom coders support for <tt>ActiveRecord::Store</tt>. Now you can set your custom coder like this:
+
+<ruby>store :settings, accessors: [ :color, :homepage ], coder: JSON</ruby>
+
+* mysql and mysql2 connections will set SQL_MODE=STRICT_ALL_TABLES by default to avoid silent data loss. This can be disabled by specifying strict: false in your database.yml.
+
+* Added default order to first to assure consistent results among diferent database engines. Introduced take as a replacement to the old behavior of first.
+
+* Added an :index option to automatically create indexes for references and belongs_to statements in migrations. The references and belongs_to methods now support an index option that receives either a boolean value or an options hash that is identical to options available to the add_index method:
+
+<ruby>
+create_table :messages do |t|
+ t.references :person, :index => true
+end
+</ruby>
+
+Is the same as:
+
+<ruby>
+create_table :messages do |t|
+ t.references :person
+end
+add_index :messages, :person_id
+</ruby>
+
+Generators have also been updated to use the new syntax.
+
+* Added bang methods for mutating <tt>ActiveRecord::Relation</tt> objects. For example, while <tt>foo.where(:bar)</tt> will return a new object leaving foo unchanged, <tt>foo.where!(:bar)</tt> will mutate the foo object.
+
+* Added #find_by and #find_by! to mirror the functionality provided by dynamic finders in a way that allows dynamic input more easily:
+
+<ruby>
+Post.find_by name: 'Spartacus', rating: 4
+Post.find_by "published_at < ?", 2.weeks.ago
+Post.find_by! name: 'Spartacus'
+</ruby>
+
+* Added <tt>ActiveRecord::Base#slice</tt> to return a hash of the given methods with their names as keys and returned values as values.
+
+* Remove IdentityMap - IdentityMap has never graduated to be an "enabled-by-default" feature, due to some inconsistencies with associations, as described in this commit: https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6. Hence the removal from the codebase, until such issues are fixed.
+
+* Added a feature to dump/load internal state of SchemaCache instance because we want to boot rails more quickly when we have many models.
+
+<ruby>
+# execute rake task.
+RAILS_ENV=production bundle exec rake db:schema:cache:dump
+=> generate db/schema_cache.dump
+
+# add config.use_schema_cache_dump = true in config/production.rb. BTW, true is default.
+
+# boot rails.
+RAILS_ENV=production bundle exec rails server
+=> use db/schema_cache.dump
+
+# If you remove clear dumped cache, execute rake task.
+RAILS_ENV=production bundle exec rake db:schema:cache:clear
+=> remove db/schema_cache.dump
+</ruby>
+
+* Added support for partial indices to PostgreSQL adapter.
+
+* The +add_index+ method now supports a +where+ option that receives a string with the partial index criteria.
+
+* Implemented <tt>ActiveRecord::Relation#none</tt> method which returns a chainable relation with zero records (an instance of the NullRelation class). Any subsequent condition chained to the returned relation will continue generating an empty relation and will not fire any query to the database.
+
+* Added the <tt>ActiveRecord::NullRelation</tt> class implementing the null object pattern for the Relation class.
+
+* Added +create_join_table+ migration helper to create HABTM join tables.
+
+<ruby>
+create_join_table :products, :categories
+# =>
+# create_table :categories_products, :id => false do |td|
+# td.integer :product_id, :null => false
+# td.integer :category_id, :null => false
+# end
+</ruby>
+
+* The primary key is always initialized in the @attributes hash to nil (unless another value has been specified).
+
+* In previous releases, the following would generate a single query with an OUTER JOIN comments, rather than two separate queries:
+
+<ruby>Post.includes(:comments).where("comments.name = 'foo'")</ruby>
+
+This behaviour relies on matching SQL string, which is an inherently flawed idea unless we write an SQL parser, which we do not wish to do. Therefore, it is now deprecated.
+
+To avoid deprecation warnings and for future compatibility, you must explicitly state which tables you reference, when using SQL snippets:
+
+<ruby>Post.includes(:comments).where("comments.name = 'foo'").references(:comments)</ruby>
+
+Note that you do not need to explicitly specify references in the following cases, as they can be automatically inferred:
+
+<ruby>
+Post.where(comments: { name: 'foo' })
+Post.where('comments.name' => 'foo')
+Post.order('comments.name')
+</ruby>
+
+You also do not need to worry about this unless you are doing eager loading. Basically, don't worry unless you see a deprecation warning or (in future releases) an SQL error due to a missing JOIN.
+
+* Support for the +schema_info+ table has been dropped. Please switch to +schema_migrations+.
+
+* Connections *must* be closed at the end of a thread. If not, your connection pool can fill and an exception will be raised.
+
+* Added the <tt>ActiveRecord::Model</tt> module which can be included in a class as an alternative to inheriting from <tt>ActiveRecord::Base</tt>:
+
+<ruby>
+class Post
+ include ActiveRecord::Model
+end
+</ruby>
+
+* PostgreSQL hstore records can be created.
+
+* PostgreSQL hstore types are automatically deserialized from the database.
+
+h4(#activerecord_deprecations). Deprecations
+
+* Deprecated most of the 'dynamic finder' methods. All dynamic methods except for +find_by_...+ and +find_by_...!+ are deprecated. Here's how you can rewrite the code:
+
+<ruby>
+find_all_by_... can be rewritten using where(...)
+find_last_by_... can be rewritten using where(...).last
+scoped_by_... can be rewritten using where(...)
+find_or_initialize_by_... can be rewritten using where(...).first_or_initialize
+find_or_create_by_... can be rewritten using where(...).first_or_create
+find_or_create_by_...! can be rewritten using where(...).first_or_create!
+</ruby>
+
+The implementation of the deprecated dynamic finders has been moved to the +active_record_deprecated_finders+ gem.
+
+* Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do. For example this:
+
+<ruby>Post.find(:all, :conditions => { :comments_count => 10 }, :limit => 5)</ruby>
+
+should be rewritten in the new style which has existed since Rails 3:
+
+<ruby>Post.where(comments_count: 10).limit(5)</ruby>
+
+Note that as an interim step, it is possible to rewrite the above as:
+
+<ruby>Post.scoped(:where => { :comments_count => 10 }, :limit => 5)</ruby>
+
+This could save you a lot of work if there is a lot of old-style finder usage in your application.
+
+Calling <tt>Post.scoped(options)</tt> is a shortcut for <tt>Post.scoped.merge(options)</tt>. <tt>Relation#merge</tt> now accepts a hash of options, but they must be identical to the names of the equivalent finder method. These are mostly identical to the old-style finder option names, except in the following cases:
+
+<plain>
+:conditions becomes :where
+:include becomes :includes
+:extend becomes :extending
+</plain>
+
+The code to implement the deprecated features has been moved out to the +active_record_deprecated_finders+ gem. This gem is a dependency of Active Record in Rails 4.0. It will no longer be a dependency from Rails 4.1, but if your app relies on the deprecated features then you can add it to your own Gemfile. It will be maintained by the Rails core team until Rails 5.0 is released.
+
+* Deprecate eager-evaluated scopes.
+
+ Don't use this:
+
+ scope :red, where(color: 'red')
+ default_scope where(color: 'red')
+
+ Use this:
+
+ scope :red, -> { where(color: 'red') }
+ default_scope { where(color: 'red') }
+
+ The former has numerous issues. It is a common newbie gotcha to do the following:
+
+ scope :recent, where(published_at: Time.now - 2.weeks)
+
+ Or a more subtle variant:
+
+ scope :recent, -> { where(published_at: Time.now - 2.weeks) }
+ scope :recent_red, recent.where(color: 'red')
+
+ Eager scopes are also very complex to implement within Active Record, and there are still bugs. For example, the following does not do what you expect:
+
+ scope :remove_conditions, except(:where)
+ where(...).remove_conditions # => still has conditions
+
+* Added deprecation for the :dependent => :restrict association option.
+
+* Up until now has_many and has_one, :dependent => :restrict option raised a DeleteRestrictionError at the time of destroying the object. Instead, it will add an error on the model.
+
+* To fix this warning, make sure your code isn't relying on a DeleteRestrictionError and then add config.active_record.dependent_restrict_raises = false to your application config.
+
+* New rails application would be generated with the config.active_record.dependent_restrict_raises = false in the application config.
+
+h3. Active Model
+
+* Passing false hash values to +validates+ will no longer enable the corresponding validators.
+
+* +ConfirmationValidator+ error messages will attach to <tt>:#{attribute}_confirmation</tt> instead of +attribute+.
+
+* Added <tt>ActiveModel::Model</tt>, a mixin to make Ruby objects work with Action Pack out of the box.
+
+* <tt>ActiveModel::Errors#to_json</tt> supports a new parameter <tt>:full_messages</tt>.
+
+* Trims down the API by removing <tt>valid?</tt> and <tt>errors.full_messages</tt>.
+
+h4(#activemodel_deprecations). Deprecations
+
+h3. Active Resource
+
+* Active Resource is removed from Rails 4.0 and is now a separate gem. TODO: put a link to the gem here.
+
+h3. Active Support
+
+* <tt>ActionView::Helpers::NumberHelper</tt> methods have been moved to <tt>ActiveSupport::NumberHelper</tt> and are now available via <tt>Numeric#to_s</tt>.
+
+* <tt>Numeric#to_s</tt> now accepts the formatting options :phone, :currency, :percentage, :delimited, :rounded, :human, and :human_size.
+
+* Add <tt>Hash#transform_keys</tt>, <tt>Hash#transform_keys!</tt>, <tt>Hash#deep_transform_keys</tt> and <tt>Hash#deep_transform_keys!</tt>.
+
+* Changed xml type datetime to dateTime (with upper case letter T).
+
+* Add <tt>:instance_accessor</tt> option for <tt>class_attribute</tt>.
+
+* +constantize+ now looks in the ancestor chain.
+
+* Add <tt>Hash#deep_stringify_keys</tt> and <tt>Hash#deep_stringify_keys!</tt> to convert all keys from a +Hash+ instance into strings.
+
+* Add <tt>Hash#deep_symbolize_keys</tt> and <tt>Hash#deep_symbolize_keys!</tt> to convert all keys from a +Hash+ instance into symbols.
+
+* <tt>Object#try</tt> can't call private methods.
+
+* AS::Callbacks#run_callbacks remove key argument.
+
+* +deep_dup+ works more expectedly now and duplicates also values in +Hash+ instances and elements in +Array+ instances.
+
+* Inflector no longer applies ice -> ouse to words like slice, police.
+
+* Add <tt>ActiveSupport::Deprecations.behavior = :silence</tt> to completely ignore Rails runtime deprecations.
+
+* Make <tt>Module#delegate</tt> stop using send - can no longer delegate to private methods.
+
+* AS::Callbacks deprecate :rescuable option.
+
+* Adds <tt>Integer#ordinal</tt> to get the ordinal suffix string of an integer.
+
+* AS::Callbacks :per_key option is no longer supported.
+
+* AS::Callbacks#define_callbacks add :skip_after_callbacks_if_terminated option.
+
+* Add html_escape_once to ERB::Util, and delegate escape_once tag helper to it.
+
+* Remove <tt>ActiveSupport::TestCase#pending</tt> method, use +skip+ instead.
+
+* Deletes the compatibility method <tt>Module#method_names</tt>, use <tt>Module#methods</tt> from now on (which returns symbols).
+
+* Deletes the compatibility method <tt>Module#instance_method_names</tt>, use <tt>Module#instance_methods</tt> from now on (which returns symbols).
+
+* Unicode database updated to 6.1.0.
+
+* Adds +encode_big_decimal_as_string+ option to force JSON serialization of BigDecimals as numeric instead of wrapping them in strings for safety.
+
+h4(#activesupport_deprecations). Deprecations
+
+* <tt>BufferedLogger</tt> is deprecated. Use <tt>ActiveSupport::Logger</tt> or the +logger+ from Ruby stdlib.
+
+* Deprecates the compatibility method <tt>Module#local_constant_names</tt> and use <tt>Module#local_constants</tt> instead (which returns symbols).
+
+h3. Credits
+
+See the "full list of contributors to Rails":http://contributors.rubyonrails.org/ for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
diff --git a/guides/source/active_model_basics.textile b/guides/source/active_model_basics.textile
index d373f4ac85..7cafff2ad8 100644
--- a/guides/source/active_model_basics.textile
+++ b/guides/source/active_model_basics.textile
@@ -187,7 +187,7 @@ class Person
attr_accessor :name, :email, :token
validates :name, :presence => true
- validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
+ validates_format_of :email, :with => /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
validates! :token, :presence => true
end
diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile
index 2addc50d68..40ef19cfbf 100644
--- a/guides/source/active_support_core_extensions.textile
+++ b/guides/source/active_support_core_extensions.textile
@@ -156,7 +156,7 @@ NOTE: Defined in +active_support/core_ext/object/duplicable.rb+.
h4. +deep_dup+
-The +deep_dup+ method returns deep copy of given object. Normally, when you +dup+ an object that contains other objects, ruby does not +dup+ them. If you have array with a string, for example, it will look like this:
+The +deep_dup+ method returns deep copy of a given object. Normally, when you +dup+ an object that contains other objects, ruby does not +dup+ them. If you have an array with a string, for example, it will look like this:
<ruby>
array = ['string']
@@ -164,7 +164,7 @@ duplicate = array.dup
duplicate.push 'another-string'
-# object was duplicated, element added only to duplicate
+# object was duplicated, so element was added only to duplicate
array #=> ['string']
duplicate #=> ['string', 'another-string']
@@ -177,7 +177,7 @@ duplicate #=> ['foo', 'another-string']
As you can see, after duplicating +Array+ instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since +dup+ does not make deep copy, the string inside array is still the same object.
-If you need a deep copy of an object, you should use +deep_dup+ in such situation:
+If you need a deep copy of an object, you should use +deep_dup+. Here is an example:
<ruby>
array = ['string']
@@ -189,7 +189,7 @@ array #=> ['string']
duplicate #=> ['foo']
</ruby>
-If object is not duplicable +deep_dup+ will just return this object:
+If object is not duplicable, +deep_dup+ will just return this object:
<ruby>
number = 1
@@ -201,9 +201,21 @@ NOTE: Defined in +active_support/core_ext/object/deep_dup.rb+.
h4. +try+
-Sometimes you want to call a method provided the receiver object is not +nil+, which is something you usually check first. +try+ is like +Object#send+ except that it returns +nil+ if sent to +nil+.
+When you want to call a method on an object only if it is not +nil+, the simplest way to achieve it is with conditional statements, adding unnecessary clutter. The alternative is to use +try+. +try+ is like +Object#send+ except that it returns +nil+ if sent to +nil+.
-For instance, in this code from +ActiveRecord::ConnectionAdapters::AbstractAdapter+ +@logger+ could be +nil+, but you save the check and write in an optimistic style:
+Here is an example:
+
+<ruby>
+# without try
+unless @number.nil?
+ @number.next
+end
+
+# with try
+@number.try(:next)
+</ruby>
+
+Another example is this code from +ActiveRecord::ConnectionAdapters::AbstractAdapter+ where +@logger+ could be +nil+. You can see that the code uses +try+ and avoids an unnecessary check.
<ruby>
def log_info(sql, name, ms)
@@ -245,7 +257,7 @@ NOTE: Defined in +active_support/core_ext/kernel/singleton_class.rb+.
h4. +acts_like?(duck)+
-The method +acts_like+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines
+The method +acts_like?+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines
<ruby>
def acts_like_string?
@@ -1845,16 +1857,24 @@ h4. Formatting
Enables the formatting of numbers in a variety of ways.
Produce a string representation of a number as a telephone number:
+
<ruby>
-5551234.to_s(:phone) # => 555-1234
-1235551234.to_s(:phone) # => 123-555-1234
-1235551234.to_s(:phone, :area_code => true) # => (123) 555-1234
-1235551234.to_s(:phone, :delimiter => " ") # => 123 555 1234
-1235551234.to_s(:phone, :area_code => true, :extension => 555) # => (123) 555-1234 x 555
-1235551234.to_s(:phone, :country_code => 1) # => +1-123-555-1234
+5551234.to_s(:phone)
+# => 555-1234
+1235551234.to_s(:phone)
+# => 123-555-1234
+1235551234.to_s(:phone, :area_code => true)
+# => (123) 555-1234
+1235551234.to_s(:phone, :delimiter => " ")
+# => 123 555 1234
+1235551234.to_s(:phone, :area_code => true, :extension => 555)
+# => (123) 555-1234 x 555
+1235551234.to_s(:phone, :country_code => 1)
+# => +1-123-555-1234
</ruby>
Produce a string representation of a number as currency:
+
<ruby>
1234567890.50.to_s(:currency) # => $1,234,567,890.50
1234567890.506.to_s(:currency) # => $1,234,567,890.51
@@ -1862,14 +1882,20 @@ Produce a string representation of a number as currency:
</ruby>
Produce a string representation of a number as a percentage:
+
<ruby>
-100.to_s(:percentage) # => 100.000%
-100.to_s(:percentage, :precision => 0) # => 100%
-1000.to_s(:percentage, :delimiter => '.', :separator => ',') # => 1.000,000%
-302.24398923423.to_s(:percentage, :precision => 5) # => 302.24399%
+100.to_s(:percentage)
+# => 100.000%
+100.to_s(:percentage, :precision => 0)
+# => 100%
+1000.to_s(:percentage, :delimiter => '.', :separator => ',')
+# => 1.000,000%
+302.24398923423.to_s(:percentage, :precision => 5)
+# => 302.24399%
</ruby>
Produce a string representation of a number in delimited form:
+
<ruby>
12345678.to_s(:delimited) # => 12,345,678
12345678.05.to_s(:delimited) # => 12,345,678.05
@@ -1879,33 +1905,36 @@ Produce a string representation of a number in delimited form:
</ruby>
Produce a string representation of a number rounded to a precision:
+
<ruby>
-111.2345.to_s(:rounded) # => 111.235
-111.2345.to_s(:rounded, :precision => 2) # => 111.23
-13.to_s(:rounded, :precision => 5) # => 13.00000
-389.32314.to_s(:rounded, :precision => 0) # => 389
-111.2345.to_s(:rounded, :significant => true) # => 111
+111.2345.to_s(:rounded) # => 111.235
+111.2345.to_s(:rounded, :precision => 2) # => 111.23
+13.to_s(:rounded, :precision => 5) # => 13.00000
+389.32314.to_s(:rounded, :precision => 0) # => 389
+111.2345.to_s(:rounded, :significant => true) # => 111
</ruby>
Produce a string representation of a number as a human-readable number of bytes:
+
<ruby>
-123.to_s(:human_size) # => 123 Bytes
-1234.to_s(:human_size) # => 1.21 KB
-12345.to_s(:human_size) # => 12.1 KB
-1234567.to_s(:human_size) # => 1.18 MB
-1234567890.to_s(:human_size) # => 1.15 GB
-1234567890123.to_s(:human_size) # => 1.12 TB
+123.to_s(:human_size) # => 123 Bytes
+1234.to_s(:human_size) # => 1.21 KB
+12345.to_s(:human_size) # => 12.1 KB
+1234567.to_s(:human_size) # => 1.18 MB
+1234567890.to_s(:human_size) # => 1.15 GB
+1234567890123.to_s(:human_size) # => 1.12 TB
</ruby>
Produce a string representation of a number in human-readable words:
+
<ruby>
-123.to_s(:human) # => "123"
-1234.to_s(:human) # => "1.23 Thousand"
-12345.to_s(:human) # => "12.3 Thousand"
-1234567.to_s(:human) # => "1.23 Million"
-1234567890.to_s(:human) # => "1.23 Billion"
-1234567890123.to_s(:human) # => "1.23 Trillion"
-1234567890123456.to_s(:human) # => "1.23 Quadrillion"
+123.to_s(:human) # => "123"
+1234.to_s(:human) # => "1.23 Thousand"
+12345.to_s(:human) # => "12.3 Thousand"
+1234567.to_s(:human) # => "1.23 Million"
+1234567890.to_s(:human) # => "1.23 Billion"
+1234567890123.to_s(:human) # => "1.23 Trillion"
+1234567890123456.to_s(:human) # => "1.23 Quadrillion"
</ruby>
NOTE: Defined in +active_support/core_ext/numeric/formatting.rb+.
@@ -2457,6 +2486,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 <tt>:root</tt>.
* 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 <tt>:skip_types</tt> exists and is true, an attribute "type" is added as well according to the following mapping:
+
<ruby>
XML_TYPE_NAMES = {
"Symbol" => "symbol",
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.
<ruby>
-config.cache_store = :memory_store, :size => 64.megabytes
+config.cache_store = :memory_store, { :size => 64.megabytes }
</ruby>
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.
diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile
index acf75d41cd..b52cd6c6b6 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:
<shell>
$ cd actionpack
-$ ruby -Itest test/template/form_helper_test.rb
+$ bundle exec ruby -Itest test/template/form_helper_test.rb
</shell>
h4. Warnings
@@ -190,6 +190,8 @@ $ rake postgresql:build_databases
NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
+NOTE: You'll see the following warning (or localized warning) during activating HStore extension in PostgreSQL 9.1.x or earlier: "WARNING: => is deprecated as an operator".
+
If you’re using another database, check the files under +activerecord/test/connections+ for default connection information. You can edit these files to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails.
You can now run the tests as you did for +sqlite3+. The tasks are respectively
@@ -317,6 +319,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.
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:
+
+<erb>
+<%= options_for_select([['Lisbon', 1, :'data-size' => '2.8 million'], ['Madrid', 2, :'data-size' => '3.2 million']], 2) %>
+
+output:
+
+<option value="1" data-size="2.8 million">Lisbon</option>
+<option value="2" selected="selected" data-size="3.2 million">Madrid</option>
+...
+</erb>
+
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+:
diff --git a/guides/source/initialization.textile b/guides/source/initialization.textile
index 48d4373afe..0638bbed10 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+
@@ -224,47 +224,27 @@ when 'server'
}
</ruby>
-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.
-
-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+.
-
-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:
+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.
<ruby>
-autoload :Inflector
-</ruby>
-
-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.
+require 'fileutils'
+require 'optparse'
+require 'action_dispatch'
-h4. +actionpack/lib/action_dispatch.rb+ cont'd.
+module Rails
+ class Server < ::Rack::Server
+</ruby>
-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.
++fileutils+ and +optparse+ are standard Ruby libraries which provide helper functions for working with files and parsing options.
-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.
+h4. +actionpack/lib/action_dispatch.rb+
-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.
+Action Dispatch is the routing component of the Rails framework. Other
+than the rouing itself, it adds
+functionalities like routing, session, and common middlewares.
-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+
@@ -363,11 +343,21 @@ def parse!(args)
...
</ruby>
-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. +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:
<ruby>
def start
@@ -405,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:
@@ -455,7 +445,8 @@ end
</ruby>
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).
<ruby>
@wrapped_app ||= build_app app
@@ -494,7 +485,7 @@ app = eval "Rack::Builder.new {( " <plus> cfgfile <plus> "\n )}.to_app",
TOPLEVEL_BINDING, config
</ruby>
-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:
<ruby>
require ::File.expand_path('../config/environment', __FILE__)
@@ -541,641 +532,128 @@ require "rails"
end
</ruby>
-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+
+For now, just keep in mind that common functionality like Rails engines,
+I18n and Rails configuration is all bein defined here.
-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.
+h4. Back to +config/environment.rb+
-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:
-
-<ruby>
-silence_warnings do
- # some code
-end
-</ruby>
-
-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.
+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+
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:
-
-<plain>
-BadConstant is deprecated! Use GoodConstant instead.
-</plain>
-
-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:
+The +initialize!+ method looks like this:
<ruby>
-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'
-</ruby>
-
-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:
-
-<ruby>
-module Rails
- module Rack
- autoload :Debugger, "rails/rack/debugger"
- autoload :Logger, "rails/rack/logger"
- autoload :LogTailer, "rails/rack/log_tailer"
- end
-end
-</ruby>
-
-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:
-
-<ruby>
-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'
-</ruby>
-
-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".
-
-<ruby>
-inflect.irregular('zombie', 'zombies')
-</ruby>
-
-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:
-
-<ruby>
-class Railtie < Rails::Railtie
- config.i18n = ActiveSupport::OrderedOptions.new
- config.i18n.railties_load_path = []
- config.i18n.load_path = []
- config.i18n.fallbacks = ActiveSupport::OrderedOptions.new
-</ruby>
-
-By inheriting from +Rails::Railtie+ the +Rails::Railtie#inherited+ method is called:
-
-<ruby>
-def inherited(base)
- unless base.abstract_railtie?
- base.send(:include, Railtie::Configurable)
- subclasses << base
- end
-end
-</ruby>
-
-This first checks if the Railtie that's inheriting it is a component of Rails itself:
-
-<ruby>
-ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Plugin Rails::Engine Rails::Application)
-
-...
-
-def abstract_railtie?
- ABSTRACT_RAILTIES.include?(name)
+def initialize!(group=:default) #:nodoc:
+ raise "Application has been already initialized." if @initialized
+ run_initializers(group, self)
+ @initialized = true
+ self
end
</ruby>
-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.
+As you can see, you can only initialize an app once. This is also where the initializers are run.
-<ruby>
-def subclasses
- @subclasses ||= []
-end
-</ruby>
+TODO: review this
-The +config+ method used at the top of +I18n::Railtie+ is defined on +Rails::Railtie+ and is defined like 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.
-<ruby>
-def config
- @config ||= Railtie::Configuration.new
-end
-</ruby>
-
-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+:
+After this is done we go back to +Rack::Server+
-<ruby>
-autoload :Configuration, "rails/railtie/configuration"
-</ruby>
+h4. Rack: lib/rack/server.rb
-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+:
+Last time we left when the +app+ method was being defined:
<ruby>
-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
-</ruby>
-
-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+.
+def app
+ @app ||= begin
+ if !::File.exist? options[:config]
+ abort "configuration #{options[:config]} not found"
+ end
-<ruby>
-initializer "i18n.callbacks" do
- ActionDispatch::Reloader.to_prepare do
- I18n::Railtie.reloader.execute_if_updated
+ app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
+ self.options.merge! options
+ app
end
end
</ruby>
-The +initializer+ method (from the +Rails::Initializable+ module) here doesn't run the block, but rather stores it to be run later on:
-
-<ruby>
-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
-</ruby>
-
-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:
-
-<ruby>
-def initialize(name, context, options, &block)
- @name, @context, @options, @block = name, context, options, block
-end
-</ruby>
-
-Once this +initialize+ method is finished, the object is added to the object the +initializers+ method returns:
+At this point +app+ is the Rails app itself (a middleware), and what
+happens next is Rack will call all the provided middlewares:
<ruby>
-def initializers
- @initializers ||= self.class.initializers_for(self)
-end
-</ruby>
-
-If +@initializers+ isn't set (which it won't be at this point), the +intializers_for+ method will be called for this class.
-
-<ruby>
-def initializers_for(binding)
- Collection.new(initializers_chain.map { |i| i.bind(binding) })
-end
-</ruby>
-
-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:
-
-<ruby>
-def initializers_chain
- initializers = Collection.new
- ancestors.reverse_each do |klass|
- next unless klass.respond_to?(:initializers)
- initializers = initializers + klass.initializers
+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
- initializers
-end
-</ruby>
-
-This method collects the initializers from the ancestors of this class and adds them to a new +Collection+ object using the <tt>+</tt> method which is defined like this for the <tt>Collection</tt> class:
-
-<ruby>
-def +(other)
- Collection.new(to_a + other.to_a)
+ app
end
</ruby>
-So this <tt>+</tt> method is overridden to return a new collection comprising of the existing collection as an array and then using the <tt>Array#+</tt> 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:
+Remember, +build_app+ was called (by wrapped_app) in the last line of +Server#start+.
+Here's how it looked like when we left:
<ruby>
-def after_initialize(&block)
- ActiveSupport.on_load(:after_initialize, :yield => true, &block)
-end
+server.run wrapped_app, options, &blk
</ruby>
-The +on_load+ method here is provided by the +active_support/lazy_load_hooks+ file which was required earlier and is defined like this:
+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:
<ruby>
-def self.on_load(name, options = {}, &block)
- if base = @loaded[name]
- execute_hook(base, options, block)
+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
- @load_hooks[name] << [block, options]
- end
-end
-</ruby>
-
-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+:
-
-<ruby>
-@load_hooks = Hash.new {|h,k| h[k] = [] }
-</ruby>
-
-This defines a new hash which has keys that default to empty arrays. This saves Rails from having to do something like this instead:
-
-<ruby>
-@load_hooks[name] = []
-@load_hooks[name] << [block, options]
-</ruby>
-
-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:
-
-<ruby>
-require 'active_support'
-require 'active_support/dependencies/autoload'
-require 'active_support/core_ext/module/attribute_accessors'
-</ruby>
-
-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:
-
-<ruby>
-module ActionPack
- module VERSION #:nodoc:
- MAJOR = 4
- MINOR = 0
- TINY = 0
- PRE = "beta"
-
- STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
+ server.register('/', Rack::Handler::Mongrel.new(app))
end
+ yield server if block_given?
+ server.run.join
end
</ruby>
-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:
-
-<ruby>
-module ActiveModel
- module VERSION #:nodoc:
- MAJOR = 4
- MINOR = 0
- TINY = 0
- PRE = "beta"
-
- STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
- end
-end
-</ruby>
-
-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:
-
-<ruby>
-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"
-</ruby>
-
-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:
-
-<ruby>
-autoload_under 'testing' do
- autoload :Assertions
-...
-</ruby>
-
-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.
-
-<ruby>
-require 'active_support'
-require 'active_model'
-require 'arel'
-</ruby>
-
-The file required next is +active_record/version+ which defines the +ActiveRecord::VERSION+ constant:
-
-<ruby>
-module ActiveRecord
- module VERSION #:nodoc:
- MAJOR = 4
- MINOR = 0
- TINY = 0
- PRE = "beta"
-
- STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
- end
-end
-</ruby>
-
-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:
-
-<ruby>
-ActiveSupport.on_load(:active_record) do
- Arel::Table.engine = self
-end
-</ruby>
-
-This will set the engine for +Arel::Table+ to be +ActiveRecord::Base+.
-
-The file then finishes with this line:
-
-<ruby>
-ActiveSupport.on_load(:i18n) do
- I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
-end
-</ruby>
-
-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 <tt>require</tt>s 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+.
-
-<ruby>
-require "rails"
-require "active_model/railtie"
-</ruby>
-
-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:
-
-<ruby>
-require "rails"
-require "action_controller"
-require "action_dispatch/railtie"
-</ruby>
-
-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+
+We wont dig into the server configuration itself, but this is
+the last piece of our journey in the Rails initialization process.
-+action_view.rb+
+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.
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:
diff --git a/guides/source/rails_on_rack.textile b/guides/source/rails_on_rack.textile
index 3a7c392508..63712b22ef 100644
--- a/guides/source/rails_on_rack.textile
+++ b/guides/source/rails_on_rack.textile
@@ -195,6 +195,23 @@ use Rack::Runtime
run Blog::Application.routes
</shell>
+If you want to remove session related middleware, do the following:
+
+<ruby>
+# config/application.rb
+config.middleware.delete "ActionDispatch::Cookies"
+config.middleware.delete "ActionDispatch::Session::CookieStore"
+config.middleware.delete "ActionDispatch::Flash"
+</ruby>
+
+And to remove browser related middleware,
+
+<ruby>
+# config/application.rb
+config.middleware.delete "ActionDispatch::BestStandardsSupport"
+config.middleware.delete "Rack::MethodOverride"
+</ruby>
+
h4. Internal Middleware Stack
Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
diff --git a/guides/source/routing.textile b/guides/source/routing.textile
index 0773a96c67..dae25853cd 100644
--- a/guides/source/routing.textile
+++ b/guides/source/routing.textile
@@ -644,6 +644,14 @@ You should put the +root+ route at the top of the file, because it is the most p
NOTE: The +root+ route only routes +GET+ requests to the action.
+h4. Unicode character routes
+
+You can specify unicode character routes directly. For example
+
+<ruby>
+match 'こんにちは' => 'welcome#index'
+</ruby>
+
h3. Customizing Resourceful Routes
While the default routes and helpers generated by +resources :posts+ will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
@@ -859,9 +867,11 @@ h3. Inspecting and Testing Routes
Rails offers facilities for inspecting and testing your routes.
-h4. Seeing Existing Routes with +rake+
+h4. Seeing Existing Routes
+
+To get a complete list of the available routes in your application, visit +http://localhost:3000/rails/info/routes+ in your browser while your server is running in the *development* environment. You can also execute the +rake routes+ command in your terminal to produce the same output.
-If you want a complete list of all of the available routes in your application, run +rake routes+ command. This will print all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see:
+Both methods will list all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see:
* The route name (if any)
* The HTTP verb used (if the route doesn't respond to all verbs)
diff --git a/guides/source/security.textile b/guides/source/security.textile
index 0931dd6393..626d6fa508 100644
--- a/guides/source/security.textile
+++ b/guides/source/security.textile
@@ -588,26 +588,43 @@ h4. Regular Expressions
INFO: _A common pitfall in Ruby's regular expressions is to match the string's beginning and end by ^ and $, instead of \A and \z._
-Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:
+Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Say you wanted to loosely validate a URL field and you used a simple regular expression like this:
<ruby>
-class File < ActiveRecord::Base
- validates :name, :format => /^[\w\.\-\<plus>]<plus>$/
-end
+ /^https?:\/\/[^\n]+$/i
</ruby>
-This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added ^ and $ so that file name will contain these characters from the beginning to the end of the string. However, _(highlight)in Ruby ^ and $ matches the *line* beginning and line end_. And thus a file name like this passes the filter without problems:
+This may work fine in some languages. However, _(highlight)in Ruby ^ and $ match the *line* beginning and line end_. And thus a URL like this passes the filter without problems:
<plain>
-file.txt%0A<script>alert('hello')</script>
+javascript:exploit_code();/*
+http://hi.com
+*/
</plain>
-Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert('hello')&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:
+This URL passes the filter because the regular expression matches – the second line, the rest does not matter. Now imagine we had a view that showed the URL like this:
+
+<ruby>
+ link_to "Homepage", @user.homepage
+</ruby>
+
+The link looks innocent to visitors, but when it's clicked, it will execute the javascript function "exploit_code" or any other javascript the attacker provides.
+
+To fix the regular expression, \A and \z should be used instead of ^ and $, like so:
<ruby>
-/\A[\w\.\-\<plus>]<plus>\z/
+ /\Ahttps?:\/\/[^\n]+\z/i
</ruby>
+Since this is a frequent mistake, the format validator (validates_format_of) now raises an exception if the provided regular expression starts with ^ or ends with $. If you do need to use ^ and $ instead of \A and \z (which is rare), you can set the :multiline option to true, like so:
+
+<ruby>
+ # content should include a line "Meanwhile" anywhere in the string
+ validates :content, :format => { :with => /^Meanwhile$/, :multiline => true }
+</ruby>
+
+Note that this only protects you against the most common mistake when using the format validator - you always need to keep in mind that ^ and $ match the *line* beginning and line end in Ruby, and not the beginning and end of a string.
+
h4. Privilege Escalation
WARNING: _Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it._
diff --git a/guides/source/upgrading_ruby_on_rails.textile b/guides/source/upgrading_ruby_on_rails.textile
index 6cdc6ab289..4bf4751127 100644
--- a/guides/source/upgrading_ruby_on_rails.textile
+++ b/guides/source/upgrading_ruby_on_rails.textile
@@ -50,6 +50,8 @@ h4(#action_pack4_0). Action Pack
Rails 4.0 changed how <tt>assert_generates</tt>, <tt>assert_recognizes</tt>, and <tt>assert_routing</tt> work. Now all these assertions raise <tt>Assertion</tt> instead of <tt>ActionController::RoutingError</tt>.
+Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, e.g. <tt>get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'</tt> to <tt>get 'こんにちは', :controller => 'welcome', :action => 'index'</tt>.
+
h4(#helpers_order). Helpers Loading Order
The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use <tt>helpers_path</tt> parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use <tt>config.railties_order=</tt> method.