This document summarizes an upcoming talk about changes in Rails 3. It notes that Rails 3 release candidate 1 will soon be released, following beta 4. It warns that upgrading apps to Rails 3 may not be straightforward. The talk will cover unobtrusive JavaScript, ActiveModel which decouples models, ActiveRelation for database queries, and Bundler for managing gem dependencies.
1. This is a talk for those already familiar
with Rails and it’s current state.
Big changes coming in Rails3
Any moment now Rails 3 rc1 will be
released. Currently Rails3 beta4.
Upgrading apps may not be much fun.
Introducing Rails 3
Awesome++
2. The Awesome
• Unobtrusive Javascript
• ActiveModel - Decoupled Models
• ActiveRelation - NoSQL, but not as you know it
• Bundler - Dependency Sandboxing
4. HTML5 Custom Data Attributes
“Custom data attributes are intended to
store custom data private to the page or
application, for which there are no more
appropriate attributes or elements.”
• data-remote
• data-method
• data-confirm
• data-disable-with
13. How does it work?
Ships with Prototype out of the box.
Officially maintained support for jQuery also.
Third party support for dojo, ExtJS, mootools, Y!, SproutCore, etc.
All you need to do is re-implement public/javascripts/rails.js and you can use
whatever you like.
Check out Simon Carletti’s excellent blog post - http://s.mashd.cc/bunce
14. ActiveModel
• Split off from ActiveRecord
• Model like behaviour (or a subset of it) for any class.
15. Validations
class Post
include ActiveModel::Validations
validates_presence_of :title, :body
attr_accessor :title, :body
end
18. Validations
post = Post.new #<Post:0x17878cc>
post.valid? false
post.errors #<OrderedHash {:body=>["can't be
blank"], :title=>["can't be blank"]}>
19. Validations
post = Post.new #<Post:0x17878cc>
post.valid? false
post.errors #<OrderedHash {:body=>["can't be
blank"], :title=>["can't be blank"]}>
post.title = 'Demo post' "Demo post"
post.body = 'Example using "Example using validations on any
validations on any old class.' old class."
20. Validations
post = Post.new #<Post:0x17878cc>
post.valid? false
post.errors #<OrderedHash {:body=>["can't be
blank"], :title=>["can't be blank"]}>
post.title = 'Demo post' "Demo post"
post.body = 'Example using "Example using validations on any
validations on any old class.' old class."
post.valid? true
21. Serialisation
class Post
include ActiveModel::Serialization
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
22. Serialisation
post = Post.new(:title => ‘Demo #<Post:0x18d8c94
Post’, :body => ‘Example using @attributes={:body=>"Example
serialisation on any old class.’) using serialisation on any old
class.", :title=>"Demo Post"}>
23. Serialisation
post = Post.new(:title => ‘Demo #<Post:0x18d8c94
Post’, :body => ‘Example using @attributes={:body=>"Example
serialisation on any old class.’) using serialisation on any old
class.", :title=>"Demo Post"}>
post.to_json “{”title”: ”Demo Post”, ”body”:
”Example using serialisation on
any old class.”}”
24. Serialisation
post = Post.new(:title => ‘Demo #<Post:0x18d8c94
Post’, :body => ‘Example using @attributes={:body=>"Example
serialisation on any old class.’) using serialisation on any old
class.", :title=>"Demo Post"}>
post.to_json “{”title”: ”Demo Post”, ”body”:
”Example using serialisation on
any old class.”}”
post.to_json(:only => :title) “{”title”: ”Demo Post”}”
25. ActiveModel
ActiveModel::AttributeMethods Quickly add methods for every attribute.
Object lifecycle callbacks.
ActiveModel::Callbacks (before_save, after_create, etc)
ActiveModel::Conversion Allows proxying to an actual ActiveRecord object.
Track attribute changes.
ActiveModel::Dirty (changed? new_record? title_changed?)
ActiveModel::Errors ActiveRecord style errors hash - used by Validations.
Translate model names to human readable versions.
ActiveModel::Naming (BlogPosts to ‘Blog posts’)
ActiveModel::Observer Object lifecycle using observers.
ActiveModel::Serialization Serialising objects to json or xml.
ActiveModel::Validations Validating model attributes.
26. ActiveRelation
• Because of ActiveModel, ActiveRecord is storage agnostic. Can use SQL,
NoSQL, file or memory storage for models.
• If you do use SQL, ActiveRelation is now used to generate queries.
• Your old finders will continue to work, but this is awesome++.
38. Bundler
• Handles gem dependencies for you.
• Automatically loads required gems into runtime.
• Keeps them up to date.
• Sandboxes from system gems.
40. bundle install
Fetching source index for http://rubygems.org/
Using rake (0.8.7)
Using abstract (1.0.0)
Using activesupport (3.0.0.beta4)
Using builder (2.1.2)
Using i18n (0.4.1)
Using activemodel (3.0.0.beta4)
Using erubis (2.6.6)
Using rack (1.1.0)
Using rack-mount (0.6.6)
Using rack-test (0.5.4)
Using tzinfo (0.3.22)
Using actionpack (3.0.0.beta4)
Using mime-types (1.16)
Using polyglot (0.3.1)
Using treetop (1.4.8)
Using mail (2.2.5)
Using actionmailer (3.0.0.beta4)
Using arel (0.4.0)
Using activerecord (3.0.0.beta4)
Using activeresource (3.0.0.beta4)
Using bundler (1.0.0.beta.4)
Using thor (0.13.7)
Using railties (3.0.0.beta4)
Using rails (3.0.0.beta4)
Using sqlite3-ruby (1.3.1)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.