Top Banner
Infopoint - Ruby on Rails - Jörg Wüthrich 08.08.2007 Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo
23

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Apr 06, 2015

Download

Documents

Lore Kemerer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 1

Ruby on Rails

Geschichte Ruby Rails Live – Demo

Page 2: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 2

Geschichte

Ruby "Vater": Yukihiro “matz” Matsumoto 1993: Beginn der Arbeiten an Rubys 1995: 1. public Release von Ruby 1996: erlangt "Akzeptanz der Massen" (aktive

Usergruppen, gefüllte Konferenzen) aktuell: 1.8.6

Rails Gründer: David Heinemeier Hansson 2003: Beginn der Entwicklung (aus

Basecamp abgeleitet) 2004: 1. Release aktuell: 1.2.3

Page 3: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 3

TIOBE Index

ww

w.ti

obe.

com

Page 4: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 4

Ruby

"A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write."(www.ruby-lang.org)

Page 5: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 5

Ruby

objektorientiert (alles ist ein Objekt) interpretiert (20KB executable in C geschrieben) Open Source – "Ruby License" oder GPL "Object" als Wurzel Single Inheritance (+ "mixin"s) Garbage Collection vollständig Kommandozeilen-orientiert wird mit Standard-Library geliefert

Page 6: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 6

Ruby - Code-Beispiel

class Song attr_accessor :name # accessor methods @@plays = 0 # static field def initialize(name)# constructor @name = name # instance field end def play # instance method @@plays += 1 # "return" omitted endend

song = Song.new('my Song')puts song.name # => my Songputs song.play # => 1

Page 7: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 7

Ruby – etwas genauer (1)

streng und dynamisch typisiert es gibt "public", "protected", "private"

protected: Aufruf aus der definierten Klasse und deren Subklassen

private: nur innerhalb eines Objekts (mit implizitem "this" als Empfänger)

Operatoren können überladen werden Definitionen (Klassen, Methoden...) werden mit

"end" abgeschlossen (kein { }) "()" bei Methoden-Aufrufen fehlen in der Regel Variablen-Scope über Naming-Convention

Page 8: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 8

Ruby – etwas genauer (2) alle Klassen sind "offen"

jede Klasse (auch Object) kann jederzeit erweitert werden Closures

Code-Blöcke mit Zugriff auf alle umliegenden Variablen (ähnlich wie anonyme Klassen in Java)

Iteration mit Blöcken

Mixin – einbinden von Modulen

ähnliche wie Interface in Java Variable Konstanten

gibt zur Laufzeit eine Warnung...

%w[dies sind Elemente einer Liste].each { |element| puts element }

include Enumerable

Page 9: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 9

Rails

"Web development that doesn't hurt"

Model View Controller ActiveRecord ActionView ActionController

Convention over Configuration Don't repeat yourself Unterstützung für DB-Migrationen / Unittesting ...

Page 10: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 10

Rails – MVCHTTP RequestHTTP Request HTTP ResponseHTTP Response

Page 11: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 11

Rails – don't repeat yourself

ActiveRecord Methoden aus DB-Metadaten generiert

ActionView Layout pro Model Template pro Methode Referenzen zwischen Templates

Scaffolding generiert Gerüst einer Applikation macht viele "Fingerübungen" überflüssig

neuer Controller neues Model-Element neue View neuer Testfall

Page 12: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 12

Live - Demo

Page 13: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 13

Rails - ActionController

Zentrale Drehscheibe für Webrequests bestehen aus Actions, die als Request oder als

Redirect aufgerufen werden können AddressController.list AddressController.create...

rendern ein Template aus app/views

def create @address = Address.new(params[:address]) if @address.save flash[:notice] = 'Address was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end

Page 14: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 14

Rails - ActionView

Template-Renderer für Output 3 Varianten im Standard enthalten

.rhtml – Mischung aus ERb ("eRuby") und Html .rxml – programmatische Alternative zu .rhtml .rjs – verwendet den JavaScriptGenerator

