From 53b34e84762b7f2d6b641f99dadbb1eab42907ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 1 Jul 2010 17:07:48 +0200 Subject: Avoid calls to Rails::Application since this is not the official API. Your application should *always* reference your application const (as Blog::Application) and Rails.application should be used just internally. --- railties/guides/source/initialization.textile | 72 ++++++++++----------------- 1 file changed, 26 insertions(+), 46 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index e458413b35..cedf823bdc 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -11,7 +11,7 @@ This guide first describes the process of +rails server+ then explains the Passe h3. Launch! -As of Rails 3, +script/server+ has become +rails server+. This was done to centralise all rails related commands to one common file. +As of Rails 3, +script/server+ has become +rails server+. This was done to centralize all rails related commands to one common file. The actual +rails+ command is kept in _railties/bin/rails_ and goes like this: @@ -58,11 +58,8 @@ In +script/rails+ we see the following: #!/usr/bin/env ruby # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. - ENV_PATH = File.expand_path('../../config/environment', __FILE__) - BOOT_PATH = File.expand_path('../../config/boot', __FILE__) - APP_PATH = File.expand_path('../../config/application', __FILE__) - - require BOOT_PATH + APP_PATH = File.expand_path('../../config/application', __FILE__) + require File.expand_path('../../config/boot', __FILE__) require 'rails/commands' @@ -79,15 +76,19 @@ h3. _config/boot.rb_ _config/boot.rb_ is the first stop for everything for initializing your application. This boot process does quite a bit of work for you and so this section attempts to go in-depth enough to explain what each of the pieces does. - # Use Bundler (preferred) + require 'rubygems' + + # Set up gems listed in the Gemfile. + gemfile = File.expand_path('../../Gemfile', __FILE__) begin - require File.expand_path('../../.bundle/environment', __FILE__) - rescue LoadError - require 'rubygems' + ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' Bundler.setup - end - + rescue Bundler::GemNotFound => e + STDERR.puts e.message + STDERR.puts "Try running `bundle install`." + exit! + end if File.exist?(gemfile) h3. Bundled Rails (3.x) @@ -164,33 +165,7 @@ TODO: Prettify when it becomes more stable. I won't go into what each of these gems are, as that is really something that needs covering on a case-by-case basis. We will however just dig a little under the surface of Bundler. -Back in _config/boot.rb_, the first line will try to include _.bundle/environment.rb_, which doesn't exist in a bare-bones Rails application and because this file does not exist Ruby will raise a +LoadError+ which will be rescued and run the following code: - - - require 'rubygems' - require 'bundler' - Bundler.setup - - -+Bundler.setup+ here will load and parse the +Gemfile+ and add the _lib_ directory of the gems mentioned **and** their dependencies (**and** their dependencies' dependencies, and so on) to the +$LOAD_PATH+. - -Now we will go down the alternate timeline where we generate a _.bundle/environment.rb_ file using the +bundle lock+ command. This command also creates a _Gemfile.lock_ file which is actually a YAML file loaded by this method in Bundler before it moves on to check for _Gemfile_: - - - def definition(gemfile = default_gemfile) - configure - root = Pathname.new(gemfile).dirname - lockfile = root.join("Gemfile.lock") - if lockfile.exist? - Definition.from_lock(lockfile) - else - Definition.from_gemfile(gemfile) - end - end - - - -The _.bundle/environment.rb_ file adds the _lib_ directory of all the gems specified in +Gemfile.lock+ to +$LOAD_PATH+. +Back in _config/boot.rb_, we call +Bundler.setup+ which will load and parse the +Gemfile+ and add the _lib_ directory of the gems mentioned **and** their dependencies (**and** their dependencies' dependencies, and so on) to the +$LOAD_PATH+. h3. Requiring Rails @@ -326,6 +301,11 @@ As you can see for the duration of the +eager_autoload+ block the class variable module ActiveSupport extend ActiveSupport::Autoload + autoload :DescendantsTracker + autoload :FileUpdateChecker + autoload :LogSubscriber + autoload :Notifications + # TODO: Narrow this list down eager_autoload do autoload :BacktraceCleaner @@ -348,7 +328,6 @@ As you can see for the duration of the +eager_autoload+ block the class variable autoload :OptionMerger autoload :OrderedHash autoload :OrderedOptions - autoload :Notifications autoload :Rescuable autoload :SecureRandom autoload :StringInquirer @@ -589,19 +568,20 @@ This file (_railties/lib/rails.rb_) requires the very, very basics that Rails ne require 'action_dispatch/railtie' -+require 'pathname'+ requires the Pathname class which is used for returning a Pathname object for +Rails.root+ so that instead of doing: ++require 'pathname'+ requires the Pathname class which is used for returning a Pathname object for +Rails.root+. Although is coming to use this path name to generate paths as below: - File.join(Rails.root, "app/controllers") + Rails.root.join("app/controllers") -You may do: +Pathname can also be converted to string, so the following syntax is preferred: - Rails.root.join("app/controllers") + "#{Rails.root}/app/controllers" -Although this is not new to Rails 3 (it was available in 2.3.5), it is something worthwhile pointing out. + +This works because Ruby automatically handles file path conversions. Although this is not new to Rails 3 (it was available in 2.3.5), it is something worthwhile pointing out. Inside this file there are other helpful helper methods defined, such as +Rails.root+, +Rails.env+, +Rails.logger+ and +Rails.application+. @@ -1833,7 +1813,7 @@ We do not already have a +Rails.application+, so instead this resorts to calling end -This +called_from+ setting looks a little overwhelming to begin with, but the short end of it is that it returns the route to your application's config directory, something like: _/home/you/yourapp/config_. After +called_from+ has been set, +super+ is again called and this means the +Rails::Railtie#inherited+ method (in _railties/lib/rails/railtie.rb_): +This +called_from+ setting looks a little overwhelming to begin with, but the short end of it is that it returns your application's root, something like: _/home/you/yourapp_. After +called_from+ has been set, +super+ is again called and this means the +Rails::Railtie#inherited+ method (in _railties/lib/rails/railtie.rb_): def inherited(base) -- cgit v1.2.3 From d7c1057652cfc971bb35ef09b0b1560fcd28ed70 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 1 Jul 2010 02:25:35 -0700 Subject: Bump bundler dependency to 1.0.0.beta.2 or later --- railties/guides/source/initialization.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index cedf823bdc..8498f79f4e 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -141,7 +141,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This * activesupport-3.0.0.beta4.gem * arel-0.4.0.gem * builder-2.1.2.gem -* bundler-0.9.26.gem +* bundler-1.0.0.beta.2.gem * erubis-2.6.5.gem * i18n-0.4.1.gem * mail-2.2.4.gem -- cgit v1.2.3 From 2ef8a2b403adcee21fe1a5bfadc125b24779cd53 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 1 Jul 2010 16:59:31 -0300 Subject: bump erubis version to 2.6.6 and thor version to 0.13.7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- railties/guides/source/initialization.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 8498f79f4e..9a0e23385d 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -142,7 +142,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This * arel-0.4.0.gem * builder-2.1.2.gem * bundler-1.0.0.beta.2.gem -* erubis-2.6.5.gem +* erubis-2.6.6.gem * i18n-0.4.1.gem * mail-2.2.4.gem * memcache-client-1.8.3.gem @@ -157,7 +157,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This * sqlite3-ruby-1.3.0.gem * text-format-1.0.0.gem * text-hyphen-1.0.0.gem -* thor-0.13.6.gem +* thor-0.13.7.gem * treetop-1.4.8.gem * tzinfo-0.3.22.gem -- cgit v1.2.3 From a5dda97602f2188a13cbcab5c7e9a5ba84ba876b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 5 Jul 2010 12:50:08 +0200 Subject: Define a convention for descendants and subclasses. The former should be symmetric with ancestors and include all children. However, it should not include self since ancestors + descendants should not have duplicated. The latter is symmetric to superclass in the sense it only includes direct children. By adopting a convention, we expect to have less conflict with other frameworks, as Datamapper. For this moment, to ensure ActiveModel::Validations can be used with Datamapper, we should always call ActiveSupport::DescendantsTracker.descendants(self) internally instead of self.descendants avoiding conflicts. --- .../source/active_support_core_extensions.textile | 81 ++++++++-------------- 1 file changed, 29 insertions(+), 52 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 844b9428bd..de0c00ac68 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -387,40 +387,6 @@ TIP: Since +with_options+ forwards calls to its receiver they can be nested. Eac NOTE: Defined in +active_support/core_ext/object/with_options.rb+. -h5. +subclasses_of+ - -The method +subclasses_of+ receives an arbitrary number of class objects and returns all their anonymous or reachable descendants as a single array: - - -class C; end -subclasses_of(C) # => [] - -subclasses_of(Integer) # => [Bignum, Fixnum] - -module M - class A; end - class B1 < A; end - class B2 < A; end -end - -module N - class C < M::B1; end -end - -subclasses_of(M::A) # => [N::C, M::B2, M::B1] - - -The order in which these classes are returned is unspecified. The returned collection may have duplicates: - - -subclasses_of(Numeric, Integer) -# => [Bignum, Float, Fixnum, Integer, Date::Infinity, Rational, BigDecimal, Bignum, Fixnum] - - -See also +Class#subclasses+ in "Extensions to +Class+ FIX THIS LINK":FIXME. - -NOTE: Defined in +active_support/core_ext/object/extending.rb+. - h4. Instance Variables Active Support provides several methods to ease access to instance variables. @@ -1141,36 +1107,47 @@ If for whatever reason an application loads the definition of a mailer class and NOTE: Defined in +active_support/core_ext/class/delegating_attributes.rb+. -h4. Descendants +h4. Descendants & Subclasses -h5. +subclasses+ +h5. +descendants+ -The +subclasses+ method returns the names of all the anonymous or reachable descendants of its receiver as an array of strings: +The +descendants+ method returns all classes, including its children, that inherits from self. class C; end -C.subclasses # => [] - -Integer.subclasses # => ["Bignum", "Fixnum"] +C.descendants #=> [] -module M - class A; end - class B1 < A; end - class B2 < A; end -end +class B < C; end +C.descendants #=> [B] -module N - class C < M::B1; end -end +class A < B; end +C.descendants #=> [B, A] -M::A.subclasses # => ["N::C", "M::B2", "M::B1"] +class D < C; end +C.descendants #=> [B, A, D] -The order in which these class names are returned is unspecified. +h5. +subclasses+ + +The +subclasses+ method returns all direct classes that inherits from self. + + +class C; end +C.subclasses #=> [] + +class B < C; end +C.subclasses #=> [B] + +class A < B; end +C.subclasses #=> [B] + +class D < C; end +C.subclasses #=> [B, D] + -See also +Object#subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME. +The order in which these class are returned is unspecified. -WARNING: This method is redefined in some Rails core classes. +WARNING: This method is redefined in some Rails core classes but should be all compatible in Rails 3.1. NOTE: Defined in +active_support/core_ext/class/subclasses.rb+. -- cgit v1.2.3 From 92ff71bb14b1b589a3d5c04d120e4b9210b243b1 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 6 Jul 2010 17:29:19 +0200 Subject: documents automatic management of join models in hmt associations, in particular the gotcha that deletion is direct --- railties/guides/source/association_basics.textile | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/association_basics.textile b/railties/guides/source/association_basics.textile index 335d17579d..c69f2ae8c9 100644 --- a/railties/guides/source/association_basics.textile +++ b/railties/guides/source/association_basics.textile @@ -137,6 +137,16 @@ end !images/has_many_through.png(has_many :through Association Diagram)! +The collection of join models can be managed via the API. For example, if you assign + + +physician.patients = patients + + +new join models are created for newly associated objects, and if some are gone their rows are deleted. + +WARNING: Automatic deletion of join models is direct, no destroy callbacks are triggered. + The +has_many :through+ association is also useful for setting up "shortcuts" through nested +has_many+ associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way: -- cgit v1.2.3 From 0f96cea322294f84df34c43000443b249a880126 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 7 Jul 2010 05:31:54 +0100 Subject: Add note about incompatibility of namespace and :controller --- railties/guides/source/routing.textile | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index 00755071c5..72a76e25bb 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -382,6 +382,12 @@ match ':controller/:action/:id/:user_id' An incoming URL of +/photos/show/1/2+ will be dispatched to the +show+ action of the +PhotosController+. +params[:id]+ will be +"1"+, and +params[:user_id]+ will be +"2"+. +NOTE: You can't use +namespace+ or +:module+ with a +:controller+ path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g: + + +match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/ + + h4. Static Segments You can specify static segments when creating a route: -- cgit v1.2.3