SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
Beginner to Builder
                           Week 6




June, 2011
Wednesday, July 20, 2011
Rails - Week 6
                    • Email in Rails
                    • Background Tasks
                    • modules
                    • Observers & Callbacks
                    • Internationalization & Localization
                      • I18n & L10n

@Schneems
Wednesday, July 20, 2011
Email in Rails
        • What is Email?
        • Why Use Email?
        • How does Rails use email?




@Schneems
Wednesday, July 20, 2011
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)




@Schneems
Wednesday, July 20, 2011
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


@Schneems
Wednesday, July 20, 2011
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



@Schneems
Wednesday, July 20, 2011
How Does RoR Send
                  Email?                            Mail Server

                   • Low Volume
                                                                  Operating System
                                           Your
                                         Computer                       MTA


                     • use Gmail                                        Postfix
                                                                        SMTP


                   • High Volume
                                                                    (Sends d
                                                                           Emails)

                                                      Their
                                                    Computer          Courier

                     • use a re-mailer                               IMAP/POP




                     • Build your own


@Schneems
Wednesday, July 20, 2011
How Does RoR Send
      •      ActionMailer

           •       Mail Gem


                           rails generate mailer Notifier




                                     /app/mailers/notifier.rb



@Schneems
Wednesday, July 20, 2011
Email With Rails




        Notifier.signup_notification(“foo@example.com”).deliver
@Schneems
Wednesday, July 20, 2011
Email With Rails
                                         • Default Mailer
                                         Settings


                                         • In-Line Attachments

                                         • mail( ) method

        Notifier.signup_notification(“foo@example.com”).deliver
@Schneems
Wednesday, July 20, 2011
Email With Rails
                                         • Default Mailer
                                         Settings


                                         • In-Line Attachments

                                         • mail( ) method

        Notifier.signup_notification(“foo@example.com”).deliver
@Schneems
Wednesday, July 20, 2011
Email With Rails
                                         • Default Mailer
                                         Settings


                                         • In-Line Attachments

                                         • mail( ) method

        Notifier.signup_notification(“foo@example.com”).deliver
@Schneems
Wednesday, July 20, 2011
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
                testing


 Notifier.signup_notification(“foo@example.com”).deliver
@Schneems
Wednesday, July 20, 2011
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




@Schneems
Wednesday, July 20, 2011
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?



@Schneems
Wednesday, July 20, 2011
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
@Schneems
Wednesday, July 20, 2011
Background Task
    • rake tasks
     • organize code in “lib/tasks”
       • run with:

                           rake <command> RAILS_ENV=<environment>




@Schneems
Wednesday, July 20, 2011
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
           end


                                rake cleanup:old_tickets

@Schneems
Wednesday, July 20, 2011
Background Task
    • What if i don’t want to execute from
      command line?
     • run task with a automation program
       • Cron
       • Monit
       • God


@Schneems
Wednesday, July 20, 2011
Cron
    • Very reliable unix time scheduler
    • Built into the OS
     • Executes command line calls
     • Smallest interval is 1 minute
     • Requires full paths


@Schneems
Wednesday, July 20, 2011
Monit
        • Not installed on OS by default
          • Monitors and Executes (cron only executes)
        • Extra functionality - Sysadmin emails etc...




@Schneems
Wednesday, July 20, 2011
God	
        • Written in ruby
        • Very configurable
        • can be memory
          intensive in some
                applications




                           sudo gem install god


@Schneems
Wednesday, July 20, 2011
Background
        • More Options
          • Workling/Starling
          • Backgroundrb
          • ResQue



@Schneems
Wednesday, July 20, 2011
Modules (ruby)
    • Add “Mixins” to your code
    • Keep code seperate with different namespaces
    • put them in your rails project under /lib




@Schneems
Wednesday, July 20, 2011
Modules (ruby)
   • Example Mixin:
    • include adds instance methods
                                             module AntiCheating
        class Dog
                                               def drug_test
          include AntiCheating
                                                 ...
        end
                                               end
                                             end



                           puppy = Dog.new
                           puppy.drug_test
                           >> Passed
@Schneems
Wednesday, July 20, 2011
Modules (ruby)
   • Example Mixin 2:
                                           module AntiCheating
    class Dog
                                             def self.cleanup(level)
      include AntiCheating
                                               ...
    end
                                             end
                                           end

                           dirtyDog = Dog.new
                           dirtyDog.cleanup
                           >> No Method Error

