Living on the edge (of Rails) #14 - the extreme edition. Extremely late.

This week’s report covers changes from 24 Mar 2008 to 30 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

There’re lots of big exciting changes this week on edge. Smells like 2.1 soon!

has_finder gem merged into Rails

Ryan Daigle has the scoop on the new has_finder-like functionality in Rails so head on over (I won’t repeat it here in the interests of DRY).

Credit goes to Nick Kallen for coming up with the has_finder gem.

Related changeset: http://dev.rubyonrails.org/changeset/9084

ActiveRecord: Unsaved attribute changes are now tracked

You can now ask an ActiveRecord model whether its attributes have been changed (and is unsaved) using the {attr_name}_changed? (magic) method. The {attr_name}_was method will return the original value of the attribute, and the {attr_name}_change method will return an array with 2 members, the 1st being the original value, the 2nd being the changed (and unsaved) value.

# Change person's name.
person.name = 'Jason Seifer'
person.changed?       # => true
person.name_changed?  # => true
person.name_was       # => 'jseifer'
person.name_change    # => ['jseifer', 'Jason Seifer']
person.name = 'Ceiling Cat'
person.name_change    # => ['jseifer', 'Ceiling Cat']

Credit goes to Rails core team member Jeremy Kemper (bitsweat) for this patch (which originated from Jeremy’s very own Dirty plugin).

Related changeset: http://dev.rubyonrails.org/changeset/9127

ActiveRecord#Base.all/first/last

Now you can do:

Post.all.each { |post| # something }
Post.first
Post.last

Kinda like how some other ORMs do (like DataMapper).

Credit: thechrisoshow and again, Nick Kallen for has_finder.

Related changeset: http://dev.rubyonrails.org/changeset/9085

Timestamped Migrations

This is more useful than it sounds: migrations are now timestamped instead of using incremental numbers in your db/migrate folder. This eliminates the problem of migrations having the same version number when working on a multi-developer Rails app.

Two new Rake tasks, rake db:migrate:up/down, have been added to allow running of the up and down methods of individual migrations.

Credit goes to John Barnette, the guy who gave you foxy fixtures.

Related changeset: http://dev.rubyonrails.org/changeset/9122

New config.gem option for specifying which gems are required by the application + rake tasks for installing and freezing gems

Specify which gems are required by your Rails app in your Rails environment.rb! “Vendor everything” supported right in Rails :). An example:

Rails::Initializer.run do |config|
  config.gems 'chronic'
  config.gems "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
  config.gems "aws-s3", :lib => "aws/s3"
end

There are also new Rake tasks:

# List required gems.
rake gems 

# Install all required gems:
rake gems:install 

# Unpack specified gem to vendor/gems/gem_name-x.x.x
rake gems:unpack GEM=chronic

Credit goes to the prolific Rick Olson for this gem of a patch.

Related changeset: http://dev.rubyonrails.org/changeset/9140

ActiveResource #clone

You can now clone an existing resource:

gregg = Person.find(1)
borg = gregg.clone

This doesn’t clone child ActiveResource members though, just the straight up attributes of resource.

Contribors: Ryan Daigle and thechrisoshow.

Related changeset: http://dev.rubyonrails.org/changeset/9121

Big documentation patch

A big doc patch consisting of all the efforts going on in the doc-rails repository on Github has been committed. Check out Pratik’s post for more info on what doc-rails is. Though with Rails moving to Git soon, doc-rails may no longer be necessary!

Related changeset: http://dev.rubyonrails.org/changeset/9093

Living on the edge (of Rails) #13 - Git script/plugin install, has_one :through

It’s time again for some edge Rails love. If you haven’t listened to it yet, be sure to check out this week’s Rails Envy podcast

This week’s report covers changes from 17 Mar 2008 to 23 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

script/plugin install works for Git repositories

Yup you can now install plugins from Git repositories simply by running:

script/plugin install git://github.com/foo/kung_fu

What this really does is to clone a git repository (sans the entire git history by passing the —depth 1 option) into your plugins directory. If you’re looking for a Piston-like way to manage external Git repositories though, you’d still be better off with the development version of Piston (François Beausoleil, is posting frequently on his progress with adding Git support).

Credit goes to Jack Danger, a long-time Rails contributor for this patch.

Related changeset: http://dev.rubyonrails.org/changeset/9049

has_one :through

It’s been a long time coming - now you can do has_one :through (like has_many :through) for your join models. Ryan Daigle has example code on how to use it if you’re curious (though really, it’s just like has_many).

