Living on the edge (of Rails) #12

In: Edge Rails|Open Source|Ruby|Ruby on Rails

20 Mar 2008

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!

4 Responses to Living on the edge (of Rails) #12

Avatar

Kamal Fariz

March 20th, 2008 at 9pm

I think that example should read


javascript_include_tag :yui

rather than :monkey :)

Avatar

Chu Yeow

March 20th, 2008 at 10pm

Ugh you’re right. Thanks for pointing that out!

Avatar

A Fresh Cup » Blog Archive » Double Shot #170

March 21st, 2008 at 7pm

[…] Living on the edge (of Rails) #12 – More incremental progress is coming our way. […]

Avatar

Jim D

March 22nd, 2008 at 2am

Weird that people would argue :last is bloat. I feel like I assume it’s already been implemented at least once a month and stick it in my code before kicking myself when I remember it hasn’t been. The use case of wanting to pull the most recent record is obvious, to me anyway.