SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Downloaden Sie, um offline zu lesen
Ruby on Rails
          Erweitern
             Ramon Wartala - Jan Krutisch
9. Juli 2007 - Lehmanns Fachbuchhandlung, Hamburg
Wer wir sind

•   Jan Krutisch und Ramon Wartala

•   Ehemalige Kollegen

•   Gründer der deutschen Rails-UG

•   Autoren von quot;Webanwendungen
    mit Ruby on Railsquot;, Addison-Wesley
Wer wir sind

•   Jan Krutisch und Ramon Wartala

•   Ehemalige Kollegen

•   Gründer der deutschen Rails-UG

•   Autoren von quot;Webanwendungen
    mit Ruby on Railsquot;, Addison-Wesley
Ramon Wartala

• Dipl.-Inform.
• IT-Leiter bei Orangemedia GmbH
 • Einer der führenden Online-Vermarkter
• Autor zahlreicher Artikel und
  Tagungsbeiträge rund um
  Softwareentwicklung
Jan Krutisch

• Dipl.-Ing. (FH) Umwelttechnik
• Entwicklungsleiter für Rails-Projekte bei
  mindmatters (www.mindmatters.de)
• artdoxa.com / familylounge.de
• Jahrelange Erfahrung als Programmierer im
  Webbereich insbesondere in Java und PHP
Ruby on Rails Historie

• Juli 2004, David Heinemeier Hansson
  veröffentlicht Ruby on Rails als quelloffenes
  Framework für dir Erstellung von Web
  Applikationen mit Ruby
• Version 1.0 im Dezember 2005
• Version 1.2 im Januar 2007
Ruby on Rails
 Grundlagen
Ruby
• Moderne Skriptsprache
• Anleihen aus Perl, Python, Smalltalk, ...
• Alles ist ein Objekt: 5.days.from_now
• Starke Introspektion (aka Reflection)
• Schwache Typisierung
• Closures, Blocks, Iteratoren, etc.
def tausch(x,y)

 [y, x]

end

x,y = tausch 1,2

=> x=2, y=1
def tausch(x,y)
                                      t
                             kgabewer
                    iter Rüc
              Impliz
 [y, x]

end

x,y = tausch 1,2

=> x=2, y=1
def tausch(x,y)
                                          t
                                 kgabewer
                        iter Rüc
                  Impliz
 [y, x]

end                   weisung
               ache Zu
          Mehrf

x,y = tausch 1,2

=> x=2, y=1
def tausch(x,y)
                                          t
                                 kgabewer
                        iter Rüc
                  Impliz
 [y, x]

end                   weisung
               ache Zu
          Mehrf                                        on
                                             aucht sch
                                      Wer br        n
                                          Klammer
x,y = tausch 1,2

=> x=2, y=1
Rails
• ModelViewController-Architektur
• Convention over         Configuration


• Don't Repeat Yourself
•   Unterstützt die Testgetriebene Entwicklung

•   Unterstützt agile Methoden

•   Unterstützt Web2.0-Techniken (Ajax etc.)
Das Versprechen von Rails

• Sehr viel schnellere Entwicklung durch:
  • Sehr viel weniger quot;Glue codequot;
    • Konzentrierung auf Geschäftslogik
      • Mehr Spaß am Entwickeln
        • Höhere Produktivität
Nachteile von Rails

• Ruby ist nicht sehr performant
• Rails ist nicht sehr performant
• Kein „Allgemeinwissen“
• (Noch) keine quot;sichere Wahlquot; für Entscheider
Die Komponenten
•   ActiveRecord: Objekt-relationaler Mapper

•   ActionPack

    •   ActionView

    •   ActionController

•   ActiveSupport

•   ActionMailer
Code!
Model


class Book < ActiveRecord::Base
end
Model
Book.find :all

Book.find :first, :conditions =>

    [quot;isbn = ?quot;, @isbn]

@book = Book.find_by_isbn(@isbn)

@book.title = quot;Ruby Cookbookquot;

@book.save

@book.destroy
Model mit Beziehungen
class Book < ActiveRecord::Base

  belongs_to :library

end

class Library < ActiveRecord::Base

  has_many :books

end
Model mit Beziehungen

@library = @book.library

@books = @library.books

@book = @library.books.find_by_isbn @isbn

@library << @book

@book.library = @library
Model mit Validierungen

class Book < ActiveRecord::Base

  validates_presence_of :title

  validates_uniqueness_of :isbn

end
Model mit Validierungen

@book = Book.new

@book.save # => false

@errors = @book.errors
Controller

class BooksController < ApplicationController

  def index

      @books = Book.find(:all)

  end

end
View (HTML)