Credit goes to the Art of Mission guys and Chris O’Sullivan for pulling it all together.

Related changeset: http://dev.rubyonrails.org/changeset/9067

rake tasks time:zones:all, time:zones:us and time:zones:local

Some timezone-related rake tasks have been added to list the names of time zones recognized by the TimeZone class for the config.time_zone option you can put in your environment.rb. For example

rake time:zones:local

* UTC +08:00 *
Beijing
Chongqing
Hong Kong
Irkutsk
Kuala Lumpur
Perth
Singapore
Taipei
Ulaan Bataar
Urumqi

Credit: Geoff Buesing (core team member)

Related changeset: http://dev.rubyonrails.org/changeset/9074

#number_to_currency helper now supports a :format option

Now you can do something like:

number_to_currency(123.50, :unit => "£", :format => "%n %u")
# => 123.50 £

Instead of being stuck with the {currency_code} {amount} format which doesn’t work in some locales (such as “42 pounds”).

Related changeset: http://dev.rubyonrails.org/changeset/9052

Living on the edge (of Rails) #12

This week’s report covers changes from 10 Mar 2008 to 16 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

Custom JavaScript and stylesheet symbols

Remember how you can do something like:

javascript_include_tag :defaults

and Rails would load all the Prototype JavaScript files and your application.js?

You can now register your own custom expansion symbol too:

# In a Rails initializer.
ActionView::Helpers::AssetTagHelper.register_javascript_expansion :yui => ['yahoo', 'autocomplete', 'calendar']

# In your view.
javascript_include_tag :yui

would result in:

<script type="text/javascript" src="/javascripts/yahoo.js"></script>
<script type="text/javascript" src="/javascripts/autocomplete.js"></script>
<script type="text/javascript" src="/javascripts/calendar.js"></script>

You can do the same with the stylesheet_link_tag by registering a custom expansion symbol via register_stylesheet_expansion.

This is useful for anyone but in particular plugin developers who have a multiple asset files would appreciate being able to tell users to include JavaScript or stylesheets using a single symbol.

Warning: This patch currently breaks the default symbols like :all (check out the ticket for more info).

Related changeset: http://dev.rubyonrails.org/changeset/9016

Sexy default timestamps in migrations

Remember how you can say timestamps in a migration and Rails will create the ‘created_at’ and ‘updated_at’ columns for you? You can now also do add_timestamps :table_name and remove_timestamps :table_name in your migrations if you decide to add these columns later to a table:

def self.up
  add_timestamps :posts
end

def self.down
  remove_timestamps :posts
end

Related changeset: http://dev.rubyonrails.org/changeset/9014

ActiveRecord::Base#find(:last)

Just like Comment.find(:first), you can now do something like Comment.find(:last). There’s some controversy over whether this is bloat, but DHH makes a good case for it with this example:

class Person
  has_many :comments, :order => 'created_at'
end

@some_person.comments.find(:last) # => Returns the most recent comment.

Related changeset: http://dev.rubyonrails.org/changeset/9012

Database rake tasks fixes

rake db:create used to ignore the ‘charset’ and ‘collation’ options in your database.yml configuration file. This has been fixed so that your created databases now respect those options.

rake db:drop and rake db:migrate:reset also no longer crash with an unhelpful exception if the database has already been dropped, and instead shows a proper error message.

Related changeset: http://dev.rubyonrails.org/changeset/9004

Rails’ logger now creates the log directory if it doesn’t exist

This is a blessing to those of us who use version control systems that don’t support empty directories (like Git). Rails’ default logger (the BufferedLogger), now creates a log/ directory if it doesn’t already exist. This should save you the step of creating/symlinking a log/ directory (or symlinking) on deploy.

Related changeset: http://dev.rubyonrails.org/changeset/9013

String#squish is faster

A faster implementation of the String#squish (and String#squish!) core extension has been committed.

Related changeset: http://dev.rubyonrails.org/changeset/9015

The #excerpt TextHelper no longer includes 1 character too many

Turns out that the #excerpt helper method was consistently including an extra character. This has been fixed.

Related changeset: http://dev.rubyonrails.org/changeset/9030

As usual, let me know of any inaccuracies or any suggestions you may have in the comments!

Living on the edge (of Rails) #11

This week’s report covers changes from 3 Mar 2008 to 9 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

Improve performance on :include/:conditions/:limit queries by selectively joining in the pre-query

