rails 3 beginner to builder 2011 week 6

43
June, 2011 Beginner to Builder Week 6 Wednesday, July 20, 2011

Upload: richard-schneeman

Post on 24-Dec-2014

5.867 views

Category:

Education


1 download

DESCRIPTION

This is the 6th 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.com/tagged/Rails-3-beginner-to-builder-2011

TRANSCRIPT

Page 1: Rails 3 Beginner to Builder 2011 Week 6

June, 2011

Beginner to BuilderWeek 6

Wednesday, July 20, 2011

Page 2: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Rails - Week 6• Email in Rails

• Background Tasks

• modules

• Observers & Callbacks

• Internationalization & Localization

• I18n & L10n

Wednesday, July 20, 2011

Page 3: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email in Rails• What is Email?

• Why Use Email?

• How does Rails use email?

Wednesday, July 20, 2011

Page 4: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

What is Email• Communications medium defined by RFC standards

✦ RFC = Request for Comments

✦ Comprised of Header & Body

✦ Header (to, from, reply-to, content-type, subject, cc, etc.)

✦ Body (message and attachments)

Wednesday, July 20, 2011

Page 5: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email- Content Types• Defines How the Mail User Agent (MUA) Interprets Body

✦ Text/HTML

✦ Text/Plain

✦ Multipart/Related (Example: Inline Pictures)

✦ Multipart/Alternative

✦ Send Text/HTML with Text/Plain as backup

✦ Add Attachments

Wednesday, July 20, 2011

Page 6: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Why use Email with Rails?

• Status Updates ( Twitter, Facebook, Etc. )

• Direct to Consumer Marketing source

• Added functionality (lost passwords, etc.)

• Send documents

• Everyone has it, and

• Everyone can use it

Wednesday, July 20, 2011

Page 7: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

How Does RoR Send Email?•Low Volume

• use Gmail

•High Volume

• use a re-mailer

• Build your own

Mail Server

Your Computer

d

Operating System

MTA

PostfixSMTP

(Sends Emails)

CourierIMAP/POP

Their Computer

Wednesday, July 20, 2011

Page 8: Rails 3 Beginner to Builder 2011 Week 6

• ActionMailer

• Mail Gem

@Schneems

rails generate mailer Notifier

How Does RoR Send

/app/mailers/notifier.rb

Wednesday, July 20, 2011

Page 9: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email With Rails

Notifier.signup_notification(“[email protected]”).deliver

Wednesday, July 20, 2011

Page 10: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email With Rails

• Default Mailer Settings

• In-Line Attachments

• mail( ) method

Notifier.signup_notification(“[email protected]”).deliver

Wednesday, July 20, 2011

Page 11: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email With Rails

• Default Mailer Settings

• In-Line Attachments

• mail( ) method

Notifier.signup_notification(“[email protected]”).deliver

Wednesday, July 20, 2011

Page 12: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email With Rails

• Default Mailer Settings

• In-Line Attachments

• mail( ) method

Notifier.signup_notification(“[email protected]”).deliver

Wednesday, July 20, 2011

Page 13: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email With Rails

Notifier.signup_notification(“[email protected]”).deliver

✦Using Gmail✦ Use Port 587✦ Gmail will throttle large

number of email requests✦ Close to real life conditions✦ Relatively Easy✦ Don’t use with automated

testing

config/environments/development.rb

Wednesday, July 20, 2011

Page 14: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Email re-cap✦ Receiving Email much harder

✦ Also less common✦ Test your Mailer using an Interceptor✦ use a re-mailer in production✦ real life application: http://whyspam.me

✦ No longer running

Wednesday, July 20, 2011

Page 15: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Background Tasks• What is a background task?

• Why use one?

• Where do i put my task in rails?

• How do i keep my task alive?

Wednesday, July 20, 2011

Page 16: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Background Task• What is a background task?

• Any server process not initiated by http request

• Commonly run for long periods of time

• Do not block or stop your application

• Clean up server, or application

• Generate reports

• Much more

Wednesday, July 20, 2011

Page 17: Rails 3 Beginner to Builder 2011 Week 6

• rake tasks

• organize code in “lib/tasks”

• run with:

@Schneems

rake <command> RAILS_ENV=<environment>

Background Task

Wednesday, July 20, 2011

Page 18: Rails 3 Beginner to Builder 2011 Week 6

• Example

• cleanup.rake

@Schneems

namespace :cleanup do desc "clean out Tickets over 30 days old" task :old_tickets => :environment do tickets = Ticket.find(:all, :conditions => ["created_at < ?", 30.days.ago ], :limit => 5000) tickets.each do |ticket| ticket.delete end endend

Background Task

rake cleanup:old_tickets

Wednesday, July 20, 2011

Page 19: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Background Task• What if i don’t want to execute from

command line?

• run task with a automation program

• Cron

• Monit

• God

Wednesday, July 20, 2011

Page 20: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Cron• Very reliable unix time scheduler

