UT on Rails3 2010- Week 2
Upcoming SlideShare
Loading in...5
×
 

UT on Rails3 2010- Week 2

on

  • 1,645 views

This series introduces students to ruby on rails 3 through the book "Agile Web Development with Rails" with accompanying lecture videos found at http://www.thinkbohemian.com/tag/rails-summer-of-code/

This series introduces students to ruby on rails 3 through the book "Agile Web Development with Rails" with accompanying lecture videos found at http://www.thinkbohemian.com/tag/rails-summer-of-code/

Statistics

Views

Total Views
1,645
Views on SlideShare
1,368
Embed Views
277

Actions

Likes
0
Downloads
28
Comments
0

3 Embeds 277

http://www.thinkbohemian.com 267
http://webcache.googleusercontent.com 6
http://web.archive.org 4

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

UT on Rails3 2010- Week 2 UT on Rails3 2010- Week 2 Presentation Transcript

  • Rails Sumer of Code Week 2 Richard Schneeman - @ThinkBohemian
  • Rails - Week 2 • Ruby • Hashes • Classes • Macros • Methods • Instances • Methods Richard Schneeman - @ThinkBohemian
  • Ruby • Hashes - (Like a Struct) • Key - Value Pairs - Different DataTypes Ok hash = {:a => 100, :b => “hello”} >> hash[:a] => 100 >> hash[:b] => hello >> hash.keys => [:b, :a] Richard Schneeman - @ThinkBohemian
  • Ruby • Hashes in method parameters • options (a hash) is optional parameter def myMethod(variable, options = {}) puts variable options.keys.each do |key| puts options[key] end end >> myMethod(“Hi”) => Hi >> myMethod(“Hi”, :a => “hello” , “there”) => Hi Hello There Richard Schneeman - @ThinkBohemian
  • Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
  • Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
  • Ruby • Objects don’t have attributes • Need getter & setter methods class MyClass def myAttribute=(value) @myAttribute = value end def myAttribute @myAttribute end end >> object = MyClass.new >> object.myAttribute = “foo” >> object.myAttribute => “foo” Richard Schneeman - @ThinkBohemian
  • Ruby • Ruby gives us Class Macros !! • Example: attr_accessor class MyClass attr_accessor :myAttribute end >> object = MyClass.new >> object.myAttribute = “foo” >> object.myAttribute => “foo” Richard Schneeman - @ThinkBohemian
  • Ruby • Class Methods • (methods that can be run on the class) class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end My_class.myClassMethod("foo") => "foo" Richard Schneeman - @ThinkBohemian
  • Ruby • Class Methods • (methods that can be run on the class) class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end My_class.myClassMethod("foo") => "foo" Richard Schneeman - @ThinkBohemian
  • Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • Ruby • Class Methods class Dog • Have self def self.find(name) ... end • Instance Methods def wag_tail(frequency) ... • Don’t (Have self) end end Richard Schneeman - @ThinkBohemian
  • Ruby • Class Methods class Dog • Have self def self.find(name) ... end • Instance Methods def wag_tail(frequency) ... • Don’t (Have self) end end Richard Schneeman - @ThinkBohemian
  • Ruby • attr_accessor is a Class method attr_accessor :myAttribute Can be defined as: def self.attr_accessor(value) ... end cool ! Richard Schneeman - @ThinkBohemian
  • Rails - Week 2 • Code Generation • Migrations • Scaffolding • Validation • Testing (Rspec AutoTest) Richard Schneeman - @ThinkBohemian
  • Scaffolding • Generate Model, View, and Controller & Migration >> rails generate scaffold Post name:string title:string content:text • Generates “One Size Fits All” code • app/models/post.rb • app/controllers/posts_controller.rb • app/views/posts/ {index, show, new, create, destroy} • Modify to suite needs (convention over configuration) Richard Schneeman - @ThinkBohemian
  • Migrations • Create your data structure in your Database • Set a database’s schema migrate/create_post.rb class CreatePost < ActiveRecord::Migration def self.up create_table :post do |t| t.string :name t.string :title t.text :content end SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1 end def self.down drop_table :posts end end Richard Schneeman - @ThinkBohemian
  • Migrations • Get your data structure into your database >> rake db:migrate • Runs all “Up” migrations def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Creates a Table named post def self.up create_table :post do |t| • Adds Columns t.string t.string :name :title • t.text :content Name, Title, Content end end • created_at & updated_at added automatically Richard Schneeman - @ThinkBohemian
  • Migrations • Creates a Table named post def self.up create_table :post do |t| • Adds Columns t.string t.string :name :title • t.text :content Name, Title, Content end end • created_at & updated_at added automatically Richard Schneeman - @ThinkBohemian
  • Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • Migrations • Made a mistake? Issue a database Ctrl + Z ! >> rake db:rollback • Runs last “down” migration def self.down drop_table :system_settings end Richard Schneeman - @ThinkBohemian
  • Scaffolding • Generate Model, View, and Controller >> scaffold Post name:string title:string content:text • models/post.rb • controllers/posts_controller.rb • views/post/{ index.html, show.html, new.html, create.html } Richard Schneeman - @ThinkBohemian
  • Rails - Validations • Check your parameters before save • Provided by ActiveModel • Utilized by ActiveRecord class Person < ActiveRecord::Base validates :title, :presence => true, :title => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false Richard Schneeman - @ThinkBohemian
  • Rails - Validations Can use ActiveModel without Rails class Person include ActiveModel::Validations attr_accessor :title validates :title, :presence => true, :title => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false Richard Schneeman - @ThinkBohemian
  • Rails - Validations • Use Rail’s built in Validations # :acceptance => Boolean. # :confirmation => Boolean. # :exclusion => { :in => Ennumerable }. # :inclusion => { :in => Ennumerable }. # :format => { :with => Regexp, :on => :create }. # :length => { :maximum => Fixnum }. # :numericality => Boolean. # :presence => Boolean. # :uniqueness => Boolean. • Write your Own Validations class User < ActiveRecord::Base validate :my_custom_validation private def my_custom_validation self.errors.add(:password, "Custom Message") unless self.property.present? end end Richard Schneeman - @ThinkBohemian
  • Testing • Test your code (or wish you did) • Makes upgrading and refactoring easier • Different Environments • production - live on the web • development - on your local computer • test - local clean environment Richard Schneeman - @ThinkBohemian
  • Testing • Unit Tests • Test individual methods against known inputs • Integration Tests • Test Multiple Controllers • Functional Tests • Test full stack M- V-C Richard Schneeman - @ThinkBohemian
  • Unit Testing • ActiveSupport::TestCase • Provides assert statements • assert true • assert (variable) • assert_same( obj1, obj2 ) • many more Richard Schneeman - @ThinkBohemian
  • Unit Testing • Run Unit Tests using: >> rake test:units RAILS_ENV=test Richard Schneeman - @ThinkBohemian
  • Unit Testing • Run Tests automatically the background • ZenTest • sudo gem install ZenTest • Run: >> autotest Richard Schneeman - @ThinkBohemian
  • Questions? Richard Schneeman - @ThinkBohemian