This document summarizes content from Week 6 of the Rails Summer of Code. It discusses email in Rails, including how email works, content types, and using ActionMailer. It also covers background tasks in Rails using rake tasks, cron jobs, Monit, and God. Modules and mixins in Ruby are explained. Callbacks and observers in Rails are described as ways to trigger behavior during an object's lifecycle. Internationalization and localization in Rails are covered, including using the I18n module and creating language-specific YAML files.
3. Email in Rails
• What is Email?
• Why Use Email?
• How does Rails use email?
Richard Schneeman - @ThinkBohemian
4. 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)
Richard Schneeman - @ThinkBohemian
5. 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
Richard Schneeman - @ThinkBohemian
6. 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
Richard Schneeman - @ThinkBohemian
7. How Does RoR Send
Email?
• Low Volume Mail Server Operating System
• use Gmail
Your
Computer MTA
• High Volume
Postfix
SMTP
(Sends Emails)
d
• use a re-mailer Their
Computer Courier
IMAP/POP
• Build your own
Richard Schneeman - @ThinkBohemian
8. How Does RoR Send
Email?
• ActionMailer
• Mail Gem
rails generate mailer Notifier
/app/mailers/notifier.rb
Richard Schneeman - @ThinkBohemian
9. Email With Rails
Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
10. Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
11. Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
12. Email With Rails
• Default Mailer Settings
• In-Line Attachments
• mail( ) method
Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
13. Email With Rails
✦ Using Gmail
config/environments/development.rb
✦ Use Port 587
✦ Gmail will throttle large number
of email requests
✦ Close to real life conditions
✦ Relatively Easy
✦ Don’t use with automated
Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
14. 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
Richard Schneeman - @ThinkBohemian
15. 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?
Richard Schneeman - @ThinkBohemian
16. 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
Richard Schneeman - @ThinkBohemian
17. Background Task
• rake tasks
• organize code in “lib/tasks”
• run with:
rake <command> RAILS_ENV=<environment>
Richard Schneeman - @ThinkBohemian
18. Background Task
• rake tasks
• organize code in “lib/tasks”
• run with:
rake <command> RAILS_ENV=<environment>
Richard Schneeman - @ThinkBohemian
19. Background Task
• Example
• cleanup.rake
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
end
rake cleanup:old_tickets
Richard Schneeman - @ThinkBohemian
20. Background Task
• What if i don’t want to execute from command line?
• run task with a automation program
• Cron
• Monit
• God
Richard Schneeman - @ThinkBohemian
21. Cron
• Very reliable unix time scheduler
• Built into the OS
• Executes command line calls
• Smallest interval is 1 minute
• Requires full paths
Richard Schneeman - @ThinkBohemian
22. Cron
• Awesome, but verbose
• Use Whenever gem instead
Richard Schneeman - @ThinkBohemian
23. Monit
• Not installed on OS by default
• Monitors and Executes (cron only executes)
• Extra functionality - Sysadmin emails etc...
Richard Schneeman - @ThinkBohemian
24. God
• Written in ruby
• Very configurable
• can be memory
intensive in some
applications
sudo gem install god
Richard Schneeman - @ThinkBohemian
25. Background Processes
• More Options
• Workling/Starling
• Backgroundrb
Richard Schneeman - @ThinkBohemian
26. Modules (ruby)
• Add “Mixins” to your code
• Keep code seperate with different namespaces
• put them in your rails project under /lib
Richard Schneeman - @ThinkBohemian
27. Modules (ruby)
• Example Mixin:
• include adds instance methods
class Dog module AntiCheating
include AntiCheating def drug_test
end ...
end
end
puppy = Dog.new
puppy.drug_test
>> Passed
Richard Schneeman - @ThinkBohemian
28. Modules (ruby)
• Example Mixin 2:
class Dog module AntiCheating
include AntiCheating def self.cleanup( level)
end ...
end
end
dirtyDog = Dog.new
dirtyDog.cleanup
>> No Method Error
Richard Schneeman - @ThinkBohemian
29. Modules (ruby)
• Example Mixin 2:
module AntiCheating
def self.cleanup( level)
...
end
end
AntiCheating.cleanup(10)
>>Very Clean
Richard Schneeman - @ThinkBohemian
30. Modules (ruby)
• Example Mixin 2:
• extend adds all module methods
class Dog module AntiCheating
extend AntiCheating def self.cleanup( level)
end ...
end
end
dirtyDog = Dog.new
dirtyDog.cleanup(2)
>> Kinda Clean
Richard Schneeman - @ThinkBohemian
31. Modules (ruby)
• Extra Credit:
• class << self
class Dog class Dog
class << self def self.sniff
def sniff ...
... = end
end end
end
end
Dog.sniff
Richard Schneeman - @ThinkBohemian
32. Modules (ruby)
• Extra Credit:
• class << self
class Dog module AntiCheating
class << self def self.cleanup( level)
include AntiCheating ...
end end
end end
dirtyDog = Dog.new
dirtyDog.cleanup(2)
>> Kinda Clean
Richard Schneeman - @ThinkBohemian
33. Modules (ruby)
• Example Namespace
module FastDogs module SlowDogs
class Dog class Dog
... ...
end end
end end
lassie = FastDogs::Dog.new droopy = SlowDogs::Dog.new
Richard Schneeman - @ThinkBohemian
34. 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
Richard Schneeman - @ThinkBohemian
35. Callbacks
• Polling
• “Are We there Yet”
• Callback
• “I’ll tell you when we’re there”
Richard Schneeman - @ThinkBohemian
37. Callbacks in Rails
• Example
• before_destroy
class Topic < ActiveRecord::Base
before_destroy :delete_parents
def delete_parents
self.class.delete_all "parent_id = #{id}"
end
end
end
Richard Schneeman - @ThinkBohemian
38. Observers
• Add callback functionality without polluting the
model
• Will run after every new user instance is created
• Keeps your code clean(er)
Richard Schneeman - @ThinkBohemian
39. i18n & L10n
Source: @mattt
Richard Schneeman - @ThinkBohemian
40. i18n & L10n
Source: @mattt
Richard Schneeman - @ThinkBohemian
41. language configuration
Source: @mattt
Source: @mattt
Richard Schneeman - @ThinkBohemian