SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Downloaden Sie, um offline zu lesen
Ruby on Rails

Rapid, solid, flexible application
         development
I am ...
Bálint Érdi
(Budapest, Hungary)

blog: http://bucionrails.com
email: balint@bucionrails.com

My evolution :)

   java web developer
   python web developer
   (freelance) ruby web developer and consultant
(with occasional non-web works)
Hungary
Legend

Facts: (Wisdom :) )
   Ruby is object oriented

Code:
  5.times { print "Hello Fluidtime!" }
Outline

1. Ruby basic concepts

2. Rails
    History & philosophy
    Basic modules
    Introducing each module
    Other features (a selection of them)
    Who uses Rails?

3. Demo app

Questions are welcome anytime
Ruby: History

 created by Yukihiro Matsumoto ~1995
 it is a blend of his favorite languages (Smalltalk, Lisp, Perl,
 ADA)
 open source [RUBY]
 now at 1.9
Ruby is object oriented
  But it really is
  Everything is an object
  Every object has a class
  Classes are objects, too

5.times { print "Hello, Fluidtime!" }
3 + 2 # => 5
3.+(2) # => 5
3.send(:+, 2) # => 5
Ruby is duck-typed (dynamically typed)

  behavior is more important than type ("if it walks like a duck
  and talks like a duck, it is a duck")
  no type declarations are needed

a = 3
a + 2 # => 5
a + " jeff" # => TypeError

a = "hello"
a + 2 # => TypeError
a + " jeff" # =>"hello
jeff"
Ruby is functional programming like

Functional programming
   methods are first-class citizens of the language
   lets you concentrate on what you want instead of how you
   want to do it (because "for" loops are ugly)
   Helps keep the code cleaner, more elegant and less error-
   prone.
   e.g Lisp

[119, 43, 982, 266].select { |n| n % 7 == 0 }
["voodoo", "kayak", "light"].find { |w| w
=~ /^(.).*1$/ }
(1..4).map { |n| n**2 }
Ruby has strong metaprogramming
features
 define methods at runtime, on the fly (define_method)
 evaluate code in the context of an instance or a class
 (instance_eval, class_eval)
 get in control when the object does not have a certain
 method (method_missing)
 ... or when a name is not found(const_missing)
Ruby has open classes, all of them
  Any class can be reopened, "core" classes, too
  you can define new methods, override new ones, etc.
  class definitions can thus span files

class Numeric
    def plus(n)
      self.+(n)
    end
end

3.plus(2) # => 5
Rails résumé

Rails is a powerful yet simple web framework written in Ruby
   History
   Philosophy
   Introducing the main building blocks
   Other included features
   Extending Rails functionality
   Who uses Rails?
History

  created by David Heinemeier Hansson, ~2003 [ROR]
  extracted framework from a project called Basecamp
  open source, community driven, ~1400 contributors during
  its history
  now at 2.3, going for 3.0
Rails basic ideology
  Very opinionated: encourages "best practices"
  "Convention over configuration" to avoid bloatware and
  keep things simple in most cases
  Possibility to change default behavior in special cases
  Sugar and vinegar: easy to do what is encouraged, possible
  (but probably not simple) to do "unrecommended" things.
   Test Driven Development (TDD) is extremely important
  (writing tests first, so an automated test suit that can be
  easily -and quickly- run is needed)
  The framework has to stay small and lean but provide a
  mechanism for easy expansion.
Rails: Consequences of the ideology

Why is all this important?

   enables you to be more productive by following the
   "guidelines"
   enables you to concentrate on the business logic without
   worrying about the technical details
   you will feel happy because it is so elegant and and feeling
   good!
   you will feel good because it is so efficient
Rails main building blocks

  Model - View - Controller (MVC) framework
     Model: ActiveRecord
     Controller: ActionController
     View: ActionView
  Clear separation of concerns
  Additional modules
     ActionSupport for supporting libs
     ActiveResource for REST compliance
     ActionMailer for sending emails
The model: ActiveRecord

  Database engine agnosticism
  Database migrations
  Associations between models
  Validations
  Callbacks
ActiveRecord: database agnosticism

  it is an ORM (Object Relational Mapping)
  you should not have to worry about what lies underneath
  you should not have to write SQL
  Adapters exist for sqlite, mysql, postgresql

Post.find(:all, :conditions => [ "created_at >
?", 2.days.ago ])
ActiveRecord: Database migrations
to make sure
   ... not just the code, but the database schema is tracked,
   too
   ... all developers work with the same schema
   ... you don't have to write raw SQL
    .. you are able to jump to any version, just like with a SCM
   ... deployment stays simple: you just run the migrations

Examples of what a migration could do:

   creating a posts table
   adding a user_id to the posts table
   changing column type from boolean to string
ActiveRecord: Associations 1.
class Blog < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :blog
  has_many :comments
end

* using the control that Ruby gives for finding names
(const_missing)
ActiveRecord: Associations 2.


   reaching associated object(s):

blog.posts.find(:first, :conditions =>
["updated_at > 1.week.ago])

   creating associated object(s):

post.comments.create(:user => me, :body =>
"That's a great post, congratulations!")

* leveraging Ruby metaprogramming capabilities
ActiveRecord: Validations

  validation is business logic so it belongs to the model
  use the provided validators which cover most of the cases

    validates_presence_of :login
    validates_format_of :zip_code, :with =>
/^d{5}$/

  roll your own for the special cases
ActiveRecord: Callbacks
 Entry points (hooks) that implement business logic at
 different during the lifecycle of the object
 lifecycle events: create, save, update, validate, destroy
 provided methods: before_save, after_create, ...
 good for setting default values, set calculated attributes,
 send out emails, etc.
ActionController: Résumé

  Basic buildup
  Filters
  Rendering from controller actions
ActionController: Basic buildup
The controller should be thin and should only contain:
   ... actions (methods) that compose the API
   ... filter calls
   ... the actual filter implementations

Actions
   new
   create
   edit
   destroy
   ...
ActionController: Filters
Filters are

   ... methods that run before, after or around actions
   ... prevalently used for checking authorization to resources
    ... used to set variables for templates in a DRY way
ActionController: Rendering

The controller sets the variables that the view template will use
and does the actual rendering

   By default, the view with the name of the action is rendered
   But it can also render
      any action by name
      a text (mainly for debugging purposes)
      other representations of resources (e.g js, json, xml, etc.)
      nothing (just returning something to the caller)
ActionView: Résumé

 Basics
 Partials & content blocks
ActionView: Basics
The view:
  ... is a representation
  ... should contain the least possible amount of business
  logic
  ... is rendered by the ERB template language which can be
  changed with plugins (check out haml! [HAML])

  Lots of view helpers are provided for generating html
  some helpers (e.g form_for) are context-sensitive so
  views can be reused without cluttering them with conditions
ActionView: Partials & Content blocks
Partials

   chunks of reusable code you can render from other views

Content blocks

   separate definition of the building blocks of a page and
   actually generating them.
   make it possible to break the link between the order of html
   in the template and their order on the generated page
   are ideal for setting a subsection in the html title of a page
   injecting per-page javascripts
A selection of Rails' features

  Sending emails with ActionMailer
  Context-dependent configurations
  Routing
  RESTful applications
  Internationalization & Localization
  Generators
  Caching
  Extending core functionality
  Handling dependencies
Sending emails with ActionMailer

 mailers are models
 one email template (a view template) for each email type
 one method in the mailer model for each email type that
 sets the variables for rendering the email
 sending out an email is calling a method on the mailer
 there is convention for finding the template name from the
 method's name
Context dependent configurations
(environments)
Development: used for development with throwaway data

Test: used for automated testing, records are created and
destroyed by each test run.

Production: used by the live application. Handle with care.

   Common configuration file + each env. has its own config
   which overwrites the values set in the common one.
   Additional environments can easily be set up
   Example: emails should be sent out in production, examined
   but not sent out when testing and it is up to you in
   development
Router

  generates user-friendly names you can use in your code (e.
  g dashboard_path and dashboard_url)
  makes your routes DRY. You don't have to put hardwired
  routes into your application
  makes dangling links nearly impossible
  routes incoming requests to the appropriate action
  it is a SEO tool

map.dashboard '/', :controller => 'dashboard',
:action => 'show'

map.search '/search/:term', :controller =>
"search", :action => "index", :term => /w*/
RESTful applications

  "HTTP is actually a general purpose protocol for applying
  verbs to nouns" [REST]
  encouraged as best practice
  declaring a resource generates the RESTful routes

map.resources :users

verb   path          generated path name
GET    /users/new     new_user_path
POST /users           users_path
GET   /users/:id/edit edit_user_path(id)
PUT   /users/:id      user_path(id)
GET   /users           users_path
DELETE /users/:id      user_path(id)
Internationalization (I18n) &
Localization
Internationalization:
   Turns a one-language site into a international one
   Has translations for built-in messages (e.g for validation
   errors) for most languages
   Translations can easily be added in sep. "locale" files

Localization:

   localizes displayed data to the actual language/country
   date, time, month names, day names, currency, etc.
Generators
 Generates files and content for common tasks
 Available from the command line
 Because laziness is a virtue in a programmer :)
 Models, controllers, resources, migrations,
 Plugins can easily add their generators
Caching

Granular:

      page caching
          Static pages that do not need Rails to enter the
          picture (e.g about us, terms & conditions)
      action caching
          Pages that rarely change but that need certain filters
          to run (e.g pages that can only viewed once logged
          in)
      fragment caching
          only cache certain parts of a page (e.g sidebar)
Consistent core, dead simple extension

  User authentication, pagination, attaching files, etc. to
  models are not part of Rails
  Possible extensions:
     plugins are bundled with the application
     gems can be bundled with the application (which is good
     practice to do) or installed system-wide
Handling dependencies

vendor directory for dependencies:

   vendor/gems
   vendor/plugins
   vendor/rails
and automated tasks to manage them

Examples:

$ rake rails:freeze:edge
$ rake gems:unpack:dependencies
Who uses Rails?

  twitter.com
  yellowpages.com
  github.com - social coding
  lighthouseapp.com - issue tracker app
  shopify.com - webshop generation and hosting
  ...

  from small-scaled applications to high-traffic ones
  for really simple stuff it could be an overkill (in these cases
  use Sinatra, another ruby framework :) [SIN])
Time for a demo app!

           http://github.
com/balinterdi/par_avion/tree/maste
                  r
Questions?
            Bálint Érdi
 blog: http://bucionrails.com
email: balint@bucionrails.com
twitter: http://twitter.com/baaz
References

[HAML] http://haml.hamptoncatlin.com/
[RUBY] http://ruby-lang.org/
[ROR] http://rubyonrails.org/
[REST] http://tomayko.com/writings/rest-to-my-wife
[SIN] http://www.sinatrarb.com/

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java script
Java scriptJava script
Java script
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Jsp
JspJsp
Jsp
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Ruby on Rails Security
Ruby on Rails SecurityRuby on Rails Security
Ruby on Rails Security
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScript
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
 
Jsp
JspJsp
Jsp
 
Struts N E W
Struts N E WStruts N E W
Struts N E W
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Wt unit 6 ppts web services
Wt unit 6 ppts web servicesWt unit 6 ppts web services
Wt unit 6 ppts web services
 

Ähnlich wie Ruby On Rails

Introducing Ruby/MVC/RoR
Introducing Ruby/MVC/RoRIntroducing Ruby/MVC/RoR
Introducing Ruby/MVC/RoRSumanth krishna
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in ScalaKnoldus Inc.
 
Getting Started with Rails
Getting Started with RailsGetting Started with Rails
Getting Started with RailsBasayel Said
 
MVC Demystified: Essence of Ruby on Rails
MVC Demystified: Essence of Ruby on RailsMVC Demystified: Essence of Ruby on Rails
MVC Demystified: Essence of Ruby on Railscodeinmotion
 
Example Of Import Java
Example Of Import JavaExample Of Import Java
Example Of Import JavaMelody Rios
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web DevelopmentSonia Simi
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The EnterpriseDaniel Egan
 

Ähnlich wie Ruby On Rails (20)

Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 
Introducing Ruby/MVC/RoR
Introducing Ruby/MVC/RoRIntroducing Ruby/MVC/RoR
Introducing Ruby/MVC/RoR
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
 
Rails interview questions
Rails interview questionsRails interview questions
Rails interview questions
 
Getting Started with Rails
Getting Started with RailsGetting Started with Rails
Getting Started with Rails
 
MVC Demystified: Essence of Ruby on Rails
MVC Demystified: Essence of Ruby on RailsMVC Demystified: Essence of Ruby on Rails
MVC Demystified: Essence of Ruby on Rails
 
Example Of Import Java
Example Of Import JavaExample Of Import Java
Example Of Import Java
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
Intro lift
Intro liftIntro lift
Intro lift
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 

Kürzlich hochgeladen

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Kürzlich hochgeladen (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Ruby On Rails

  • 1. Ruby on Rails Rapid, solid, flexible application development
  • 2. I am ... Bálint Érdi (Budapest, Hungary) blog: http://bucionrails.com email: balint@bucionrails.com My evolution :) java web developer python web developer (freelance) ruby web developer and consultant (with occasional non-web works)
  • 4. Legend Facts: (Wisdom :) ) Ruby is object oriented Code: 5.times { print "Hello Fluidtime!" }
  • 5. Outline 1. Ruby basic concepts 2. Rails History & philosophy Basic modules Introducing each module Other features (a selection of them) Who uses Rails? 3. Demo app Questions are welcome anytime
  • 6. Ruby: History created by Yukihiro Matsumoto ~1995 it is a blend of his favorite languages (Smalltalk, Lisp, Perl, ADA) open source [RUBY] now at 1.9
  • 7. Ruby is object oriented But it really is Everything is an object Every object has a class Classes are objects, too 5.times { print "Hello, Fluidtime!" } 3 + 2 # => 5 3.+(2) # => 5 3.send(:+, 2) # => 5
  • 8. Ruby is duck-typed (dynamically typed) behavior is more important than type ("if it walks like a duck and talks like a duck, it is a duck") no type declarations are needed a = 3 a + 2 # => 5 a + " jeff" # => TypeError a = "hello" a + 2 # => TypeError a + " jeff" # =>"hello jeff"
  • 9. Ruby is functional programming like Functional programming methods are first-class citizens of the language lets you concentrate on what you want instead of how you want to do it (because "for" loops are ugly) Helps keep the code cleaner, more elegant and less error- prone. e.g Lisp [119, 43, 982, 266].select { |n| n % 7 == 0 } ["voodoo", "kayak", "light"].find { |w| w =~ /^(.).*1$/ } (1..4).map { |n| n**2 }
  • 10. Ruby has strong metaprogramming features define methods at runtime, on the fly (define_method) evaluate code in the context of an instance or a class (instance_eval, class_eval) get in control when the object does not have a certain method (method_missing) ... or when a name is not found(const_missing)
  • 11. Ruby has open classes, all of them Any class can be reopened, "core" classes, too you can define new methods, override new ones, etc. class definitions can thus span files class Numeric def plus(n) self.+(n) end end 3.plus(2) # => 5
  • 12. Rails résumé Rails is a powerful yet simple web framework written in Ruby History Philosophy Introducing the main building blocks Other included features Extending Rails functionality Who uses Rails?
  • 13. History created by David Heinemeier Hansson, ~2003 [ROR] extracted framework from a project called Basecamp open source, community driven, ~1400 contributors during its history now at 2.3, going for 3.0
  • 14. Rails basic ideology Very opinionated: encourages "best practices" "Convention over configuration" to avoid bloatware and keep things simple in most cases Possibility to change default behavior in special cases Sugar and vinegar: easy to do what is encouraged, possible (but probably not simple) to do "unrecommended" things. Test Driven Development (TDD) is extremely important (writing tests first, so an automated test suit that can be easily -and quickly- run is needed) The framework has to stay small and lean but provide a mechanism for easy expansion.
  • 15. Rails: Consequences of the ideology Why is all this important? enables you to be more productive by following the "guidelines" enables you to concentrate on the business logic without worrying about the technical details you will feel happy because it is so elegant and and feeling good! you will feel good because it is so efficient
  • 16. Rails main building blocks Model - View - Controller (MVC) framework Model: ActiveRecord Controller: ActionController View: ActionView Clear separation of concerns Additional modules ActionSupport for supporting libs ActiveResource for REST compliance ActionMailer for sending emails
  • 17. The model: ActiveRecord Database engine agnosticism Database migrations Associations between models Validations Callbacks
  • 18. ActiveRecord: database agnosticism it is an ORM (Object Relational Mapping) you should not have to worry about what lies underneath you should not have to write SQL Adapters exist for sqlite, mysql, postgresql Post.find(:all, :conditions => [ "created_at > ?", 2.days.ago ])
  • 19. ActiveRecord: Database migrations to make sure ... not just the code, but the database schema is tracked, too ... all developers work with the same schema ... you don't have to write raw SQL .. you are able to jump to any version, just like with a SCM ... deployment stays simple: you just run the migrations Examples of what a migration could do: creating a posts table adding a user_id to the posts table changing column type from boolean to string
  • 20. ActiveRecord: Associations 1. class Blog < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :blog has_many :comments end * using the control that Ruby gives for finding names (const_missing)
  • 21. ActiveRecord: Associations 2. reaching associated object(s): blog.posts.find(:first, :conditions => ["updated_at > 1.week.ago]) creating associated object(s): post.comments.create(:user => me, :body => "That's a great post, congratulations!") * leveraging Ruby metaprogramming capabilities
  • 22. ActiveRecord: Validations validation is business logic so it belongs to the model use the provided validators which cover most of the cases validates_presence_of :login validates_format_of :zip_code, :with => /^d{5}$/ roll your own for the special cases
  • 23. ActiveRecord: Callbacks Entry points (hooks) that implement business logic at different during the lifecycle of the object lifecycle events: create, save, update, validate, destroy provided methods: before_save, after_create, ... good for setting default values, set calculated attributes, send out emails, etc.
  • 24. ActionController: Résumé Basic buildup Filters Rendering from controller actions
  • 25. ActionController: Basic buildup The controller should be thin and should only contain: ... actions (methods) that compose the API ... filter calls ... the actual filter implementations Actions new create edit destroy ...
  • 26. ActionController: Filters Filters are ... methods that run before, after or around actions ... prevalently used for checking authorization to resources ... used to set variables for templates in a DRY way
  • 27. ActionController: Rendering The controller sets the variables that the view template will use and does the actual rendering By default, the view with the name of the action is rendered But it can also render any action by name a text (mainly for debugging purposes) other representations of resources (e.g js, json, xml, etc.) nothing (just returning something to the caller)
  • 28. ActionView: Résumé Basics Partials & content blocks
  • 29. ActionView: Basics The view: ... is a representation ... should contain the least possible amount of business logic ... is rendered by the ERB template language which can be changed with plugins (check out haml! [HAML]) Lots of view helpers are provided for generating html some helpers (e.g form_for) are context-sensitive so views can be reused without cluttering them with conditions
  • 30. ActionView: Partials & Content blocks Partials chunks of reusable code you can render from other views Content blocks separate definition of the building blocks of a page and actually generating them. make it possible to break the link between the order of html in the template and their order on the generated page are ideal for setting a subsection in the html title of a page injecting per-page javascripts
  • 31. A selection of Rails' features Sending emails with ActionMailer Context-dependent configurations Routing RESTful applications Internationalization & Localization Generators Caching Extending core functionality Handling dependencies
  • 32. Sending emails with ActionMailer mailers are models one email template (a view template) for each email type one method in the mailer model for each email type that sets the variables for rendering the email sending out an email is calling a method on the mailer there is convention for finding the template name from the method's name
  • 33. Context dependent configurations (environments) Development: used for development with throwaway data Test: used for automated testing, records are created and destroyed by each test run. Production: used by the live application. Handle with care. Common configuration file + each env. has its own config which overwrites the values set in the common one. Additional environments can easily be set up Example: emails should be sent out in production, examined but not sent out when testing and it is up to you in development
  • 34. Router generates user-friendly names you can use in your code (e. g dashboard_path and dashboard_url) makes your routes DRY. You don't have to put hardwired routes into your application makes dangling links nearly impossible routes incoming requests to the appropriate action it is a SEO tool map.dashboard '/', :controller => 'dashboard', :action => 'show' map.search '/search/:term', :controller => "search", :action => "index", :term => /w*/
  • 35. RESTful applications "HTTP is actually a general purpose protocol for applying verbs to nouns" [REST] encouraged as best practice declaring a resource generates the RESTful routes map.resources :users verb path generated path name GET /users/new new_user_path POST /users users_path GET /users/:id/edit edit_user_path(id) PUT /users/:id user_path(id) GET /users users_path DELETE /users/:id user_path(id)
  • 36. Internationalization (I18n) & Localization Internationalization: Turns a one-language site into a international one Has translations for built-in messages (e.g for validation errors) for most languages Translations can easily be added in sep. "locale" files Localization: localizes displayed data to the actual language/country date, time, month names, day names, currency, etc.
  • 37. Generators Generates files and content for common tasks Available from the command line Because laziness is a virtue in a programmer :) Models, controllers, resources, migrations, Plugins can easily add their generators
  • 38. Caching Granular: page caching Static pages that do not need Rails to enter the picture (e.g about us, terms & conditions) action caching Pages that rarely change but that need certain filters to run (e.g pages that can only viewed once logged in) fragment caching only cache certain parts of a page (e.g sidebar)
  • 39. Consistent core, dead simple extension User authentication, pagination, attaching files, etc. to models are not part of Rails Possible extensions: plugins are bundled with the application gems can be bundled with the application (which is good practice to do) or installed system-wide
  • 40. Handling dependencies vendor directory for dependencies: vendor/gems vendor/plugins vendor/rails and automated tasks to manage them Examples: $ rake rails:freeze:edge $ rake gems:unpack:dependencies
  • 41. Who uses Rails? twitter.com yellowpages.com github.com - social coding lighthouseapp.com - issue tracker app shopify.com - webshop generation and hosting ... from small-scaled applications to high-traffic ones for really simple stuff it could be an overkill (in these cases use Sinatra, another ruby framework :) [SIN])
  • 42. Time for a demo app! http://github. com/balinterdi/par_avion/tree/maste r
  • 43. Questions? Bálint Érdi blog: http://bucionrails.com email: balint@bucionrails.com twitter: http://twitter.com/baaz
  • 44. References [HAML] http://haml.hamptoncatlin.com/ [RUBY] http://ruby-lang.org/ [ROR] http://rubyonrails.org/ [REST] http://tomayko.com/writings/rest-to-my-wife [SIN] http://www.sinatrarb.com/