@Schneems
Wednesday, July 20, 2011
Modules (ruby)
   • Example Mixin 2:
                           module AntiCheating
                             def self.cleanup(level)
                               ...
                             end
                           end


                              AntiCheating.cleanup
                              >> Very Clean



@Schneems
Wednesday, July 20, 2011
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”

@Schneems
Wednesday, July 20, 2011
Modules (ruby)
   • Extra Credit:
    • class << self
                           class Dog
                                                  class Dog
                             class << self
                                                    def self.sniff
                               def sniff
                               ...           ==     ...
                                                    end
                               end
                                                  end
                             end
                           end



@Schneems
Wednesday, July 20, 2011
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(10)
                           >> “Really Clean”
@Schneems
Wednesday, July 20, 2011
Modules (ruby)
   • Example Namespace
                           module FastDogs       module SlowDogs
                             class Dog             class Dog
                             ...                   ...
                             end                   end
                           end                   end


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




@Schneems
Wednesday, July 20, 2011
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




@Schneems
Wednesday, July 20, 2011
Callbacks
    • Polling
     • “Are We there Yet”
    • Callback
     • “I’ll tell you when we’re there”



@Schneems
Wednesday, July 20, 2011
Callbacks in Rails
                               save
        • Lifecycle Hooks =>   (-) 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

@Schneems
Wednesday, July 20, 2011
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



@Schneems
Wednesday, July 20, 2011
Observers
        • Add callback functionality without polluting the
                model




        • Will run after every new user instance is created
        • Keeps your code clean(er)
@Schneems
Wednesday, July 20, 2011
i18n & L10n




                            Source: @mattt
@Schneems
Wednesday, July 20, 2011
i18n & L10n




                           Source: @mattt

@Schneems
Wednesday, July 20, 2011
language configuration
                                            Source: @mattt




                           Source: @mattt

@Schneems
Wednesday, July 20, 2011
Multiple Language Files
                           fr.yml




                                    ja.yml




                                       Source: @mattt
@Schneems
Wednesday, July 20, 2011
It Worked!




                                  Source: @mattt
@Schneems
Wednesday, July 20, 2011
What about L10n




@Schneems
Wednesday, July 20, 2011
Questions?
                           http://guides.rubyonrails.org
                            http://stackoverflow.com
                               http://peepcode.com


@Schneems
Wednesday, July 20, 2011

Contenu connexe

En vedette

AJAX?? Não!! Asynchronous Javascript and... JSON!!
AJAX?? Não!! Asynchronous Javascript and... JSON!!AJAX?? Não!! Asynchronous Javascript and... JSON!!
AJAX?? Não!! Asynchronous Javascript and... JSON!!Lucas Brasilino
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Richard Schneeman
 
Minicurso JSON & Ajax
Minicurso JSON & AjaxMinicurso JSON & Ajax
Minicurso JSON & AjaxWilker Iceri
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentationadamcookeuk
 

En vedette (6)

AJAX?? Não!! Asynchronous Javascript and... JSON!!
AJAX?? Não!! Asynchronous Javascript and... JSON!!AJAX?? Não!! Asynchronous Javascript and... JSON!!
AJAX?? Não!! Asynchronous Javascript and... JSON!!
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
 
Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2
 
Minicurso JSON & Ajax
Minicurso JSON & AjaxMinicurso JSON & Ajax
Minicurso JSON & Ajax
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 

Similaire à Rails 3 Beginner to Builder 2011 Week 6

Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Reuven Lerner
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Reuven Lerner
 
Gaelyk - SpringOne2GX - 2010 - Guillaume Laforge
Gaelyk - SpringOne2GX - 2010 - Guillaume LaforgeGaelyk - SpringOne2GX - 2010 - Guillaume Laforge
Gaelyk - SpringOne2GX - 2010 - Guillaume LaforgeGuillaume Laforge
 
Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Yahoo Developer Network
 
Javascript Views, Client-side or Server-side with NodeJS
Javascript Views, Client-side or Server-side with NodeJSJavascript Views, Client-side or Server-side with NodeJS
Javascript Views, Client-side or Server-side with NodeJSSylvain Zimmer
 
Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Bachkoutou Toutou
 
AppScale Talk at SBonRails
AppScale Talk at SBonRailsAppScale Talk at SBonRails
AppScale Talk at SBonRailsChris Bunch
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterJohn Adams
 
Scoping call presentation_rev_4_mary
Scoping call presentation_rev_4_maryScoping call presentation_rev_4_mary
Scoping call presentation_rev_4_maryrhochambeau32
 