• Built into the OS

• Executes command line calls

• Smallest interval is 1 minute

• Requires full paths

Wednesday, July 20, 2011

Page 21: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Monit• Not installed on OS by default

• Monitors and Executes (cron only executes)

• Extra functionality - Sysadmin emails etc...

Wednesday, July 20, 2011

Page 22: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

God • Written in ruby

• Very configurable

• can be memory intensive in some applications

sudo gem install god

Wednesday, July 20, 2011

Page 23: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Background • More Options

• Workling/Starling

• Backgroundrb

• ResQue

Wednesday, July 20, 2011

Page 24: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Modules (ruby)• Add “Mixins” to your code

• Keep code seperate with different namespaces

• put them in your rails project under /lib

Wednesday, July 20, 2011

Page 25: Rails 3 Beginner to Builder 2011 Week 6

• Example Mixin:

• include adds instance methods

@Schneems

class Dog

include AntiCheating

end

Modules (ruby)

module AntiCheating

def drug_test

...

end

end

puppy = Dog.new

puppy.drug_test

>> Passed

Wednesday, July 20, 2011

Page 26: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Modules (ruby)• Example Mixin 2:

class Dog

include AntiCheating

end

module AntiCheating

def self.cleanup(level)

...

end

end

dirtyDog = Dog.new

dirtyDog.cleanup

>> No Method Error

Wednesday, July 20, 2011

Page 27: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Modules (ruby)• Example Mixin 2:

module AntiCheating

def self.cleanup(level)

...

end

end

AntiCheating.cleanup

>> Very Clean

Wednesday, July 20, 2011

Page 28: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Modules (ruby)• Example Mixin 2:

• extend adds all module methodsclass Dog

extend AntiCheating

end

module AntiCheating

def self.cleanup(level)

...

end

end

dirtyDog = Dog.new

dirtyDog.cleanup(2)

>> “Kinda Clean”

Wednesday, July 20, 2011

Page 29: Rails 3 Beginner to Builder 2011 Week 6

• Extra Credit:

• class << self

@Schneems

class Dog

class << self

def sniff

...

end

end

end

Modules (ruby)

==

class Dog

def self.sniff

...

end

end

Wednesday, July 20, 2011

Page 30: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Modules (ruby)• Extra Credit:

• class << self

class Dog

class << self

include AntiCheating

end

end

module AntiCheating

def self.cleanup(level)

...

end

end

dirtyDog = Dog.new

dirtyDog.cleanup(10)

>> “Really Clean”

Wednesday, July 20, 2011

Page 31: Rails 3 Beginner to Builder 2011 Week 6

• Example Namespace

@Schneems

module FastDogs

class Dog

...

end

end

Modules (ruby)

module SlowDogs

class Dog

...

end

end

lassie = FastDogs::Dog.new droopy = SlowDogs::Dog.new

Wednesday, July 20, 2011

Page 32: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Callbacks and Observers• Callbacks

• Non-polling event based method

• hooks into lifecycle of Active Record object

• Observers

• Implement trigger behavior for class outside of the original class

Wednesday, July 20, 2011

Page 33: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

• Polling

• “Are We there Yet”

• Callback

• “I’ll tell you when we’re there”

Callbacks

Wednesday, July 20, 2011

Page 34: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Callbacks in Railssave(-) valid(1) before_validation(2) before_validation_on_create(-) validate(-) validate_on_create(3) after_validation(4) after_validation_on_create(5) before_save(6) before_create(-) create(7) after_create(8) after_save

• Lifecycle Hooks =>

Wednesday, July 20, 2011

Page 35: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

class Topic < ActiveRecord::Base

before_destroy :delete_parents

def delete_parents

self.class.delete_all "parent_id = #{id}"

end

end

end

Callbacks in Rails• Example

• before_destroy

Wednesday, July 20, 2011

Page 36: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Observers• Add callback functionality without polluting the

model

• Will run after every new user instance is created

• Keeps your code clean(er)

Wednesday, July 20, 2011

Page 37: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Source: @mattt

i18n & L10n

Wednesday, July 20, 2011

Page 38: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Source: @mattt

i18n & L10n

Wednesday, July 20, 2011

Page 39: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Source: @mattt

Source: @mattt

language configuration

Wednesday, July 20, 2011

Page 40: Rails 3 Beginner to Builder 2011 Week 6

@SchneemsSource: @mattt

fr.yml

ja.yml

Multiple Language Files

Wednesday, July 20, 2011

Page 41: Rails 3 Beginner to Builder 2011 Week 6

@SchneemsSource: @mattt

It Worked!

Wednesday, July 20, 2011

Page 42: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

What about L10n

Wednesday, July 20, 2011

Page 43: Rails 3 Beginner to Builder 2011 Week 6

@Schneems

Questions?

http://guides.rubyonrails.orghttp://stackoverflow.com

http://peepcode.com

Wednesday, July 20, 2011