Barcamp Auckland Rails3 presentation

1,771 views

Published on

My Rails3 presentation from Barcamp Auckland 4 2010.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,771
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide






















































































  • Barcamp Auckland Rails3 presentation

    1. 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. 2. The Awesome • Unobtrusive Javascript • ActiveModel - Decoupled Models • ActiveRelation - NoSQL, but not as you know it • Bundler - Dependency Sandboxing
    3. 3. Unobtrusive Javascript • HTML5 Custom Data Attributes • Framework Agnostic
    4. 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
    5. 5. <%= link_to_remote post.title, :url => post %> <a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('rsI1s8y8MPBnndR+AD +Q8pkPhvfFmsvSElu9sB8ZluY=')}); return false;">Demo</a>
    6. 6. <%= link_to post.title, post, :remote => true %> <a href=”/posts/1” data-remote=”true”>Demo</a>
    7. 7. <%= link_to ‘Delete’, post, :method => :delete %> <a href="/posts/1" onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'true'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'rsI1s8y8MPBnndR+AD+Q8pkPhvfFmsvSElu9sB8ZluY=');
    8. 8. <%= link_to ‘Delete’, post, :method => :delete %> <a href="/posts/1" data-method="delete" rel="nofollow">Delete</a>
    9. 9. <%= link_to 'Confirm', post, :confirm => 'Are you sure?' %> <a href="/posts/1" onclick="return confirm('Are you sure?');">Confirm</a>
    10. 10. <%= link_to 'Confirm', post, :confirm => 'Are you sure?' %> <a href="/posts/1" data-confirm="Are you sure?">Confirm</a>
    11. 11. <%= f.submit 'Create Post', :disable_with => 'Please wait...' %> <input id="post_submit" name="commit" onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Please wait...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit" value="Create Post" />
    12. 12. <%= f.submit :disable_with => 'Please wait...' %> <input data-disable-with="Please wait..." id="post_submit" name="commit" type="submit" value="Create Post" />
    13. 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. 14. ActiveModel • Split off from ActiveRecord • Model like behaviour (or a subset of it) for any class.
    15. 15. Validations class Post include ActiveModel::Validations validates_presence_of :title, :body attr_accessor :title, :body end
    16. 16. Validations post = Post.new #<Post:0x17878cc>
    17. 17. Validations post = Post.new #<Post:0x17878cc> post.valid? false
    18. 18. Validations post = Post.new #<Post:0x17878cc> post.valid? false post.errors #<OrderedHash {:body=>["can't be blank"], :title=>["can't be blank"]}>
    19. 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. 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. 21. Serialisation class Post include ActiveModel::Serialization attr_accessor :attributes def initialize(attributes) @attributes = attributes end end
    22. 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. 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. 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. 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. 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++.
    27. 27. @posts = Post.find(:all, :conditions => {:published => true}) [#<Post id: 1, title: "Demo", body: "Demo post.", created_at: "2010-07-15 21:28:10", updated_at: "2010-07-16 02:49:45", published: true>]
    28. 28. @posts = Post.where(:published => true) [#<Post id: 1, title: "Demo", body: "Demo post.", created_at: "2010-07-15 21:33:56", updated_at: "2010-07-16 02:57:07", published: true>]
    29. 29. @posts = Post.find(:all, :conditions => {:published => true}).class Array
    30. 30. @posts = Post.where(:published => true).class ActiveRecord::Relation
    31. 31. Lazy Loading Queries are not executed until the object is enumerated or cast to an array.
    32. 32. Chainable You can chain multiple where() calls together making it much easier to filter your resultset.
    33. 33. Flexible Post.select(:title) .joins(:comments) .group('date(comments.created_at)') .having(:created_at => 1.day.ago..Time.now) .where(:published => true) .order('comments.created_at DESC') .limit(1) .offset(0) .includes(:comments) .lock .readonly(true)
    34. 34. Flexible Post.select(:title) .joins(:comments) .group('date(comments.created_at)') .having(:created_at => 1.day.ago..Time.now) .where(:published => true) .order('comments.created_at DESC') [#<Post title: "Demo">] .limit(1) .offset(0) .includes(:comments) .lock .readonly(true)
    35. 35. Scoping class Post < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false) end
    36. 36. Scoping class Post < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false) end Post.published
    37. 37. Scoping class Post < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false) end Post.published [#<Post id: 1, title: "Demo", body: "Demo post.", created_at: "2010-07-16 03:21:46", updated_at: "2010-07-16 03:26:46", published: true>]
    38. 38. Bundler • Handles gem dependencies for you. • Automatically loads required gems into runtime. • Keeps them up to date. • Sandboxes from system gems.
    39. 39. Gemfile source ‘http://rubygems.org’ gem ‘rails’, ‘3.0.0.beta4’ gem ‘sqlite3-ruby’, :require => ‘sqlite3’
    40. 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.
    41. 41. source 'http://rubygems.org' gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'haml' gem 'authlogic', :git => 'http://github.com/odorcicd/authlogic.git', :branch => 'rails3' gem 'stringex', :git => 'http://github.com/jamesotron/stringex.git' gem 'oauth' gem 'json' Gemfile gem 'twitter' gem 'oauth-plugin', :git => 'git://github.com/pelle/oauth-plugin.git', :branch => 'rails3' gem 'rdiscount' gem 'paperclip' source ‘http://rubygems.org’ gem 'pg' gem ‘rails’, ‘3.0.0.beta4’ group :development do gem ‘sqlite3-ruby’, :require => ‘sqlite3’ 'sqlite3' gem 'sqlite3-ruby', :require => gem 'bullet' gem 'method_extensions' gem 'ruby-debug' gem 'mail_safe' end
    42. 42. Questions?

    ×