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
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'"
@Schneems
Thursday, June 16, 2011
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'"
@Schneems
Thursday, June 16, 2011
8. Hashes in Rails
• Used heavily as parameters
• options (a hash) is optional parameter
Rails API: (ActionView::Helpers::FormHelper) text_area
@Schneems
Thursday, June 16, 2011
9. Hashes in Rails
• Used heavily as parameters
• options (a hash) is optional parameter
Rails API: (ActionView::Helpers::FormHelper) text_area
@Schneems
Thursday, June 16, 2011
10. Ruby
• Objects don’t have attributes
• Only methods
• Need getter & setter methods
@Schneems
Thursday, June 16, 2011
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”
@Schneems
Thursday, June 16, 2011
12. Ruby - attr_accessor
class Car
attr_accessor :color
end
>> my_car = Car.new
>> my_car.color = “hot_pink”
>> my_car.color
=> “hot_pink”
@Schneems
Thursday, June 16, 2011
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”
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
15. Ruby - Class Methods
class MyClass
def self.my_method_1(value)
puts value
end
end
MyClass.my_method_1("foo")
>> "foo"
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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"
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
37. If & Unless
puts “hello” if true
>> “hello”
puts “hello” if false
>> nil
puts “hello” unless true
>> nil
puts “hello” unless false
>> “hello”
@Schneems
Thursday, June 16, 2011
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
@Schneems
Thursday, June 16, 2011
40. Testing
• Unit Tests
• Test individual methods against known
inputs
• Integration Tests
• Test Multiple Controllers
• Functional Tests
• Test full stack M- V-C
@Schneems
Thursday, June 16, 2011
41. Unit Testing
• ActiveSupport::TestCase
• Provides assert statements
• assert true
• assert (variable)
• assert_same( obj1, obj2 )
• many more
@Schneems
Thursday, June 16, 2011
42. Unit Testing
• Run Unit Tests using:
>> rake test:units RAILS_ENV=test
@Schneems
Thursday, June 16, 2011
43. Unit Testing
• Run Tests automatically the
background
• ZenTest
• gem install autotest-standalone
• Run: >> autotest
@Schneems
Thursday, June 16, 2011