Ruby on Rails By Miguel Vega [email_address] http://www.upstrat.com/ and Ezwan Aizat bin Abdullah Faiz [email_address] http://rails.aizatto.com/ http://creativecommons.org/licenses/by/3.0/
Who Am I? Miguel Vega 1 ½ years doing Ruby and Rails PHP before then Started a company (upstrat) just to have an excuse to use rails.
Who Am I? Multiple Names Ezwan Aizat Bin Abdullah Faiz Aizat Faiz aizatto That scrubby looking guy on the screen Student Free and Open Source (FOSS) Enthusiast Local FOSS Champion http://foss.org.my Web Developer Ruby on Rails LAMP
What is Ruby on Rails?
Built on Ruby Programming Language
Web Application Framework
+ =
Wait! Unexpected Side Effect!
YOU =
 
Why Ruby?
A great man once said Often people, especially computer engineers, focus on the machines . They think, “By doing this, the machine will run faster . By doing this, the machine will run more effectively . By doing this, the machine will something something something ”
They are focusing on machines . But in fact we need to focus on humans , on how humans care about doing programming or operating the application of the machines. We are the masters . They are the slaves .
 
By doing this, the machine will something something something
We are the masters , they are the slaves Yukihiro Matsumoto Creator of Ruby
They are the SLAVES!
and I think we keep forgetting that
 