Inside Wordnik's Architecture
Inside Wordnik's ArchitectureInside Wordnik's Architecture
Inside Wordnik's ArchitectureTony Tam
 
SharePoint SpeedMetal Admin 101 - SPSDEN
SharePoint SpeedMetal Admin 101 - SPSDENSharePoint SpeedMetal Admin 101 - SPSDEN
SharePoint SpeedMetal Admin 101 - SPSDENChris McNulty
 
"How Mozilla Uses Selenium"
"How Mozilla Uses Selenium""How Mozilla Uses Selenium"
"How Mozilla Uses Selenium"Stephen Donner
 
Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Richard Schneeman
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionAhmed Swilam
 
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)Ingo Renner
 
Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti
 
How I stopped worrying about and loved DumpRenderTree
How I stopped worrying about and loved DumpRenderTreeHow I stopped worrying about and loved DumpRenderTree
How I stopped worrying about and loved DumpRenderTreeHajime Morrita
 
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...Joshua Kamdjou
 

Similaire à Rails 3 Beginner to Builder 2011 Week 6 (20)

Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
 
library h3lp
library h3lplibrary h3lp
library h3lp
 
Gaelyk - SpringOne2GX - 2010 - Guillaume Laforge
Gaelyk - SpringOne2GX - 2010 - Guillaume LaforgeGaelyk - SpringOne2GX - 2010 - Guillaume Laforge
Gaelyk - SpringOne2GX - 2010 - Guillaume Laforge
 
Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010
 
Javascript Views, Client-side or Server-side with NodeJS
Javascript Views, Client-side or Server-side with NodeJSJavascript Views, Client-side or Server-side with NodeJS
Javascript Views, Client-side or Server-side with NodeJS
 
Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011
 
AppScale Talk at SBonRails
AppScale Talk at SBonRailsAppScale Talk at SBonRails
AppScale Talk at SBonRails
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling Twitter
 
Scoping call presentation_rev_4_mary
Scoping call presentation_rev_4_maryScoping call presentation_rev_4_mary
Scoping call presentation_rev_4_mary
 
Inside Wordnik's Architecture
Inside Wordnik's ArchitectureInside Wordnik's Architecture
Inside Wordnik's Architecture
 
SharePoint SpeedMetal Admin 101 - SPSDEN
SharePoint SpeedMetal Admin 101 - SPSDENSharePoint SpeedMetal Admin 101 - SPSDEN
SharePoint SpeedMetal Admin 101 - SPSDEN
 
"How Mozilla Uses Selenium"
"How Mozilla Uses Selenium""How Mozilla Uses Selenium"
"How Mozilla Uses Selenium"
 
Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web IntroductionClass 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
 
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)
Apache Solr for TYPO3 (@ T3CON10 Dallas, TX)
 
Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1
 
How I stopped worrying about and loved DumpRenderTree
How I stopped worrying about and loved DumpRenderTreeHow I stopped worrying about and loved DumpRenderTree
How I stopped worrying about and loved DumpRenderTree
 
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...
Voight-Kampff for Email Addresses: Quantifying Email Address Reputation to Id...
 

Plus de Richard Schneeman

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLRichard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Richard Schneeman
 
Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Richard Schneeman
 

Plus de Richard Schneeman (8)

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQL
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5
 
UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 2
UT on Rails3 2010- Week 2UT on Rails3 2010- Week 2
UT on Rails3 2010- Week 2
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 

