Rails 3 Beginner to Builder 2011 Week 2

12,427 views

Published on

This is the 2nd of 8 presentations given at University of Texas during my Beginner to Builder Rails 3 Class. For more info and the whole series including video presentations at my blog:


http://schneems.tumblr.com/tagged/Rails-3-beginner-to-builder-2011

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
12,427
On SlideShare
0
From Embeds
0
Number of Embeds
9,684
Actions
Shares
0
Downloads
142
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Rails 3 Beginner to Builder 2011 Week 2

  1. 1. Beginner to Builder Week 2 Richard Schneeman @schneemsJune, 2011Thursday, June 16, 2011
  2. 2. Rails - Week 2 • Ruby • Hashes & default params • Classes • Macros • Methods • Instances • Methods@SchneemsThursday, June 16, 2011
  3. 3. Rails - Week 2 • Code Generation • Migrations • Scaffolding • Validation • Testing (Rspec AutoTest)@SchneemsThursday, June 16, 2011
  4. 4. Ruby - Default Params def what_is_foo(foo = "default") puts "foo is: #{foo}" end what_is_foo >> "foo is: default" what_is_foo("not_default") >> "foo is: not_default"@SchneemsThursday, June 16, 2011
  5. 5. Ruby • Hashes - (Like a Struct) • Key - Value Ok - Different DataTypes Pairs hash = {:a => 100, “b” => “hello”} >> hash[:a] => 100 >> hash[“b”] => hello >> hash.keys => [“b”, :a]@SchneemsThursday, June 16, 2011
  6. 6. Ruby - Default Params • Hashes in method parameters • options (a hash) is optional parameter • has a default value def list_hash(options = {:default => "foo"}) options.each do |key, value| puts "key #{key} points to #{value}" end end list_hash >> "key default points to foo"@SchneemsThursday, June 16, 2011
  7. 7. Ruby - Default Params def list_hash(options = {:default => "foo"}) options.each do |key, value| puts "key #{key} points to #{value}" end end list_hash(:override => "bar") >> "key override points to bar" list_hash(:multiple => "values", :can => "be_passed") >> "key multiple points to values" >> "key can points to be_passed"@SchneemsThursday, June 16, 2011
  8. 8. Hashes in Rails • Used heavily as parameters • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area@SchneemsThursday, June 16, 2011
  9. 9. Hashes in Rails • Used heavily as parameters • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area@SchneemsThursday, June 16, 2011
  10. 10. Ruby • Objects don’t have attributes • Only methods • Need getter & setter methods@SchneemsThursday, June 16, 2011
  11. 11. Attributes class MyClass def my_attribute=(value) @myAttribute = value end def my_attribute @myAttribute end end >> object = MyClass.new >> object.my_attribute = “foo” >> object.my_attribute => “foo”@SchneemsThursday, June 16, 2011
  12. 12. Ruby - attr_accessor class Car attr_accessor :color end >> my_car = Car.new >> my_car.color = “hot_pink” >> my_car.color => “hot_pink”@SchneemsThursday, June 16, 2011
  13. 13. Attributes class MyClass def my_attribute=(value) @myAttribute = value end def my_attribute @myAttribute end end >> object = MyClass.new >> object.my_attribute = “foo” >> object.my_attribute => “foo”@SchneemsThursday, June 16, 2011
  14. 14. Ruby - Self class MyClass puts self end >> MyClass # self is our class class MyClass def my_method puts self end end MyClass.new.my_method >> <MyClass:0x1012715a8> # self is our instance@SchneemsThursday, June 16, 2011
  15. 15. Ruby - Class Methods class MyClass def self.my_method_1(value) puts value end end MyClass.my_method_1("foo") >> "foo"@SchneemsThursday, June 16, 2011
  16. 16. Ruby - Class Methods class MyClass def self.my_method_1(value) puts value end end my_instance = MyClass.new my_instance.my_method_1("foo") >> NoMethodError: undefined method `my_method_1 for #<MyClass:0x100156cf0> from (irb):108 from :0 def self: declares class methods@SchneemsThursday, June 16, 2011
  17. 17. Ruby - Instance Methods class MyClass def my_method_2(value) puts value end end MyClass.my_method_2("foo") >> NoMethodError: undefined method `my_method_2 for MyClass:Class from (irb):114 from :0@SchneemsThursday, June 16, 2011
  18. 18. Ruby - Instance Methods class MyClass def my_method_2(value) puts value end end my_instance = MyClass.new my_instance.my_method_2("foo") >> "foo"@SchneemsThursday, June 16, 2011
  19. 19. Ruby • Class Methods class Dog • Have self def self.find(name) ... • Instance Methods end • Don’t (Have self) def wag_tail(frequency) ... end end@SchneemsThursday, June 16, 2011
  20. 20. Ruby • attr_accessor is a Class method class Dog attr_accessor :fur_color end Can be defined as: class Dog def self.attr_accessor(value) #... end end@Schneems cool !Thursday, June 16, 2011
  21. 21. Rails - Week 2 • Code Generation • Migrations • Scaffolding • Validation • Testing (Rspec AutoTest)@SchneemsThursday, June 16, 2011
  22. 22. 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)@SchneemsThursday, June 16, 2011
  23. 23. Database Backed Models • Store and access massive amounts of data • Table • columns (name, type, modifier) • rows Table: Users@SchneemsThursday, June 16, 2011
  24. 24. Create Schema • Create Schema • Multiple machines Enter...Migrations@SchneemsThursday, June 16, 2011
  25. 25. 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@SchneemsThursday, June 16, 2011
  26. 26. 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@SchneemsThursday, June 16, 2011
  27. 27. Migrations def self.up create_table :post do |t| • Creates a Table named post def self.up t.string :name do |t| • Adds Columns create_table :post t.string :title t.string :name t.string :title • Name, Title, Content t.textt.text :content end :content end end • created_at & updated_at added automatically end@SchneemsThursday, June 16, 2011
  28. 28. 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@SchneemsThursday, June 16, 2011
  29. 29. 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@SchneemsThursday, June 16, 2011
  30. 30. 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@SchneemsThursday, June 16, 2011
  31. 31. 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@SchneemsThursday, June 16, 2011
  32. 32. 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@SchneemsThursday, June 16, 2011
  33. 33. Migrations • Made a mistake? Issue a database Ctrl + Z ! >> rake db:rollback • Runs last “down” migration def self.down drop_table :system_settings end@SchneemsThursday, June 16, 2011
  34. 34. Rails - Validations • Check your parameters before save • Provided by ActiveModel • Utilized by ActiveRecord class Person < ActiveRecord::Base validates :title, :presence => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false@SchneemsThursday, June 16, 2011
  35. 35. Rails - Validations Can use ActiveModel without Rails class Person include ActiveModel::Validations attr_accessor :title validates :title, :presence => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false@SchneemsThursday, June 16, 2011
  36. 36. Rails - Validations • Use Rail’s built in Validations # :acceptance => Boolean. # :confirmation => Boolean. # :exclusion => { :in => Enumerable }. # :inclusion => { :in => Enumerable }. # :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(:coolness, "bad") unless self.cool == “supercool” end end@SchneemsThursday, June 16, 2011
  37. 37. If & Unless puts “hello” if true >> “hello” puts “hello” if false >> nil puts “hello” unless true >> nil puts “hello” unless false >> “hello”@SchneemsThursday, June 16, 2011
  38. 38. blank? & present? puts “hello”.blank? >> false puts “hello”.present? >> true puts false.blank? >> true puts nil.blank? >> true puts [].blank? >> true puts “”.blank? >> true@SchneemsThursday, June 16, 2011
  39. 39. 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@SchneemsThursday, June 16, 2011
  40. 40. Testing • Unit Tests • Test individual methods against known inputs • Integration Tests • Test Multiple Controllers • Functional Tests • Test full stack M- V-C@SchneemsThursday, June 16, 2011
  41. 41. Unit Testing • ActiveSupport::TestCase • Provides assert statements • assert true • assert (variable) • assert_same( obj1, obj2 ) • many more@SchneemsThursday, June 16, 2011
  42. 42. Unit Testing • Run Unit Tests using: >> rake test:units RAILS_ENV=test@SchneemsThursday, June 16, 2011
  43. 43. Unit Testing • Run Tests automatically the background • ZenTest • gem install autotest-standalone • Run: >> autotest@SchneemsThursday, June 16, 2011
  44. 44. Questions?@SchneemsThursday, June 16, 2011

×