Ruby brings...
Large Increase in Productivity
How? Non traditional programming style
Everything is an Object string = String .new 5 .times do puts " Hello World " end # Hello World # Hello World # Hello World # Hello World # Hello World
Everything is an Object 1 .upto( 100 ) { | i | puts i } # 1 # 2 # 3 # ... # 100 3.141_592_65 .ceil # 4 2.71828182845905 .floor # 2
Blocks (aka Closures) patients.each do | patient | if patient.ill? physician.examine patient else patient.jump_for_joy! end end
Blocks (aka Closures) hello = Proc .new do | string | puts " Hello #{string.upcase}" end hello.call ' Aizat ' # Hello AIZAT hello.call ' World ' # Hello WORLD
Open Classes class String def sanitize self .gsub( / [^a-z1-9]+ /i , ' _ ' ) end end puts " Ruby gives you alot of $$$ yo! " .sanitize # Ruby_gives_you_alot_of_yo_
Open Classes class Array def rand self [ Kernel ::rand(size)] end end puts %w[ a b c d e f g ] .rand # "e"
Open Classes class Array def shuffle size.downto( 1 ) { | n | push delete_at( Kernel ::rand(n)) } self end end puts %w[ a b c d e f g ] .shuffle.inspect # ["c", "e", "f", "g", "b", "d", "a"]
Open Classes class Numeric def seconds self end def minutes self .seconds * 60 end def hours self .minutes * 60 end def days self .hours * 24 end def weeks self .days * 7 end alias second seconds alias minute minutes alias hour hours alias day days alias week weeks end
Open Classes 1 .second # 1 1 .minute # 60 7.5 .minutes # 450.0 3 .hours # 10800 10.75 hours # 38700 24 .hours # 86400 31 .days # 2678400 51 .weeks # 30844800 365 .days # 31536000 Time .now # Thu May 10 12:10:00 0800 2007 Time .now - 5 .hours # Thu May 10 07:10:00 0800 2007 Time .now - 3 .days # Mon May 07 12:10:00 0800 2007 Time .now - 1 .week # Thu May 03 12:10:00 0800 2007
This is how Rails does its magic
and more...
What is Rails? Convention over Configuration
MVC Model View Controller
M VC Model View Controller
ActiveRecord Object Relation Mapping (ORM) Class to Table, Object to Row CRUD simple Database Agnostic
ActiveRecord
ActiveRecord::Base#find class Patient < ActiveRecord :: Base end Patient .find( 1 ) # SELECT * FROM patients WHERE id = 1 Patient .find_by_name ' Miguel Vega ' # SELECT * FROM patients WHERE name = 'Miguel Vega' Patient .find_by_date_of_birth ' 1986-07-24 ' # SELECT * FROM patients WHERE date_of_birth = '1986-07-24' Patient .find_by_name_and_date_of_birth ' Miguel Vega ' , ' 1986-07-24 ' # SELECT * FROM patients WHERE name = 'Miguel Vega' AND date_of_birth = '1986-07-24'
ActiveRecord::Base#find Patient .count # SELECT COUNT(*) AS count Patient .find :all , :order => ' name DESC ' # SELECT * FROM patients ORDER BY name DESC Patient .find :all , :conditions => [ &quot; name LIKE ? &quot; , &quot; The other guy &quot; ] # SELECT * FROM patients WHERE name = 'The other guy'
Models class Patient < ActiveRecord :: Base end class Encounter < ActiveRecord :: Base end class Physican < ActiveRecord :: Base end
Associations class Patient < ActiveRecord :: Base has_many :encounters has_many :physicans , :through => :encounters end class Encounter < ActiveRecord :: Base belongs_to :patient belongs_to :physician end class Physican < ActiveRecord :: Base has_many :encounters has_many :patients , :through => :encounters end
Smart Defaults class Patient < ActiveRecord :: Base has_many :encounters , :class_name => ' Encounter ' , :foreign_key => ' patient_id ' has_many :physicans , :through => :encounters , :class_name => ' Physician ' , :foreign_key => ' physician_id ' end class Encounter < ActiveRecord :: Base belongs_to :patient , :class_name => ' Patient ' , :foreign_key => ' patient_id ' belongs_to :physician , :class_name => ' Physician ' , :foreign_key => ' physician_id ' end class Physican < ActiveRecord :: Base has_many :encounters , :class_name => ' Encounter ' , :foreign_key => ' patient_id ' has_many :patients , :through => :encounters , :class_name => ' Patient ' , :foreign_key => ' patient_id ' end
Or what people like to call Convention over Configuration
Associations p = Patient .find :first # SELECT * FROM patients LIMIT 1 p.encounters.count # SELECT COUNT(*) AS count FROM encounters WHERE (patient_id = 1) p.encounters.find :condition => [ ' date <= ? ' Time .now - 12 .weeks] # SELECT * FROM encounters WHERE date <= '2007-02-13 16:19:29' AND (patient_id = 1)
Validations class Patient < ActiveRecord :: Base has_many :encounters has_many :physicans , :through => :encounters validates_presence_of :name validates_inclusion_of :gender , :in => [ ' male ' , ' female ' ] validates_email_of :email validates_numericality_of :age validates_confirmation_of :password end
MV C Model View Controller
ActionController Separation of business logic and presentation Ideally there should be no logic in the view
ActionController class PatientController < ApplicationController def index @patient = Patient .find :first @title = ' Patient Detail ' @homepage_title = &quot; Patient: #{@patient.name}&quot; end end
M V C Model View Controller
View < html > < head > < title > <%= @title %> </ title > </ head > < body > < h1 > <%= @homepage_title %> </ h1 > < b > Patient: </ b > < dl > < dt > Name </ dt > < dd > <%= @patient .name %> </ dd > </ dl > <%= render :partial => ' patient_details ' %> </ body > </ html >
Why Rails?
Convention over Configuration
Easy SQL ActiveRecord::Base#find
Easy SQL Even through associations
RAD/RID Rapid Application Development Rapid Iterative Development
Testing Framework Built in Unit, Function, Integration testing
Awesome Plugin Structure
Simpler and Nicer looking Code
Web 2.0 Ready Tagging, Folksonomy, Ajax, REST
Simply RESTful
DRY Don't Repeat Yourself
DRY Don't Repeat Yourself
DRY Saves time Easier to manage Reusable Code Best of all...
You don't look like a code monkey
Most Importantly
Because it's FUN
Questions?
Thank you amcvega@gmail.com - aizat.faiz@gmail.com Images used are from Tango Desktop Project http://tango.freedesktop.org/