Dernier

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Dernier (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Rails 3 Beginner to Builder 2011 Week 6

  • 1. Beginner to Builder Week 6 June, 2011 Wednesday, July 20, 2011
  • 2. Rails - Week 6 • Email in Rails • Background Tasks • modules • Observers & Callbacks • Internationalization & Localization • I18n & L10n @Schneems Wednesday, July 20, 2011
  • 3. Email in Rails • What is Email? • Why Use Email? • How does Rails use email? @Schneems Wednesday, July 20, 2011
  • 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) @Schneems Wednesday, July 20, 2011
  • 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 @Schneems Wednesday, July 20, 2011
  • 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 @Schneems Wednesday, July 20, 2011
  • 7. How Does RoR Send Email? Mail Server • Low Volume Operating System Your Computer MTA • use Gmail Postfix SMTP • High Volume (Sends d Emails) Their Computer Courier • use a re-mailer IMAP/POP • Build your own @Schneems Wednesday, July 20, 2011
  • 8. How Does RoR Send • ActionMailer • Mail Gem rails generate mailer Notifier /app/mailers/notifier.rb @Schneems Wednesday, July 20, 2011
  • 9. Email With Rails Notifier.signup_notification(“foo@example.com”).deliver @Schneems Wednesday, July 20, 2011
  • 10. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver @Schneems Wednesday, July 20, 2011
  • 11. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver @Schneems Wednesday, July 20, 2011
  • 12. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver @Schneems Wednesday, July 20, 2011
  • 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 testing Notifier.signup_notification(“foo@example.com”).deliver @Schneems Wednesday, July 20, 2011
  • 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 ✦ No longer running @Schneems Wednesday, July 20, 2011
  • 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? @Schneems Wednesday, July 20, 2011
  • 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 @Schneems Wednesday, July 20, 2011
  • 17. Background Task • rake tasks • organize code in “lib/tasks” • run with: rake <command> RAILS_ENV=<environment> @Schneems Wednesday, July 20, 2011
  • 18. 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 end rake cleanup:old_tickets @Schneems Wednesday, July 20, 2011
  • 19. Background Task • What if i don’t want to execute from command line? • run task with a automation program • Cron • Monit • God @Schneems Wednesday, July 20, 2011
  • 20. Cron • Very reliable unix time scheduler • Built into the OS • Executes command line calls • Smallest interval is 1 minute • Requires full paths @Schneems Wednesday, July 20, 2011
  • 21. Monit • Not installed on OS by default • Monitors and Executes (cron only executes) • Extra functionality - Sysadmin emails etc... @Schneems Wednesday, July 20, 2011
  • 22. God • Written in ruby • Very configurable • can be memory intensive in some applications sudo gem install god @Schneems Wednesday, July 20, 2011
  • 23. Background • More Options • Workling/Starling • Backgroundrb • ResQue @Schneems Wednesday, July 20, 2011
  • 24. Modules (ruby) • Add “Mixins” to your code • Keep code seperate with different namespaces • put them in your rails project under /lib @Schneems Wednesday, July 20, 2011
  • 25. Modules (ruby) • Example Mixin: • include adds instance methods module AntiCheating class Dog def drug_test include AntiCheating ... end end end puppy = Dog.new puppy.drug_test >> Passed @Schneems Wednesday, July 20, 2011
  • 26. Modules (ruby) • Example Mixin 2: module AntiCheating class Dog def self.cleanup(level) include AntiCheating ... end end end dirtyDog = Dog.new dirtyDog.cleanup >> No Method Error @Schneems Wednesday, July 20, 2011
  • 27. Modules (ruby) • Example Mixin 2: module AntiCheating def self.cleanup(level) ... end end AntiCheating.cleanup >> Very Clean @Schneems Wednesday, July 20, 2011
  • 28. 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” @Schneems Wednesday, July 20, 2011
  • 29. Modules (ruby) • Extra Credit: • class << self class Dog class Dog class << self def self.sniff def sniff ... == ... end end end end end @Schneems Wednesday, July 20, 2011
  • 30. 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(10) >> “Really Clean” @Schneems Wednesday, July 20, 2011
  • 31. Modules (ruby) • Example Namespace module FastDogs module SlowDogs class Dog class Dog ... ... end end end end lassie = FastDogs::Dog.new droopy = SlowDogs::Dog.new @Schneems Wednesday, July 20, 2011
  • 32. 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 @Schneems Wednesday, July 20, 2011
  • 33. Callbacks • Polling • “Are We there Yet” • Callback • “I’ll tell you when we’re there” @Schneems Wednesday, July 20, 2011
  • 34. Callbacks in Rails save • Lifecycle Hooks => (-) 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 @Schneems Wednesday, July 20, 2011
  • 35. 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 @Schneems Wednesday, July 20, 2011
  • 36. Observers • Add callback functionality without polluting the model • Will run after every new user instance is created • Keeps your code clean(er) @Schneems Wednesday, July 20, 2011
  • 37. i18n & L10n Source: @mattt @Schneems Wednesday, July 20, 2011
  • 38. i18n & L10n Source: @mattt @Schneems Wednesday, July 20, 2011
  • 39. language configuration Source: @mattt Source: @mattt @Schneems Wednesday, July 20, 2011
  • 40. Multiple Language Files fr.yml ja.yml Source: @mattt @Schneems Wednesday, July 20, 2011
  • 41. It Worked! Source: @mattt @Schneems Wednesday, July 20, 2011
  • 43. Questions? http://guides.rubyonrails.org http://stackoverflow.com http://peepcode.com @Schneems Wednesday, July 20, 2011