This is another ActiveRecord performance boost related to the pre-loading any eager-loaded :includes mentioned previously. Basically what this patch does is to only join referenced tables when needed. It does this by checking the :conditions and :limit options to determine whether a table should be joined or can be left out and pre-loaded instead. You can find out more in tickets #9560 and #9497.

Credit goes to Gabe da Silveira (dasil003 on Trac) for this awesome performance patch.

Related changeset: http://dev.rubyonrails.org/changeset/8977

Better error message for type errors when parsing request parameters

Now when you pass an incorrectly formed request (e.g. GET, POST) parameter to your controller, it will raise an exception that includes a friendlier error message that indicates exactly what you passed to it. This is helpful when trying to debug whether you constructed your form correctly.

Contributors: Chad Humphries and matt.

Related changeset: http://dev.rubyonrails.org/changeset/8986

Make MimeResponds::Responder#any work without explicit types.

Here’s something I didn’t know: you can actually use any in your respond_to blocks as a catch-all response. E.g.

respond_to do |format|
  format.html do
    redirect_to :action => 'login'
  end
  format.any(:js, :xml) do
    request_http_basic_authentication 'Web Password'
  end
end

Contrary to its name, using ‘any’ actually requires you to pass a list of types to respond to. This has been enhanced now so that if you don’t pass any arguments, it’ll function as a real catch-all.

respond_to do |format|
  format.any do
    request_http_basic_authentication 'Web Password'
  end
end

Credit goes to Joshua Wehner for this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8987

Add readonly option to has_many :through associations

Turns out the :readonly option for associations mentioned earlier was left out for has_many :through associations. This oversight has been fixed.

Thanks to Emilio Tagua for this bugfix.

Related changeset: http://dev.rubyonrails.org/changeset/8989

As usual, let me know of any inaccuracies or any suggestions you may have in the comments!

Living on the edge (of Rails) #10

Another awfully sleepy week on Rails edge. Though by the time I had sent over the notes to Gregg Pollack and Jason Seifer of the awesome Rails Envy podcast, there has been some nice changes (that I’ll be mentioning next week, but there’s absolutely nothing stopping you from checking those out yourself).

In other interesting news, Pratik Naik (lifofifo), a long-time Rails contributor has been given commit rights to Rails. Congrats Pratik, well-deserved and it has been long overdue in my opinion! Pratik keeps an interesting blog at http://m.onkey.org/ (where he’s not afraid to say “fuck” in his posts and his code) and hangs out an awful lot on #rubyonrails and #rails-contrib on IRC.

As a sidenote, Capistrano 2.2.0 was released by Jamis last week.

This week’s report covers changes from 25 Feb 2008 to 2 Mar 2008 (the day the corresponding Rails Envy podcast was recorded).

Time#end_of_XXX methods

A bunch of Time core extension methods have been added. These are: Time#end_of_day, Time#end_of_week, Time#end_of_year, and Time#end_of_quarter, which all return exactly what you expect them to return:

Time.now.end_of_week # => Sun Mar 09 00:00:00 0800 2008

Credit goes to Juanjo Bazán (a former Rails Hackfest winner) and Tarmo Tänav for contributing this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8934

Date helpers now accept HTML options

ActionView’s date helpers (such as date_select, time_select, select_datetime) did not support any HTML options, unlike the other helpers (like f.text_field(:name, :class => 'my_css_class', :size => 20)). This inconsistency has been fixed and you can now finally do:

<%= date_select 'user', 'birthday', :order => [:day], :class => 'my_css_class' %>

Murray Steele (h-lame on the Rails Trac) and Jakob Skjerning contributed this patch.

Related changeset: http://dev.rubyonrails.org/changeset/8968

No need for explicit respond_to for RJS templates

ActionController has been changed so that JS requests will automatically render action.js.rjs files without the need to specify an explicit respond_to block. This means that your .rjs files work the same way as your .html.erb files - just put them in the right place and Rails will use it.

Related changeset: http://dev.rubyonrails.org/changeset/8956

Bugfixes

  • http://dev.rubyonrails.org/changeset/8937 - Prevent Rails from crashing when trying to deserialize an XML representation of a model named “Type” (using Hash#from_xml). Contributed by Juanjo Bazán and Isaac Feliu.
  • http://dev.rubyonrails.org/changeset/8942 - Fix eager loading so that it doesn’t pull in duplicate records in some cases. Contributed by Catfish.

As usual, let me know of any inaccuracies or any suggestions you may have in the comments!