Ruby on Rails

  • 1.
    Ruby on RailsBy Miguel Vega [email_address] http://www.upstrat.com/ and Ezwan Aizat bin Abdullah Faiz [email_address] http://rails.aizatto.com/ http://creativecommons.org/licenses/by/3.0/
  • 2.
    Who Am I?Miguel Vega 1 ½ years doing Ruby and Rails PHP before then Started a company (upstrat) just to have an excuse to use rails.
  • 3.
    Who Am I?Multiple Names Ezwan Aizat Bin Abdullah Faiz Aizat Faiz aizatto That scrubby looking guy on the screen Student Free and Open Source (FOSS) Enthusiast Local FOSS Champion http://foss.org.my Web Developer Ruby on Rails LAMP
  • 4.
    What is Rubyon Rails?
  • 5.
    Built on RubyProgramming Language
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    A great manonce said Often people, especially computer engineers, focus on the machines . They think, “By doing this, the machine will run faster . By doing this, the machine will run more effectively . By doing this, the machine will something something something ”
  • 13.
    They are focusing on machines . But in fact we need to focus on humans , on how humans care about doing programming or operating the application of the machines. We are the masters . They are the slaves .
  • 14.
  • 15.
    By doing this,the machine will something something something
  • 16.
    We are the masters , they are the slaves Yukihiro Matsumoto Creator of Ruby
  • 17.
    They are the SLAVES!
  • 18.
    and I think we keep forgetting that
  • 19.
  • 20.
  • 21.
    Large Increase inProductivity
  • 22.
    How? Non traditionalprogramming style
  • 23.
    Everything is anObject string = String .new 5 .times do puts &quot; Hello World &quot; end # Hello World # Hello World # Hello World # Hello World # Hello World
  • 24.
    Everything is anObject 1 .upto( 100 ) { | i | puts i } # 1 # 2 # 3 # ... # 100 3.141_592_65 .ceil # 4 2.71828182845905 .floor # 2
  • 25.
    Blocks (aka Closures)patients.each do | patient | if patient.ill? physician.examine patient else patient.jump_for_joy! end end
  • 26.
    Blocks (aka Closures)hello = Proc .new do | string | puts &quot; Hello #{string.upcase}&quot; end hello.call ' Aizat ' # Hello AIZAT hello.call ' World ' # Hello WORLD
  • 27.
    Open Classes class String def sanitize self .gsub( / [^a-z1-9]+ /i , ' _ ' ) end end puts &quot; Ruby gives you alot of $$$ yo! &quot; .sanitize # Ruby_gives_you_alot_of_yo_
  • 28.
    Open Classes class Array def rand self [ Kernel ::rand(size)] end end puts %w[ a b c d e f g ] .rand # &quot;e&quot;
  • 29.
    Open Classes class Array def shuffle size.downto( 1 ) { | n | push delete_at( Kernel ::rand(n)) } self end end puts %w[ a b c d e f g ] .shuffle.inspect # [&quot;c&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;b&quot;, &quot;d&quot;, &quot;a&quot;]
  • 30.
    Open Classes class Numeric def seconds self end def minutes self .seconds * 60 end def hours self .minutes * 60 end def days self .hours * 24 end def weeks self .days * 7 end alias second seconds alias minute minutes alias hour hours alias day days alias week weeks end
  • 31.
    Open Classes 1.second # 1 1 .minute # 60 7.5 .minutes # 450.0 3 .hours # 10800 10.75 hours # 38700 24 .hours # 86400 31 .days # 2678400 51 .weeks # 30844800 365 .days # 31536000 Time .now # Thu May 10 12:10:00 0800 2007 Time .now - 5 .hours # Thu May 10 07:10:00 0800 2007 Time .now - 3 .days # Mon May 07 12:10:00 0800 2007 Time .now - 1 .week # Thu May 03 12:10:00 0800 2007
  • 32.
    This is howRails does its magic
  • 33.
  • 34.
    What is Rails?Convention over Configuration
  • 35.
    MVC Model ViewController
  • 36.
    M VC Model View Controller
  • 37.
    ActiveRecord Object RelationMapping (ORM) Class to Table, Object to Row CRUD simple Database Agnostic
  • 38.
  • 39.
    ActiveRecord::Base#find class Patient < ActiveRecord :: Base end Patient .find( 1 ) # SELECT * FROM patients WHERE id = 1 Patient .find_by_name ' Miguel Vega ' # SELECT * FROM patients WHERE name = 'Miguel Vega' Patient .find_by_date_of_birth ' 1986-07-24 ' # SELECT * FROM patients WHERE date_of_birth = '1986-07-24' Patient .find_by_name_and_date_of_birth ' Miguel Vega ' , ' 1986-07-24 ' # SELECT * FROM patients WHERE name = 'Miguel Vega' AND date_of_birth = '1986-07-24'
  • 40.
    ActiveRecord::Base#find Patient .count# SELECT COUNT(*) AS count Patient .find :all , :order => ' name DESC ' # SELECT * FROM patients ORDER BY name DESC Patient .find :all , :conditions => [ &quot; name LIKE ? &quot; , &quot; The other guy &quot; ] # SELECT * FROM patients WHERE name = 'The other guy'
  • 41.
    Models class Patient < ActiveRecord :: Base end class Encounter < ActiveRecord :: Base end class Physican < ActiveRecord :: Base end
  • 42.
    Associations class Patient < ActiveRecord :: Base has_many :encounters has_many :physicans , :through => :encounters end class Encounter < ActiveRecord :: Base belongs_to :patient belongs_to :physician end class Physican < ActiveRecord :: Base has_many :encounters has_many :patients , :through => :encounters end
  • 43.
    Smart Defaults class Patient < ActiveRecord :: Base has_many :encounters , :class_name => ' Encounter ' , :foreign_key => ' patient_id ' has_many :physicans , :through => :encounters , :class_name => ' Physician ' , :foreign_key => ' physician_id ' end class Encounter < ActiveRecord :: Base belongs_to :patient , :class_name => ' Patient ' , :foreign_key => ' patient_id ' belongs_to :physician , :class_name => ' Physician ' , :foreign_key => ' physician_id ' end class Physican < ActiveRecord :: Base has_many :encounters , :class_name => ' Encounter ' , :foreign_key => ' patient_id ' has_many :patients , :through => :encounters , :class_name => ' Patient ' , :foreign_key => ' patient_id ' end
  • 44.
    Or what peoplelike to call Convention over Configuration
  • 45.
    Associations p = Patient .find :first # SELECT * FROM patients LIMIT 1 p.encounters.count # SELECT COUNT(*) AS count FROM encounters WHERE (patient_id = 1) p.encounters.find :condition => [ ' date <= ? ' Time .now - 12 .weeks] # SELECT * FROM encounters WHERE date <= '2007-02-13 16:19:29' AND (patient_id = 1)
  • 46.
    Validations class Patient < ActiveRecord :: Base has_many :encounters has_many :physicans , :through => :encounters validates_presence_of :name validates_inclusion_of :gender , :in => [ ' male ' , ' female ' ] validates_email_of :email validates_numericality_of :age validates_confirmation_of :password end
  • 47.
    MV C ModelView Controller
  • 48.
    ActionController Separation ofbusiness logic and presentation Ideally there should be no logic in the view
  • 49.
    ActionController class PatientController < ApplicationController def index @patient = Patient .find :first @title = ' Patient Detail ' @homepage_title = &quot; Patient: #{@patient.name}&quot; end end
  • 50.
    M V CModel View Controller
  • 51.
    View < html> < head > < title > <%= @title %> </ title > </ head > < body > < h1 > <%= @homepage_title %> </ h1 > < b > Patient: </ b > < dl > < dt > Name </ dt > < dd > <%= @patient .name %> </ dd > </ dl > <%= render :partial => ' patient_details ' %> </ body > </ html >
  • 52.
  • 53.
  • 54.
  • 55.
    Easy SQL Eventhrough associations
  • 56.
    RAD/RID Rapid ApplicationDevelopment Rapid Iterative Development
  • 57.
    Testing Framework Builtin Unit, Function, Integration testing
  • 58.
  • 59.
    Simpler and Nicerlooking Code
  • 60.
    Web 2.0 ReadyTagging, Folksonomy, Ajax, REST
  • 61.
  • 62.
  • 63.
  • 64.
    DRY Saves timeEasier to manage Reusable Code Best of all...
  • 65.
    You don't looklike a code monkey
  • 66.
  • 67.
  • 68.
  • 69.
    Thank you amcvega@gmail.com- aizat.faiz@gmail.com Images used are from Tango Desktop Project http://tango.freedesktop.org/