rails 3 beginner to builder 2011 week 1

53
June, 2011 Beginner To Builder Week 1 Richard Schneeman @schneems Friday, June 10, 2011

Upload: richard-schneeman

Post on 16-May-2015

13.863 views

Category:

Education


0 download

DESCRIPTION

This is the first 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

TRANSCRIPT

Page 1: Rails 3 Beginner to Builder 2011 Week 1

June, 2011

Beginner To BuilderWeek 1Richard Schneeman@schneems

Friday, June 10, 2011

Page 2: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Background

• Georgia Tech

• 5 Years of Rails

• Rails dev for Gowalla

Friday, June 10, 2011

Page 3: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

About the Class• Why?

• Structure

• Class & Course Work

• Ruby through Rails

• 8 Weeks

• 1 hour

Friday, June 10, 2011

Page 4: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Rails - Week 1• Ruby

• Versus Rails

• Data Types

• Rails Architecture

• MVC (Model View Controller)

• ORM (Object Relational Mapping)

• RESTful (REpresentational STate)

Friday, June 10, 2011

Page 5: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Rails - Week 1•Workspace

• Version Control - Keep your code safe

• RubyGems - Use other’s code

• Bundler - Manage Dependencies

• RVM - Keep your system clean

• Tests - make sure it works

Friday, June 10, 2011

Page 6: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Versus Rails• Ruby - Is a programming Language

• Like C# or Python

• Can be used to program just about anything

• Rails - Is a Framework

• Provides common web functionality

• Focus on your app, not on low level details

Friday, June 10, 2011

Page 7: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Rails is a Web Framework• Develop, deploy, and maintain dynamic web apps

• Written using Ruby

• Extremely flexible, expressive, and quick development time

Friday, June 10, 2011

Page 8: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Technologies• Html - creates a view

• Javascript - makes it interactive

• css - makes it pretty

• Ruby - Makes it a web app

Friday, June 10, 2011

Page 9: Rails 3 Beginner to Builder 2011 Week 1

@SchneemsFriday, June 10, 2011

Page 10: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Resources• why’s (poignant) guide to ruby

• Free, quirky

• Programming Ruby (Pickaxe)

• Not free, encyclopedic

Friday, June 10, 2011

Page 11: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Resources• Metaprogramming Ruby

• Skips basics

• Unleash the power of Ruby

Friday, June 10, 2011

Page 12: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Interactive Ruby Console(IRB) or http://TryRuby.org/

Friday, June 10, 2011

Page 13: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Strings• Characters (letters, digits, punctuation)

surrounded by quotes

food = "chunky bacon"

puts "I'm hungry for, #{food}!"

>> "I'm hungry for, chunky bacon!"

"I'm hungry for, chunky bacon!".class

>> String

Friday, June 10, 2011

Page 14: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby (numbers)

123.class

>> Fixnum

(123.0).class

>> Float

Friday, June 10, 2011

Page 15: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Symbols•Symbols are lightweight strings

• start with a colon

• immutable

:a, :b or :why_the_lucky_stiff

:why_the_lucky_stiff.class

>> Symbol

Friday, June 10, 2011

Page 16: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Hash•A hash is a dictionary surrounded by curly

braces.

•Dictionaries match words with their definitions.

my_var = {:sup => "dog", :foo => "bar"}

my_var[:foo]

>> "bar"

{:sup => "dog", :foo => "bar"}.class

>> Hash

Friday, June 10, 2011

Page 17: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Array•An array is a list surrounded by square

brackets and separated by commas.

array = [ 1, 2, 3, 4 ]

array.first

>> 1

[ 1, 2, 3, 4 ].class

>> Array

Friday, June 10, 2011

Page 18: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Array•Zero Indexed

array = [ 1, 2, 3, 4 ]

array[0]

>> 1

array = [ 1, 2, 3, 4 ]

array[1]

>> 2

Friday, June 10, 2011

Page 19: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Blocks•Code surrounded by curly braces

2.times { puts "hello"}

>> "hello"

>> "hello"

•Do and end can be used instead2.times do

puts "hello"

end

>> "hello"

>> "hello"

Friday, June 10, 2011

Page 20: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Ruby Blocks•Can take arguments

• variables surrounded by pipe (|)

2.times do |i|

puts "hello #{i}"

end

>> "hello 0"

>> "hello 1"

Friday, June 10, 2011

Page 21: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Rails why or why-not?• Speed

• developer vs computer

• Opinionated framework

• Quick moving ecosystem

Friday, June 10, 2011

Page 22: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Rails Architecture• Terminology

• DRY

• Convention over Configuration

• Rails Architecture

• MVC (Model View Controller)

• ORM (Object Relational Mapping)

• RESTful (REpresentational State Transfer)

Friday, June 10, 2011

Page 23: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

DRYDon’t Repeat Yourself

Reuse, don’t re-invent the...

Friday, June 10, 2011

Page 24: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Convention over Configuration

Decrease the number of decisions needed, gaining simplicity but without losing flexibility.

Friday, June 10, 2011

Page 25: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

• Isolates “Domain Logic”

• Can I See it?

• View

• Is it Business Logic?

• Controller

• Is it a Reusable Class Logic?

• Model

Model-View-Controller

Friday, June 10, 2011

Page 26: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Model-View-Controller• Generated By Rails

