60
What’s new in Rails 2.1? Keith Pitty Sydney Rails Meetup 11 June 2008

What\'s new in Rails 2.1

Embed Size (px)

Citation preview

Page 1: What\'s new in Rails 2.1

What’s new in Rails 2.1?

Keith Pitty

Sydney Rails Meetup11 June 2008

Page 2: What\'s new in Rails 2.1
Page 3: What\'s new in Rails 2.1

over 1,400 contributors

Page 4: What\'s new in Rails 2.1

more than 1,600 patches

Page 5: What\'s new in Rails 2.1

• Time zones

• Dirty tracking

• Gem dependencies

• Named scope

• UTC-based migrations

• Better caching

Major features

Page 6: What\'s new in Rails 2.1

Time zones

New!

courtesy of Geoff Busing

Page 7: What\'s new in Rails 2.1

$ rake -T time(in /Users/keithpitty/work/rails2.1demo)rake time:zones:all # Displays names of all time zones recognized by the...rake time:zones:local # Displays names of time zones recognized by the Rai...rake time:zones:us # Displays names of US time zones recognized by the ...

Page 8: What\'s new in Rails 2.1

$ rake time:zones:local

* UTC +10:00 *BrisbaneCanberraGuamHobartMelbournePort MoresbySydneyVladivostok

Page 9: What\'s new in Rails 2.1

# config/environment.rb config.time_zone = 'Sydney'

Page 10: What\'s new in Rails 2.1

Times now stored in UTC in the dbbut displayed using the default time zone

Page 11: What\'s new in Rails 2.1

If upgrading, times will need to be converted to UTC

Warning!

Page 12: What\'s new in Rails 2.1

Add time zone support to users

Example

Page 13: What\'s new in Rails 2.1

Add time_zone to User model

Page 14: What\'s new in Rails 2.1

Add time_zone_select to signup view

Page 15: What\'s new in Rails 2.1

<h1>Sign up as a new user</h1><% @user.password = @user.password_confirmation = nil %>

<%= error_messages_for :user %><% form_for :user, :url => users_path do |f| -%><p><label for="login">Login</label><br/><%= f.text_field :login %></p>

<p><label for="email">Email</label><br/><%= f.text_field :email %></p>

<p><label for="password">Password</label><br/><%= f.password_field :password %></p>

<p><label for="password_confirmation">Confirm Password</label><br/><%= f.password_field :password_confirmation %></p>

<p><label for="time_zone">Time zone</label><br/><%= f.time_zone_select :time_zone, TimeZone.all %>

<p><%= submit_tag 'Sign up' %></p><% end -%>

Page 16: What\'s new in Rails 2.1

Use before_filter to set time zonein ApplicationController

Page 17: What\'s new in Rails 2.1

class ApplicationController < ActionController::Base helper :all protect_from_forgery include AuthenticatedSystem before_filter :set_user_time_zone private def set_user_time_zone Time.zone = current_user.time_zone if logged_in? end end

Page 18: What\'s new in Rails 2.1

Dirty tracking

New!

Page 19: What\'s new in Rails 2.1

$ ./script/consoleLoading development environment (Rails 2.1.0)>> Beer.find :first=> #<Beer id: 1, name: "Coopers Sparkling Ale", created_at: "2008-06-05 11:31:55", updated_at: "2008-06-05 11:31:55">>> b.changed?=> false>> b.name = "Coopers Pale Ale"=> "Coopers Pale Ale">> b.changed?=> true>> b.changed=> ["name"]>> b.name_changed?=> true>> b.name_was=> "Coopers Sparkling Ale">> b.changes=> {"name"=>["Coopers Sparkling Ale", "Coopers Pale Ale"]}>> b.save=> true>> b.changed?=> false

Page 20: What\'s new in Rails 2.1

Partial updates by default in new Rails 2.1 apps

Page 21: What\'s new in Rails 2.1

SQL (0.000138) BEGIN Beer Update (0.000304) UPDATE `beers` SET `updated_at` = '2008-06-05 11:38:01', `name` = 'Coopers Pale Ale' WHERE `id` = 1 SQL (0.005019) COMMIT

Page 22: What\'s new in Rails 2.1

Updates via other than attr= writer

Warning!

Page 23: What\'s new in Rails 2.1

>> b.name_will_change!=> "Coopers Pale Ale">> b.name.upcase!=> "COOPERS PALE ALE">> b.name_change=> ["Coopers Pale Ale", "COOPERS PALE ALE"]

Page 24: What\'s new in Rails 2.1

Gem Dependencies

New!

Page 25: What\'s new in Rails 2.1

$ rake -T gem(in /Users/keithpitty/work/rails2.1demo)rake gems # List the gems that this rails application ...rake gems:build # Build any native extensions for unpacked gemsrake gems:install # Installs all required gems for this applic...rake gems:unpack # Unpacks the specified gem into vendor/gems.rake gems:unpack:dependencies # Unpacks the specified gems and its depende...rake rails:freeze:gems # Lock this application to the current gems ...

Page 26: What\'s new in Rails 2.1

# environment.rb

