UT on Rails3 2010- Week 2

1,404
-1

Published on

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/

Published in: Self Improvement
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,404
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

































  • UT on Rails3 2010- Week 2

    1. 1. Rails Sumer of Code Week 2 Richard Schneeman - @ThinkBohemian
    2. 2. Rails - Week 2 • Ruby • Hashes • Classes • Macros • Methods • Instances • Methods Richard Schneeman - @ThinkBohemian
    3. 3. 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
    4. 4. 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
    5. 5. Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
    6. 6. Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
    7. 7. 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
    8. 8. 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
    9. 9. 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
    10. 10. 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
    11. 11. 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
    12. 12. 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
    13. 13. 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
    14. 14. 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
    15. 15. 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
    16. 16. Ruby • attr_accessor is a Class method attr_accessor :myAttribute Can be defined as: def self.attr_accessor(value) ... end cool ! Richard Schneeman - @ThinkBohemian
    17. 17. Rails - Week 2 • Code Generation • Migrations • Scaffolding • Validation • Testing (Rspec AutoTest) Richard Schneeman - @ThinkBohemian
    18. 18. 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
    19. 19. 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
    20. 20. 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
    21. 21. 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
    22. 22. 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
    23. 23. 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
    24. 24. 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
    25. 25. 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
    26. 26. 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
    27. 27. 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
    28. 28. 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
    29. 29. 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
    30. 30. 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
    31. 31. 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
    32. 32. 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
    33. 33. 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
    34. 34. 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
    35. 35. Unit Testing • ActiveSupport::TestCase • Provides assert statements • assert true • assert (variable) • assert_same( obj1, obj2 ) • many more Richard Schneeman - @ThinkBohemian
    36. 36. Unit Testing • Run Unit Tests using: >> rake test:units RAILS_ENV=test Richard Schneeman - @ThinkBohemian
    37. 37. Unit Testing • Run Tests automatically the background • ZenTest • sudo gem install ZenTest • Run: >> autotest Richard Schneeman - @ThinkBohemian
    38. 38. Questions? Richard Schneeman - @ThinkBohemian
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×