May 4th, 2008
This week’s report covers changes from 29 April 2008 to 4th May 2008 (the day the corresponding Rails Envy podcast was recorded).
change_table for ActiveRecord migrations
Thanks to Jeff Dean, who also blogged about the new change_table feature in ActiveRecord migrations, you can now change a table with a block like so:
change_table :videos do |t|
t.add_timestamps
t.add_belongs_to :goat
t.add_string :name, :email, :limit => 20
t.remove_column :name, :email # takes multiple arguments
t.rename :new_name
t.string :new_string_column # executes against the renamed table name
end
Some key things to note:
add_XXX would add a new column for you, e.g. add_string would add a new string field.
- Of course, add_timestamps would add the magic
created_at and updated_at datetime fields.
remove_column now takes multiple arguments.
rename would rename the table.
Very nice, DRY enhancement, props to Jeff Dean once again.
Related changeset: http://github.com/rails/rails/commit/96980bd561d79824b6cb6efbcbecdcbf8785d452
ActiveRecord::Base.create takes a block like ActiveRecord::Base.new
Yup now you can also create ActiveRecord objects with a block argument just like you could for ActiveRecord::Base.new:
@person = Person.create(params[:person]) do |p|
p.name = 'Konata Izumi'
p.age = 17
end
Credit goes to Adam Meehan for this patch.
Related changeset: http://github.com/rails/rails/commit/dd120ede53eaf71dee76894998a81626b7a689fc
Bugfix: change_column should be able to use :null => true on a field that
formerly had false
You can now use change_column in your migrations to alter a column as nullable if it was previously NOT NULL.
This bugfix is courtesy of Nate Wiger.
Related changeset: http://github.com/rails/rails/commit/10ef65a3b054270ed3d458ec8eb7c2b9a3e638f7
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.
April 27th, 2008
This week’s report covers changes from 21 Apr 2008 to 27 Apr 2008 (the day the corresponding Rails Envy podcast was recorded).
Not much interesting to report this week - there were mostly a bunch of bugfixes and Ruby 1.8.7-compatibility commits.
Introduce ActiveResource::Base.timeout and rescuing from Timeout::Error in ActiveResource::Connection
These are 2 changes that I’d talked about earlier this week so I won’t repeat myself - take a read through ActiveResource timeouts and why it matters.
Smart integer datatype for the MySQL adapter in migrations
The MySQL adapter in Rails now maps the integer column type in your migrations to either smallint, int, or bigint depending on the :limit option.
This means that a migration like this:
def self.up
create_table :searches do |t|
t.integer :foo, :limit => 2
end
will create your foo column as a smallint(2) MySQL datatype (instead of int(2) before). (More information MySQL numeric datatypes.)
Credit goes to DHH for this patch.
Related changeset: http://github.com/rails/rails/commit/a37546517dad9f6d9a7de6e1dba4d960909d71e8
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.
April 20th, 2008
Not much going on this week on edge Rails. It does seem however that we do have a new Rails core member, Joshua Peek. Also, the new Rails’ bug tracker hosted on Lighthouse is ready for use, so be sure to submit your patches and bug reports there.
This week’s report covers changes from 14 Apr 2008 to 20 Apr 2008 (the day the corresponding Rails Envy podcast was recorded).
Conditional caches_page
The caches_page now takes an :if option for specifying when a page can actually be cached via a Proc. You can now do this, for example:
caches_page :index, :if => Proc.new { |c| !c.request.format.json? }
That will only cache your index page if the requested format is not JSON.
Credit goes to Paul Horsfall for this enhancement.
Related changeset: http://github.com/rails/rails/commit/14a40804a29a57ad05ca6bffbe1e5334089593a9
New ActionView::TestCase for testing view helpers
Remember how you can now use specialized TestCase classes for testing controllers and ActionMailer classes? Now you can do the same with your Rails view helpers with the new ActionView::TestCase class.
Here’s a quick example:
module PeopleHelper
def title(text)
content_tag(:h1, text)
end
def homepage_path
people_path
end
end
class PeopleHelperTest < ActionView::TestCase
def setup
ActionController::Routing::Routes.draw do |map|
map.people 'people', :controller => 'people', :action => 'index'
map.connect ':controller/:action/:id'
end
end
def test_title
assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails")
end
def test_homepage_path
assert_equal "/people", homepage_path
end
Credit goes to Josh Peek for this sweet little enhancement.
ActiveSupport::Cache’s mem_cache_store accepts options
Even though Memcache-client was added to ActiveSupport recently, it didn’t allow you to specify any configuration options beyond just the IP of the memcached server. Now you can pass along more configuration options like so:
config.action_controller.fragment_cache_store = :mem_cache_store, 'localhost', { :compression => true, :debug => true, :namespace => 'foo' }
This patch is courtesy of Jonathan Weiss.
Related changeset: http://github.com/rails/rails/commit/9e1d506a8cfedef2fdd605e4cbf4bf53651ad214
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.
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!