What\'s new in Rails 2.1

3,036 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,036
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

What\'s new in Rails 2.1

  1. 1. What’s new in Rails 2.1? Keith Pitty Sydney Rails Meetup 11 June 2008
  2. 2. over 1,400 contributors
  3. 3. more than 1,600 patches
  4. 4. Major features • Time zones • Dirty tracking • Gem dependencies • Named scope • UTC-based migrations • Better caching
  5. 5. New! Time zones courtesy of Geoff Busing
  6. 6. $ 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 ...
  7. 7. $ rake time:zones:local * UTC +10:00 * Brisbane Canberra Guam Hobart Melbourne Port Moresby Sydney Vladivostok
  8. 8. # config/environment.rb config.time_zone = 'Sydney'
  9. 9. Times now stored in UTC in the db but displayed using the default time zone
  10. 10. Warning! If upgrading, times will need to be converted to UTC
  11. 11. Example Add time zone support to users
  12. 12. Add time_zone to User model
  13. 13. Add time_zone_select to signup view
  14. 14. <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=quot;loginquot;>Login</label><br/> <%= f.text_field :login %></p> <p><label for=quot;emailquot;>Email</label><br/> <%= f.text_field :email %></p> <p><label for=quot;passwordquot;>Password</label><br/> <%= f.password_field :password %></p> <p><label for=quot;password_confirmationquot;>Confirm Password</ label><br/> <%= f.password_field :password_confirmation %></p> <p><label for=quot;time_zonequot;>Time zone</label><br/> <%= f.time_zone_select :time_zone, TimeZone.all %> <p><%= submit_tag 'Sign up' %></p> <% end -%>
  15. 15. Use before_filter to set time zone in ApplicationController
  16. 16. 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
  17. 17. New! Dirty tracking
  18. 18. $ ./script/console Loading development environment (Rails 2.1.0) >> Beer.find :first => #<Beer id: 1, name: quot;Coopers Sparkling Alequot;, created_at: quot;2008-06-05 11:31:55quot;, updated_at: quot;2008-06-05 11:31:55quot;> >> b.changed? => false >> b.name = quot;Coopers Pale Alequot; => quot;Coopers Pale Alequot; >> b.changed? => true >> b.changed => [quot;namequot;] >> b.name_changed? => true >> b.name_was => quot;Coopers Sparkling Alequot; >> b.changes => {quot;namequot;=>[quot;Coopers Sparkling Alequot;, quot;Coopers Pale Alequot;]} >> b.save => true >> b.changed? => false
  19. 19. Partial updates by default in new Rails 2.1 apps
  20. 20. 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
  21. 21. Warning! Updates via other than attr= writer
  22. 22. >> b.name_will_change! => quot;Coopers Pale Alequot; >> b.name.upcase! => quot;COOPERS PALE ALEquot; >> b.name_change => [quot;Coopers Pale Alequot;, quot;COOPERS PALE ALEquot;]
  23. 23. New! Gem Dependencies
  24. 24. $ 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 gems rake 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 ...
  25. 25. # environment.rb # Bootstrap the Rails environment, frameworks, and default configuration require 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 => 'f4a5c1596f8fff01033b7417629c47fb6344748864b8f3a25fc6884b1ea41d214642f8eba01aa274ef9d128 1fc6cdc23bc33ea57ac66878a7b1164b96d875343' } config.gem quot;hpricotquot;, :version => '0.6', :source => quot;http://code.whytheluckystiff.netquot; config.gem quot;aws-s3quot;, :lib => quot;aws/s3quot; end
  26. 26. $ rake gems (in /Users/keithpitty/work/rails2.1demo) [I] hpricot = 0.6 [ ] aws-s3 I = Installed F = Frozen
  27. 27. $ rake gems:install
  28. 28. $ rake gems:unpack
  29. 29. $ rake gems:unpack GEM=hpricot
  30. 30. $ rake gems:unpack:dependencies
  31. 31. $ rake gems:build
  32. 32. # test.rb config.gem quot;mochaquot;
  33. 33. $ rake gems RAILS_ENV='test' (in /Users/keithpitty/work/rails2.1demo) [I] hpricot = 0.6 [ ] aws-s3 [ ] mocha I = Installed F = Frozen
  34. 34. New! Named scope courtesy of Nick Kallen
  35. 35. 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
  36. 36. >> 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
  37. 37. >> Beer.heavy.available.awesome(10) => [#<Beer id: 888319404, name: quot;Coopers Sparkling Alequot;, brewer: quot;Coopersquot;, rating: 10, available: true, full_strength: true, created_at: quot;2008-06-08 07:13:17quot;, updated_at: quot;2008-06-08 07:13:17quot;>]
  38. 38. >> Beer.rated(0) => [#<Beer id: 177722761, name: quot;Fosters Lagerquot;, brewer: quot;Fostersquot;, rating: 0, available: false, full_strength: true, created_at: quot;2008-06-08 07:13:17quot;, updated_at: quot;2008-06-08 07:13:17quot;>]
  39. 39. Also: named scope extensions, anonymous scopes
  40. 40. New! UTC-based migrations
  41. 41. Migration prefix no longer simply sequential
  42. 42. Migration prefix now uses UTC timestamp
  43. 43. mysql> select * from schema_migrations; +----------------+ | version | +----------------+ | 20080604194357 | | 20080604195453 | | 20080605112923 | +----------------+ 3 rows in set (0.00 sec)
  44. 44. Especially helpful for teams (avoids most conflicts)
  45. 45. Also helpful when working on branches
  46. 46. Migrations are applied intelligently
  47. 47. Also: change_table method introduced to migrations
  48. 48. New! Better caching
  49. 49. Specify preferred caching engine in environment.rb
  50. 50. # Following options supplied in Rails 2.1: ActionController::Base.cache_store = memory_store ActionController::Base.cache_store = file_store, quot;path/to/cache/directoryquot; ActionController::Base.cache_store = drb_store, quot;druby://localhost:9192quot; ActionController::Base.cache_store = mem_cache_store, quot;localhostquot;
  51. 51. Or subclass ActiveSupport::Cache::Store and implement read, write, delete and delete_matched methods
  52. 52. New! A few other tidbits
  53. 53. rake rails:freeze:edge RELEASE=1.2.0
  54. 54. script/plugin install supports -e for svn export and plugins hosted in Git repositories
  55. 55. script/dbconsole
  56. 56. >> quot; A string full of white spaces quot;.squish => quot;A string full of white spacesquot;
  57. 57. Resources • http://weblog.rubyonrails.com/2008/6/1/rails-2-1-time-zones- dirty-caching-gem-dependencies-caching-etc • Ryan Bates’ Railscasts • Ryan Daigle’s feature introductions • Ruby on Rails 2.1: What’s New? by Carlos Brando
  58. 58. Thanks for listening http://keithpitty.com

×