<ul>

  <% for book in @books %>

    <li><%=h book.title %></li>

  <% end %>

</ul>
View (XML)
xml.instruct! :xml, :version=>quot;1.0quot;

xml.bookfeed do

  for book in @book

      xml.book do

        xml.title(book.title)

      end

  end

end
Weitere Features
Syntaktischer Zucker

• belongs_to :library
• has_many :books
• books_path
• book_path(@book)
Tests

• Basiert auf Test::Unit
• Unit-Tests (Auf Modellebene)
• Funktionale Tests (Auf Controllerebene)
• Integrationstests (Auf Applikationsebene)
• Testausführung automatisiert über Rake
Ajax etc.

• prototype.js
• script.aculo.us
• Helper
• rjs-Templates
Helper
• Textformatierung (Textile / Markdown)
  excerpt(text, 'foo')

    => '...und foo ging...'

• Datums-Formatierungen
• Datums-Formular-Helper
• ...
RESTful

• URL = Resource
• GET / POST / PUT / DELETE
• Klare Architektur
• Viele kleine Controller mit wenig Actions
Rails erweitern
Rails erweitern
Durch:
• rake-Tasks
• Mixins
• Generatoren
• RubyGems
• Plugins
Plugins

• Erweitern der Rails-Basisklassen
 • Metaprogrammierung!
 • Überschreiben, Ergänzen (Mixins)
• Eigene Generatoren
• Eigene Rake-Tasks
Plugin-Skript
• ruby scripts/plugin
 • discover
 • source / unsource / sources
 • list
 • install / update
 • remove
Beispiel: acts_as_ferret

class Question < ActiveRecord::Base
  acts_as_ferret :fields =>
                   [:title, :text]
end

Question.find_by_contents('foo')
Model-Plugins
• acts_as_state_machine
• acts_as_taggable
• acts_as_versioned
• acts_as_translatable
• attachment_fu
• acts_as_rateable
Authentifizierung

• acts_as_authenticated
• authorization
• restful_authentication
• openid_authentication
Tests

• selenium_on_rails
• rspec (Behaviour Driven Development)
• rcov (Test coverage)
• ...
Sonstiges

• Streamlined
• HOBO
• und >400 mehr
• http://www.agilewebdevelopment.com/
Eigene Erweiterungen
Eigene Plugins


• Funktionalität bestimmen
• Plugin-Generator anwerfen
• Funktionalität implementieren
Eigene Plugins


• acts_as_amazon_book
• ruby script/generate plugin
  acts_as_amazon_book
Eigene Plugins


• acts_as_amazon_book
• ruby script/generate plugin
  acts_as_amazon_book
Plugin Anatomie
vendorpluginsacts_as_amazon_book
  init.rb
  install.rb
  uninstall.rb
vendorpluginsacts_as_amazonlib
  acts_as_amazon_book.rb
vendorpluginacts_as_amazontasks
acts_as_amazon_book.rb
Plugin-Nutzung
Eigene Generatoren

   • Rails Generatoren in
     librails_generatorgenerators
     components
   • Eigene Generatorn in lib
     generators
Eigene Generatoren


• Generatoren sind in Rails an vielen Stellen
• Eigener Scaffolder Generator wii_scaffolder
• Rails scaffolder als Ausgangspunkt
Generator-Anatomie

• Jeder Generator erbt von
  Rails::Generator::Base bzw.
  Rails::Generator::NamedBase
• Regeln zur Erstellung / Anpassung von
  Dateien / Templates
• Implementierung der Methode manifest
Generator-Anatomie

   
 record do |m|

   
 
 m.file(…)

   
 
 m.directory(…)

   

…

   
 end
Generator-Anatomie
wii_scaffold anwenden

• ruby script/generate wii_scaffolder Book
  books list
• „Erzeuge mir mit Hilfe des wii_scaffold-
  Generators ein Modell Book mit einem
  Controller books und einer Action list“
wii_scaffold anwenden
Erzeugte Dateien:
 app/model/book.rb
 public/stylesheets/wii_scaffold.css
 app/views/book/list.rhtml
 app/controllers/books_controller.rb
 app/views/layouts/books.rhtml
wii_scaffold anwenden
Erweiterung verteilen

• Code und zusätzliche Dateien verteilen
• RubyGems als standardisiertes
  Distributionswerkzeug
• Plugins auch direkt über SVN installierbar
    ruby script/plugin install 

    http://svn.meinsubversion.com/mein/plugin
RubyGem

• Ausgangspunkt jeder Gem ist die
  Datei .gemspec
• Ruby-basierte Spezifikation der zu
  „verpackenden“ Erweiterung