• Grouped by Folders

• Connected “AutoMagically”

• Models

• Views

• Controllers

• Multiple Views Per Controller

Friday, June 10, 2011

Page 27: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Database Backed Models• Store and access massive amounts of

data

• Table

• columns (name, type, modifier)

• rows

Table: Users

Friday, June 10, 2011

Page 28: Rails 3 Beginner to Builder 2011 Week 1

• Structured Query Language

• A way to talk to databases

@Schneems

SELECT *

FROM Book

WHERE price > 100.00

ORDER BY title;

SQL

Friday, June 10, 2011

Page 29: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

SQL operations• insert

• query

• update and delete

• schema creation and modification

Friday, June 10, 2011

Page 30: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Object Relational Mapping• Maps database backend to ruby objects

• ActiveRecord (Rail’s Default ORM)>> userVariable = User.where(:name => "Bob")

>> userVariable.name=> Bob

Generates:SELECT \"users\".* FROM \"users\"WHERE (name = 'bob')

Friday, June 10, 2011

Page 31: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

• >> userVariable = User .where(:name => "Bob")

models/user.rb

the User class inherits from ActiveRecord::Base

Object Relational Mapping

class User < ActiveRecord::Base

end

Friday, June 10, 2011

Page 32: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Object Relational Mapping

• >> userVariable = User. where(:name => "Bob")

where is the method that looks in the databaseAutoMagically in the User Table (if you made one)

Friday, June 10, 2011

Page 33: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

RESTful

• The state of the message matters

• Different state = different message

REpresentational State Transfer

“You Again?” “You Again?”

Friday, June 10, 2011

Page 34: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

RESTful• Servers don’t care about smiles

• They do care about how you access them

• (HTTP Methods)

• GET

• PUT

• POST

• DELETE

REpresentational State Transfer

Friday, June 10, 2011

Page 35: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

RESTful• Rails Maps Actions to HTTP Methods

• GET - index, show, new

• PUT - update

• POST - create

• DELETE - destroy

REpresentational State Transfer

Friday, June 10, 2011

Page 36: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Work Environment• Version Control - Keep your code safe

• RubyGems - Use other’s code

• Bundler - Manage Dependencies

• RVM - Keep your system clean

• Tests - make sure it works

Friday, June 10, 2011

Page 37: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Version Control• my_last_update_1.rb

• my_realy_last_update_2.rb

• really_the_good_code_last_final_new.rb

Friday, June 10, 2011

Page 38: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Version Control• my_last_update_1.rb

• my_realy_last_update_2.rb

• really_the_good_code_last_final_new.rb

Friday, June 10, 2011

Page 39: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Version Control• my_last_update_1.rb

• my_realy_last_update_2.rb

• really_the_good_code_last_final_new.rb

Friday, June 10, 2011

Page 40: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Version Control• Make note of whats different

• See changes over time

• revert back to known state

• work with a team

Friday, June 10, 2011

Page 41: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Version Control• Git (recommended)

• SVN

• Mecurial

• Perforce

• Many More

Friday, June 10, 2011

Page 42: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Github

http://github.com

Friday, June 10, 2011

Page 43: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

RubyGems• Gems

• External code “packages”

• Rubygems Manages these “packages”

gem install keytar

irb

>> require “rubygems”

=> true

>> require “keytar”

=> true

Friday, June 10, 2011

Page 44: Rails 3 Beginner to Builder 2011 Week 1

@SchneemsFriday, June 10, 2011

Page 45: Rails 3 Beginner to Builder 2011 Week 1

• Install

• Gemfiles

• specify dependencies

@Schneems

gem install bundler

Bundler

source :rubygems

gem 'rails', '3.0.4'

gem 'unicorn', '3.5.0'

gem 'keytar'

Friday, June 10, 2011

Page 46: Rails 3 Beginner to Builder 2011 Week 1

• Install

• installs all gems listed in gemfile

• very useful managing across systems

@Schneems

bundle install

Bundler

Friday, June 10, 2011

Page 47: Rails 3 Beginner to Builder 2011 Week 1

• Ruby Version Manager

• Clean Sandbox for each project

@Schneems

RVM

rvm use ruby-1.8.7-p302

rvm use ruby-1.9.2-p180

Friday, June 10, 2011

Page 48: Rails 3 Beginner to Builder 2011 Week 1

• Use fresh gemset for each project

• Switch projects...switch gemsets

@Schneems

RVM

rvm gemset use gowalla

rvm gemset use keytar

Friday, June 10, 2011

Page 49: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Testing• Does your code 6 months ago work?

• What did it do again?

• Manual Versus Programatic

• Save Time in the long road by progamatic Testing

Friday, June 10, 2011

Page 50: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Testing• Test framework built into Rails

• Swap in other frame works

• Use Continuous Integration (CI)

• All tests green

• When your web app breaks, write a test for it (never again)

Friday, June 10, 2011

Page 51: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

IDE• Mac: Textmate

• Windows: Notepad ++

• Eclipse & Aptana RadRails

Friday, June 10, 2011

Page 52: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Recap• Rails

• Framework

• Convention over Configuration

• Ruby

• Expressive Scripting language

Friday, June 10, 2011

Page 53: Rails 3 Beginner to Builder 2011 Week 1

@Schneems

Questions?

Friday, June 10, 2011