@@ -6,6 +6,62 @@ By referring to this guide you will learn the extensions to the Ruby core classe
endprologue.
h3. How to Load Core Extensions
In order to have a near zero default footprint, Active Support does not load anything by default. It is broken in small pieces so that you may load just what you need, and also has some convenience entry points to load related extensions in one shot, even everything.
Thus, after a simple require like:
<ruby>
require 'active_support'
</ruby>
objects do not even respond to +blank?+, let's see how to load its definition.
h4. Cherry-picking a Definition
The most lightweight way to get +blank?+ is to cherry-pick the file that defines it.
For every single method defined as a core extension this guide has a note that says where is such a method defined. In the case of +blank?+ the note reads:
NOTE: Defined in +active_support/core_ext/object/blank.rb+.
That means that this single call is enough:
<ruby>
require 'active_support/core_ext/object/blank'
</ruby>
Active Support has been carefully revised so that cherry-picking a file loads only strictly needed dependencies, if any.
h4. Loading Grouped Core Extensions
The next level is to simply load all extensions to +Object+. As a rule of thumb, extensions to +SomeClass+ are available in one shot by loading +active_support/core_ext/some_class+.
Thus, if that would do, to have +blank?+ available we could just load all extensions to +Object+:
<ruby>
require 'active_support/core_ext/object'
</ruby>
h4. Loading All Core Extensions
You may prefer just to load all core extensions, there is a file for that:
<ruby>
require 'active_support/core_ext'
</ruby>
h4. Loading All Active Support
And finally, if you want to have all Active Support available just issue:
<ruby>
require 'active_support/all'
</ruby>
That does not even put the entire Active Support in memory upfront indeed, some stuff is configured via +autoload+, so it is only loaded if used.
h3. Extensions to All Objects
h4. +blank?+ and +present?+
...
...
@@ -1249,6 +1305,338 @@ The call +str.last(n)+ is equivalent to +str.from(-n)+ if +n+ > 0, and returns a
NOTE: Defined in +active_support/core_ext/string/access.rb+.
h4. Inflections
h5. +pluralize+
The method +pluralize+ returns the plural of its receiver:
<ruby>
"table".pluralize # => "tables"
"ruby".pluralize # => "rubies"
"equipment".pluralize # => "equipment"
</ruby>
As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Builtin rules can be extended in +config/initializers/inflections.rb+. That file is generated by the +rails+ command and has instructions in comments.
Active Record uses this method to compute the default table name that corresponds to a model:
and understands strings that start with lowercase:
<ruby>
"visualEffect".underscore # => "visual_effect"
</ruby>
+underscore+ accepts no argument though.
Rails class and module autoloading uses +underscore+ to infer the relative path without extension of a file that would define a given missing constant:
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
h5. +parameterize+
The method +parameterize+ normalizes its receiver in a way that can be used in pretty URLs.
<ruby>
"John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"
</ruby>
In fact, the result string is wrapped in an instance of +ActiveSupport::Multibyte::Chars+.
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
h5. +tableize+
The method +tableize+ is +underscore+ followed by +pluralize+.
<ruby>
"Person".tableize # => "people"
"Invoice".tableize # => "invoices"
"InvoiceLine".tableize # => "invoice_lines"
</ruby>
As a rule of thumb, +tableize+ returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight +tableize+ indeed, because it also demodulizes de class name and checks a few options that may affect the returned string.
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
h5. +classify+
The method +classify+ is the inverse of +tableize+. It gives you the class name corresponding to a table name:
Note that +classify+ returns a class name as a string. You can get the actual class object invoking +constantize+ on it, explained next.
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
h5. +constantize+
The method +constantize+ resolves the constant reference expression in its receiver:
<ruby>
"Fixnum".constantize # => Fixnum
module M
X = 1
end
"M::X".constantize # => 1
</ruby>
If the string evaluates to no known constant, or its content is not even a valid constant name, +constantize+ raises +NameError+.
Constant name resolution by +constantize+ starts always at the top-level +Object+ even if there is no leading "::".
<ruby>
X = :in_Object
module M
X = :in_M
X # => :in_M
"::X".constantize # => :in_Object
"X".constantize # => :in_Object (!)
end
</ruby>
So, it is in general not equivalent to what Ruby would do in the same spot, had a real constant be evaluated.
Mailer test cases obtain the mailer being tested from the name of the test class using +constantize+:
<ruby>
# action_mailer/test_case.rb
def determine_default_mailer(name)
name.sub(/Test$/, '').constantize
rescue NameError => e
raise NonInferrableMailerError.new(name)
end
</ruby>
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
h5. +humanize+
The method +humanize+ gives you a sensible name for display out of an attribute name. To do so it replaces underscores with spaces, removes any "_id" suffix, and capitalizes the first word:
<ruby>
"name".humanize # => "Name"
"author_id".humanize # => "Author"
"comments_count".humanize # => "Comments count"
</ruby>
The helper method +full_messages+ uses +humanize+ as a fallback to include attribute names:
* All images are in the railties/guides/images directory.
* Sample format : "Active Record Associations":http://github.com/lifo/docrails/blob/3e56a3832415476fdd1cb963980d0ae390ac1ed3/railties/guides/source/association_basics.textile
* Sample output : "Active Record Associations":http://guides.rails.info/association_basics.html
* You can build the Guides during testing by running +rake guides+ in the +railties+ directory.
* You can build the Guides during testing by running +rake generate_guides+ in the +railties+ directory.