RubyGem

• gem build acts_as_amazon_book.gemspec
• => acts_as_amazon_book-0.0.1.gem
• gem install acts_as_amazon_book-0.0.1.gem
• gem server
RubyGem

• gem build acts_as_amazon_book.gemspec
• => acts_as_amazon_book-0.0.1.gem
• gem install acts_as_amazon_book-0.0.1.gem
• gem server
Danke
              Fragen?

• http://www.rubyonrails.org
• http://www.rubyonrails-ug.de
• http://www.railsjobs.de/

Weitere ähnliche Inhalte

Ähnlich wie Lehmanns Rails Erweitern

An Introduction to Ruby On Rails
An Introduction to Ruby On RailsAn Introduction to Ruby On Rails
An Introduction to Ruby On RailsJonathan Weiss
 
AdvancedTdd
AdvancedTddAdvancedTdd
AdvancedTddjlink
 
Java und Go im Vergleich
Java und Go im VergleichJava und Go im Vergleich
Java und Go im VergleichQAware GmbH
 
HTML5 und node.js Grundlagen
HTML5 und node.js GrundlagenHTML5 und node.js Grundlagen
HTML5 und node.js GrundlagenMayflower GmbH
 
Ruby on Rails in a metro session
Ruby on Rails in a metro sessionRuby on Rails in a metro session
Ruby on Rails in a metro sessionVirttoo org
 
Einführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungEinführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungDigicomp Academy AG
 
An Introduction to Ruby
An Introduction to RubyAn Introduction to Ruby
An Introduction to RubyJonathan Weiss
 
JsUnconf 2014
JsUnconf 2014JsUnconf 2014
JsUnconf 2014emrox
 
Von Java Zu Groovy
Von Java Zu GroovyVon Java Zu Groovy
Von Java Zu Groovyjlink
 
Rails i18n - Railskonferenz 2007
Rails i18n - Railskonferenz 2007Rails i18n - Railskonferenz 2007
Rails i18n - Railskonferenz 2007jan_mindmatters
 
Javascript auf Client und Server mit node.js - webtech 2010
Javascript auf Client und Server mit node.js - webtech 2010Javascript auf Client und Server mit node.js - webtech 2010
Javascript auf Client und Server mit node.js - webtech 2010Dirk Ginader
 

Ähnlich wie Lehmanns Rails Erweitern (20)

Ruby on Rails SS09 04
Ruby on Rails SS09 04Ruby on Rails SS09 04
Ruby on Rails SS09 04
 
Testing tools
Testing toolsTesting tools
Testing tools
 
An Introduction to Ruby On Rails
An Introduction to Ruby On RailsAn Introduction to Ruby On Rails
An Introduction to Ruby On Rails
 
AdvancedTdd
AdvancedTddAdvancedTdd
AdvancedTdd
 
Ruby on Rails SS09 08
Ruby on Rails SS09 08Ruby on Rails SS09 08
Ruby on Rails SS09 08
 
Java und Go im Vergleich
Java und Go im VergleichJava und Go im Vergleich
Java und Go im Vergleich
 
Offline Arbeiten
Offline ArbeitenOffline Arbeiten
Offline Arbeiten
 
HTML5 und node.js Grundlagen
HTML5 und node.js GrundlagenHTML5 und node.js Grundlagen
HTML5 und node.js Grundlagen
 
Ruby on Rails SS09 06
Ruby on Rails SS09 06Ruby on Rails SS09 06
Ruby on Rails SS09 06
 
Ruby on Rails in a metro session
Ruby on Rails in a metro sessionRuby on Rails in a metro session
Ruby on Rails in a metro session
 
Einführung in die funktionale Programmierung
Einführung in die funktionale ProgrammierungEinführung in die funktionale Programmierung
Einführung in die funktionale Programmierung
 
An Introduction to Ruby
An Introduction to RubyAn Introduction to Ruby
An Introduction to Ruby
 
JsUnconf 2014
JsUnconf 2014JsUnconf 2014
JsUnconf 2014
 
Von Java Zu Groovy
Von Java Zu GroovyVon Java Zu Groovy
Von Java Zu Groovy
 
Rails i18n - Railskonferenz 2007
Rails i18n - Railskonferenz 2007Rails i18n - Railskonferenz 2007
Rails i18n - Railskonferenz 2007
 
Node.js
Node.jsNode.js
Node.js
 
Ruby on Rails SS09 03
Ruby on Rails SS09 03Ruby on Rails SS09 03
Ruby on Rails SS09 03
 