# Bootstrap the Rails environment, frameworks, and default configurationrequire File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config| config.time_zone = 'Sydney' config.action_controller.session = { :session_key => '_rails2.1demo_session', :secret => 'f4a5c1596f8fff01033b7417629c47fb6344748864b8f3a25fc6884b1ea41d214642f8eba01aa274ef9d1281fc6cdc23bc33ea57ac66878a7b1164b96d875343' } config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" config.gem "aws-s3", :lib => "aws/s3"end

Page 27: What\'s new in Rails 2.1

$ rake gems(in /Users/keithpitty/work/rails2.1demo)[I] hpricot = 0.6[ ] aws-s3

I = InstalledF = Frozen

Page 28: What\'s new in Rails 2.1

$ rake gems:install

Page 29: What\'s new in Rails 2.1

$ rake gems:unpack

Page 30: What\'s new in Rails 2.1

$ rake gems:unpack GEM=hpricot

Page 31: What\'s new in Rails 2.1

$ rake gems:unpack:dependencies

Page 32: What\'s new in Rails 2.1

$ rake gems:build

Page 33: What\'s new in Rails 2.1

# test.rbconfig.gem "mocha"

Page 34: What\'s new in Rails 2.1

$ rake gems RAILS_ENV='test'(in /Users/keithpitty/work/rails2.1demo)[I] hpricot = 0.6[ ] aws-s3 [ ] mocha

I = InstalledF = Frozen

Page 35: What\'s new in Rails 2.1

Named scope

New!

courtesy of Nick Kallen

Page 36: What\'s new in Rails 2.1

class Beer < ActiveRecord::Base named_scope :available, :conditions => {:available => true} named_scope :unavailable, :conditions => {:available => false} named_scope :heavy, :conditions => {:full_strength => true} named_scope :light, :conditions => {:full_strength => false} named_scope :cats_piss, :conditions => {:rating => 0..3} named_scope :drinkable, :conditions => ['rating > 3'] named_scope :minimum_rating, lambda { |min_rating| { :conditions => ['rating >= ?', min_rating] } } named_scope :rated, lambda { |rating| { :conditions => ['rating = ?', rating] } } named_scope :awesome, lambda { |*args| { :conditions => ['rating >= ?', (args.first || 9)] } }end

Page 37: What\'s new in Rails 2.1

>> Beer.count=> 15>> Beer.available.count=> 9>> Beer.heavy.available.count=> 8>> Beer.heavy.available.drinkable.count=> 6>> Beer.heavy.available.minimum_rating(7).count=> 5>> Beer.heavy.available.awesome.count=> 3>> Beer.heavy.available.awesome(10).count=> 1

Page 38: What\'s new in Rails 2.1

>> Beer.heavy.available.awesome(10)=> [#<Beer id: 888319404, name: "Coopers Sparkling Ale", brewer: "Coopers", rating: 10, available: true, full_strength: true, created_at: "2008-06-08 07:13:17", updated_at: "2008-06-08 07:13:17">]

Page 39: What\'s new in Rails 2.1

>> Beer.rated(0)=> [#<Beer id: 177722761, name: "Fosters Lager", brewer: "Fosters", rating: 0, available: false, full_strength: true, created_at: "2008-06-08 07:13:17", updated_at: "2008-06-08 07:13:17">]

Page 40: What\'s new in Rails 2.1

Also: named scope extensions, anonymous scopes

Page 41: What\'s new in Rails 2.1

UTC-based migrations

New!

Page 42: What\'s new in Rails 2.1
Page 43: What\'s new in Rails 2.1

Migration prefix no longer simply sequential

Page 44: What\'s new in Rails 2.1

Migration prefix now uses UTC timestamp

Page 45: What\'s new in Rails 2.1

mysql> select * from schema_migrations;+----------------+| version |+----------------+| 20080604194357 | | 20080604195453 | | 20080605112923 | +----------------+3 rows in set (0.00 sec)

Page 46: What\'s new in Rails 2.1

Especially helpful for teams (avoids most conflicts)

Page 47: What\'s new in Rails 2.1

Also helpful when working on branches

Page 48: What\'s new in Rails 2.1

Migrations are applied intelligently

Page 49: What\'s new in Rails 2.1

Also: change_table method introduced to migrations

Page 50: What\'s new in Rails 2.1

Better caching

New!

Page 51: What\'s new in Rails 2.1

Specify preferred caching engine in environment.rb

Page 52: What\'s new in Rails 2.1

# Following options supplied in Rails 2.1: ActionController::Base.cache_store = memory_store ActionController::Base.cache_store = file_store, "path/to/cache/directory" ActionController::Base.cache_store = drb_store, "druby://localhost:9192" ActionController::Base.cache_store = mem_cache_store, "localhost"

Page 53: What\'s new in Rails 2.1

Or subclass ActiveSupport::Cache::Store and implement read, write, delete and delete_matched methods

Page 54: What\'s new in Rails 2.1

A few other tidbits

New!

Page 55: What\'s new in Rails 2.1

rake rails:freeze:edge RELEASE=1.2.0

Page 56: What\'s new in Rails 2.1

script/plugin installsupports

-e for svn exportand plugins hosted in Git repositories

Page 57: What\'s new in Rails 2.1

script/dbconsole

Page 58: What\'s new in Rails 2.1

>> " A string full of white spaces ".squish=> "A string full of white spaces"

Page 60: What\'s new in Rails 2.1

Thanks for listening

http://keithpitty.com