enthält diverse "Helpers" (vergleichbar mit Tag Libraries)

<% for column in Address.content_columns %><p> <b><%= column.human_name %>:</b> <%= @address.send(column.name) %></p><% end %>

<%= link_to 'Edit', :action => 'edit', :id => @address %> |<%= link_to 'Back', :action => 'list' %>

Page 15: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 15

Rails – ActiveRecord (1)

Repräsentieren eine DB-Tabelle führen selbst keine Attribute, sondern leiten

diese aus der Tabellen-Definition ab Änderungen werden nicht am ActivRecord-

Objekt gemacht, sondern immer direkt auf der Tabelle

Standard-Verhalten kann übersteuert werden Unterstützt optimistisches / pessimistisches

Locking def edit @address = Address.find(params[:id], :lock => true) end

Page 16: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 16

Rails – ActiveRecord (2)

DB-Migrationen Verwaltung des Lifecycles von Tabellen

Neue Tabelle Spalten hinzufügen / entfernen Index setzen ...

Ruby Notation in Standard-Fällen

SQL-Notation, falls benötigt

class InitAdresslist < ActiveRecord::Migration def self.up create_table :addresses do |table| table.column :lastname, :string, :limit => 40 table.column :firstname, :string, :limit => 40 table.column :email, :string, :limit => 100 end end ...end

Page 17: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 17

Rails – ActiveRecord (3)

Assoziationen

ActiveRecord

Zugriff

class Dvd < ActiveRecord::Base belongs_to :categoryendclass Category < ActiveRecord::Base has_many :dvdend

dvdsWithCategories = Dvd.find(:all, :include => :category) puts dvdsWithCategories.title # => "Bourne Identity" puts dvdsWithCategories.category.name # => "Action"

Page 18: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 18

Rails – ActiveRecord (4)

Validationclass Address < ActiveRecord::Base protected def validate errors.add_on_empty %w(first_name last_name) errors.add("email", "has invalid format (use [email protected])") unless email =~ /[a-z]*@[a-z]*\.[a-z]*/ endend

Page 19: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 19

Warum Ruby on Rails?

Wenig Konfiguration notwendigKaum Wiederholungen, relativ sauberer Codevoll objektorientiertDB-Anbindung simpelWeb 2.0 "ready"Integriertes UnittestingIntegriertes StagingOpen Source mit aktiver Community

relativ jung; Erfahrungen mit wirklich grossen Projekten fehlenlangsamer als PHP oder ASP

Page 20: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 20

Referenzen www.ruby-lang.org - die Ruby Webseite http://rubyonrails.org/ – die Ruby on Rails Webseite http://wiki.rubyonrails.org/rails - Beantwortet viele Fragen rund

um Ruby on Rails http://homepage2.nifty.com/sakazuki/rde_en/index.html - RDE

(Ruby Development Environment) http://www.aptana.com/ - Eclipse basierte Ruby on Rails

Entwicklungs-Umgebung http://www.martinfowler.com/eaaCatalog/activeRecord.html -

das ActiveRecord Pattern http://www.meshplex.org/wiki/Ruby/Ruby_on_Rails_programming_tutorials -

guter Überblick über die Möglichkeiten von Ruby on Rails

Page 21: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 21

backup

Page 22: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 22

Ruby Tools

ruby.exe – Interpreter rake.bat – ruby make rdoc.bat – Ruby Doc

analog zu javadoc -> output als html, chm, ri, xml) ri.bat – Ruby Information

ähnlich "man" auf Unix irb.bat – interactive ruby gem.bat – package manager

Page 23: Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007Seite 1 Ruby on Rails Geschichte Ruby Rails Live – Demo.

Infopoint - Ruby on Rails - Jörg Wüthrich08.08.2007 Seite 23

Rails - Testing

Unit Tests Test für einzelnes Modul (ActiveRecord)

Functional Tests Test der Actions eines Controllers separat

Integration Tests Test des Zusammenspiels von mehreren Controllern

und Actions