rails 3 beginner to builder 2011 week 6
Post on 24-Dec-2014
5.867 Views
Preview:
DESCRIPTION
TRANSCRIPT
June, 2011
Beginner to BuilderWeek 6
Wednesday, July 20, 2011
@Schneems
Rails - Week 6• Email in Rails
• Background Tasks
• modules
• Observers & Callbacks
• Internationalization & Localization
• I18n & L10n
Wednesday, July 20, 2011
@Schneems
Email in Rails• What is Email?
• Why Use Email?
• How does Rails use email?
Wednesday, July 20, 2011
@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
@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
@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
@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
• ActionMailer
• Mail Gem
@Schneems
rails generate mailer Notifier
How Does RoR Send
/app/mailers/notifier.rb
Wednesday, July 20, 2011
@Schneems
Email With Rails
Notifier.signup_notification(“foo@example.com”).deliver
Wednesday, July 20, 2011
@Schneems
Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Wednesday, July 20, 2011
@Schneems
Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Wednesday, July 20, 2011
@Schneems
Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Wednesday, July 20, 2011
@Schneems
Email With Rails
Notifier.signup_notification(“foo@example.com”).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
@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
@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
@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
• rake tasks
• organize code in “lib/tasks”
• run with:
@Schneems
rake <command> RAILS_ENV=<environment>
Background Task
Wednesday, July 20, 2011
• 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
@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
@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
@Schneems
Monit• Not installed on OS by default
• Monitors and Executes (cron only executes)
• Extra functionality - Sysadmin emails etc...
Wednesday, July 20, 2011
@Schneems
God • Written in ruby
• Very configurable
• can be memory intensive in some applications
sudo gem install god
Wednesday, July 20, 2011
@Schneems
Background • More Options
• Workling/Starling
• Backgroundrb
• ResQue
Wednesday, July 20, 2011
@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
• 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
@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
@Schneems
Modules (ruby)• Example Mixin 2:
module AntiCheating
def self.cleanup(level)
...
end
end
AntiCheating.cleanup
>> Very Clean
Wednesday, July 20, 2011
@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
• 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
@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
• 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
@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
@Schneems
• Polling
• “Are We there Yet”
• Callback
• “I’ll tell you when we’re there”
Callbacks
Wednesday, July 20, 2011
@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
@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
@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
@Schneems
Source: @mattt
i18n & L10n
Wednesday, July 20, 2011
@Schneems
Source: @mattt
i18n & L10n
Wednesday, July 20, 2011
@Schneems
Source: @mattt
Source: @mattt
language configuration
Wednesday, July 20, 2011
@SchneemsSource: @mattt
fr.yml
ja.yml
Multiple Language Files
Wednesday, July 20, 2011
@SchneemsSource: @mattt
It Worked!
Wednesday, July 20, 2011
@Schneems
What about L10n
Wednesday, July 20, 2011
@Schneems
Questions?
http://guides.rubyonrails.orghttp://stackoverflow.com
http://peepcode.com
Wednesday, July 20, 2011
top related