April 13th, 2008
A slight change this week since Rails has moved to Git - all changesets are now referenced to Github.
This week’s report covers changes from 7 Apr 2008 to 13 Apr 2008 (the day the corresponding Rails Envy podcast was recorded).
A helpers proxy method for accessing helper methods from outside of views
A helpers method has been introduced to ActionController - you can now access your helpers like so:
ApplicationController.helpers.simple_format(text)
Credit goes to 19 year old Josh Peek, one of the more prolific Rails contributors (and most likely to join Pratik in Rails Core imo).
Related changeset: http://github.com/rails/rails/commit/917423d664038d6791738a73ad1446437dbb71df
json_escape for escaping HTML entities in JSON strings
A json_escape (aliased as j, similar to how html_escape is aliased as h) has been introduced by Rick Olson for escaping HTML entities in JSON strings. This is useful if you find yourself emitting JSON strings in HTML pages (e.g. for documenting a JSON API):
json_escape("is a > 0 & a < 10?")
# => is a \u003E 0 \u0026 a \u003C 10?
Of course, since json_escape is aliased as j, you can do this in your ERB templates:
<%=j @person.to_json %>
Related changeset: http://github.com/rails/rails/commit/0ff7a2d89fc95dcb0a32ed92aab7156b0778a7ea
Automatically parse posted JSON content for Mime::JSON requests
Rails will now accept POSTed JSON content! For example, you can now send a POST to “/posts” of your Post resource like this:
POST /posts
{"post": {"title": "Breaking News"}}
And your create action will automatically parse the POSTed JSON into the request params so that this code below just works:
def create
@post = Post.create params[:post]
end
Credit goes to Rick Olson.
Related changeset: http://github.com/rails/rails/commit/4d594cffcfc93b37fad4e423ec8593299e50133c
schema_migrations table replaces the schema_info table, allowing for interleaved migrations
A new schema_migrations table for storing which migrations have been run has been introduced, and this replaces the existing schema_info table (which simply keeps track of the last applied schema version). This change makes it possible to add migrations (for example, after you’ve merged from someone’s branch) - when you migrate your database via rake db:migrate, those never-applied “interleaved” migrations will be run. Similarly, when migrating down schema versions, never-applied “interleaved” will be skipped.
See http://dev.rubyonrails.org/ticket/11493 for more information.
This useful patch is the brainchild of Jordi Bunster (WWR profile). I’m sure many Rails developers who branch extensively will thank Jordi for this excellent patch!
Related changeset: http://github.com/rails/rails/commit/8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e
gems:unpack:dependencies Rake task
Remember how you can now run the gems:unpack Rake task to unpack a gem into your vendor/gems/ directory (this was mentioned in an earlier Living on the Edge)?
David Dollar (Github profile) has firmed up that patch quite a bit by adding the gems:unpack:dependencies Rake task that unpacks the dependencies of the gems as well. So running:
gems:unpack:dependencies GEM=activecouch
would unpack the full Rubygem dependency tree of gems that the activecouch gem depends upon.
Related changeset: http://github.com/rails/rails/commit/4364c361b599f99bc2345ce4eb2d145b07ed8a0f
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series! Now go out there and start forking Rails!
April 6th, 2008
I’m gonna post up the Living on the Edge a little earlier this week because I’ve found I just can’t keep up (when things get busy) with 2 separate sets of notes for the Rails Envy podcast and another for my own blog posts.
From this week onwards, I’ll be following Pratik’s suggestion to post about changes as they happen instead of after an entire week so expect a slight change in the format of Living on the edge for next week!
This week’s report covers changes from 31 Mar 2008 to 6 Apr 2008 (the day the corresponding Rails Envy podcast was recorded).
Rails.env, Rails.logger, Rails.cache, Rails.root
This has been a long time coming: You can now use Rails.env instead of RAILS_ENV, Rails.root instead of RAILS_ROOT, Rails.logger instead of RAILS_DEFAULT_LOGGER, and Rails.cache instead of RAILS_CACHE. Not having to use constants is nice because it always felt a bit dirty to have so many hard-coded constants in the Rails namespace.
Credit goes to Pratik Naik, the newest Rails core team member for this cleanup.
Related changeset: http://dev.rubyonrails.org/changeset/9180
Render :partial now works with a collection of heterogeneous elements
You can now use render :partial to render a collection of different elements and Rails will figure out which partial template to use. For example:
render :partial => [ monkey, donkey, dramatic_chipmunk ]
will render the monkey with the ‘monkeys/_monkey.html.erb’ partial, donkey with the ‘donkeys/_donkey.html.erb’ partial, and last but definitely not lest, dramatic_gopher with the ‘dramatic_chipmunks/_dramatic_chipmunk.html.erb’ partial
This is especially useful if when using STI, where your ActiveRecord model would return objects of different types. The collection [ monkey, donkey, dramatic_chipmunk ] could very well be something returned from your Animal model that has subclasses of Monkey, Donkey and DramaticChipmunk via STI.
Credit: Zach Dennis of ar-extensions fame.
Related changeset: http://dev.rubyonrails.org/changeset/9177
ActiveModel work revived
It seems like work on ActiveModel (think unified ActiveRecord and ActiveResource behaviors) has been revived, if the latest commits (http://dev.rubyonrails.org/changeset/9171 and http://dev.rubyonrails.org/changeset/9173) by David (DHH) is anything to go by.
Bugfix: Assigning an invalid has_one association now causes #save to fail on parent
You could do this before:
firm = Firm.find(:first)
firm.account = Account.new # Associate INVALID account
firm.save # This doesn't fail
Now that’s fixed thanks to Pratik Naik.
Related changeset: http://dev.rubyonrails.org/changeset/9232
As usual, any feedback is respectfully appreciated!
April 5th, 2008
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
Quick notes for installing libmemcached and Evan Weaver’s memcached gem, in case I forget (again):
- Get libmemcached source from http://tangent.org/552/libmemcached.html. Configure, make, make install. Make sure you get a version compatible with the Ruby memcached gem by opening the COMPATIBILITY file in the memcached gem files.
gem install memcached
- Create a symlink
ln -s /usr/local/lib/libmemcached.so.2 /usr/lib/.
- Try it out with
irb -rubygems, require 'memcached'
(0)
March 27th, 2008
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