Javascript auf Client und Server mit node.js - webtech 2010
Javascript auf Client und Server mit node.js - webtech 2010Javascript auf Client und Server mit node.js - webtech 2010
Javascript auf Client und Server mit node.js - webtech 2010
 
Rack-Middleware
Rack-MiddlewareRack-Middleware
Rack-Middleware
 
NoSQL with MySQL
NoSQL with MySQLNoSQL with MySQL
NoSQL with MySQL
 

Mehr von jan_mindmatters

Ruby for Artists and Tinkerers. A non-presentation.
Ruby for Artists and Tinkerers. A non-presentation.Ruby for Artists and Tinkerers. A non-presentation.
Ruby for Artists and Tinkerers. A non-presentation.jan_mindmatters
 
realtime audio on ze web @ hhjs
realtime audio on ze web @ hhjsrealtime audio on ze web @ hhjs
realtime audio on ze web @ hhjsjan_mindmatters
 
Railsrumble railscamphh 2010
Railsrumble railscamphh 2010Railsrumble railscamphh 2010
Railsrumble railscamphh 2010jan_mindmatters
 
MongoDB on Rails (and Ruby)
MongoDB on Rails (and Ruby)MongoDB on Rails (and Ruby)
MongoDB on Rails (and Ruby)jan_mindmatters
 
Mongodb on Ruby And Rails (froscon 2010)
Mongodb on Ruby And Rails (froscon 2010)Mongodb on Ruby And Rails (froscon 2010)
Mongodb on Ruby And Rails (froscon 2010)jan_mindmatters
 
10 fun projects to improve your coding skills
10 fun projects to improve your coding skills10 fun projects to improve your coding skills
10 fun projects to improve your coding skillsjan_mindmatters
 
MongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realMongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realjan_mindmatters
 
Open Source Hardware - Of makers and tinkerers
Open Source Hardware - Of makers and tinkerersOpen Source Hardware - Of makers and tinkerers
Open Source Hardware - Of makers and tinkerersjan_mindmatters
 
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & lessLiebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & lessjan_mindmatters
 
Facebook mit Rails und Facebooker
Facebook mit Rails und FacebookerFacebook mit Rails und Facebooker
Facebook mit Rails und Facebookerjan_mindmatters
 
Show the frontend some love - HAML, SASS and COMPASS
Show the frontend some love - HAML, SASS and COMPASSShow the frontend some love - HAML, SASS and COMPASS
Show the frontend some love - HAML, SASS and COMPASSjan_mindmatters
 

Mehr von jan_mindmatters (14)

Ruby for Artists and Tinkerers. A non-presentation.
Ruby for Artists and Tinkerers. A non-presentation.Ruby for Artists and Tinkerers. A non-presentation.
Ruby for Artists and Tinkerers. A non-presentation.
 
realtime audio on ze web @ hhjs
realtime audio on ze web @ hhjsrealtime audio on ze web @ hhjs
realtime audio on ze web @ hhjs
 
Railsrumble railscamphh 2010
Railsrumble railscamphh 2010Railsrumble railscamphh 2010
Railsrumble railscamphh 2010
 
Mongodb railscamphh
Mongodb railscamphhMongodb railscamphh
Mongodb railscamphh
 
MongoDB on Rails (and Ruby)
MongoDB on Rails (and Ruby)MongoDB on Rails (and Ruby)
MongoDB on Rails (and Ruby)
 
Mongodb on Ruby And Rails (froscon 2010)
Mongodb on Ruby And Rails (froscon 2010)Mongodb on Ruby And Rails (froscon 2010)
Mongodb on Ruby And Rails (froscon 2010)
 
10 fun projects to improve your coding skills
10 fun projects to improve your coding skills10 fun projects to improve your coding skills
10 fun projects to improve your coding skills
 
MongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realMongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 real
 
Open Source Hardware - Of makers and tinkerers
Open Source Hardware - Of makers and tinkerersOpen Source Hardware - Of makers and tinkerers
Open Source Hardware - Of makers and tinkerers
 
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & lessLiebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
 
Facebook mit Rails und Facebooker
Facebook mit Rails und FacebookerFacebook mit Rails und Facebooker
Facebook mit Rails und Facebooker
 
Show the frontend some love - HAML, SASS and COMPASS
Show the frontend some love - HAML, SASS and COMPASSShow the frontend some love - HAML, SASS and COMPASS
Show the frontend some love - HAML, SASS and COMPASS
 
HAML / SASS and COMPASS
HAML / SASS and COMPASSHAML / SASS and COMPASS
HAML / SASS and COMPASS
 
Merb. Rails in anders.
Merb. Rails in anders.Merb. Rails in anders.
Merb. Rails in anders.
 

Lehmanns Rails Erweitern