30
Introduction to Ruby on Rails Agnieszka Figiel blog.agnessa.eu Kraków Ruby Users Group May 19th 2007

Introduction to Ruby on Rails

Embed Size (px)

DESCRIPTION

An introduction to the RoR framework prepared for the Kraków Ruby Users Group (http://www.ruby.org.pl)

Citation preview

Page 1: Introduction to Ruby on Rails

Introduction to Ruby on Rails

Agnieszka Figielblog.agnessa.eu

Kraków Ruby Users GroupMay 19th 2007

Page 2: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Ruby on Rails

"Rails is a full-stack framework for developingdatabase-backed web applications according to theModel-View-Control pattern."www.rubyonrails.org - Ruby on Rails official site

2005 David Heinemeier Hansson

opinionated software: "it’s a very pragmatic, very targetedframework with a strong sense of direction. You might notshare its vision, but it undeniably has one."DHH for Linux Journal (http://www.linuxjournal.com/article/8686)

Introduction to Ruby on Rails

Page 3: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Outline

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 4: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 5: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Model - View - Controller

separate data (model) from user interface (view)

I ModelI data access and business logicI independent of the view and controller

I ViewI data presentation and user interactionI read-only access to the model

I ControllerI handling eventsI operating on model and view

Introduction to Ruby on Rails

Page 6: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Database Persistance

I OR mappingActive Record design pattern

I migrationsincremental schema management

I multiple db adaptersMySQL, PostgreSQL, SQLite, SQL Server, IBM DB2,Informix, Oracle, Firebird/Interbase, LDAP, SybaseASA

Introduction to Ruby on Rails

Page 7: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Full Stack Framework

I MVC suiteI built-in webserverI default db adapterI integrated loggerI AJAX, web services, emailI test frameworkI plugins

Introduction to Ruby on Rails

Page 8: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Convention over Configuration

I fixed directory structureeverything has its place – source files, libs, plugins,database files, documentation etc

I file naming conventionse.g. camel case class name, underscore file name

I database naming conventionstable names, primary and foreign keys

I standard configuration filese.g. database connections, environment setting definitions(development, production, test)

Introduction to Ruby on Rails

Page 9: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

DRY - Don’t Repeat Yourself

I reusing codee.g. view elements

I reusing datae.g. no need to declare table field names – can be readfrom the database

I making each line of code work hardere.g. mini languages for specific domains, likeobject-relational mapping

I metaprogrammingdynamically created methods

Introduction to Ruby on Rails

Page 10: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 11: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Model - ActiveRecord

I conventions:I name based OR mapping: mice db table -> Mouse classI primary key: auto-incremented numeric field called "id"I foreign key: "[singular_of_foreign_table_name]_id", e.g.

"cat_id"I dynamic getters, setters, finders

dynamic means the existance of a field in db is enough tomanipulate it, e.g. dynamic getter like: mouse.name

I abstracted db operations - create, update, destroy, find, ...possible to use raw sql, but usually unnecessary, e.g.:

Mouse.find(:all, :include => {:mouse_holes},:conditions => "location = ’shed’",:order => "name", :limit => 10)

Introduction to Ruby on Rails

Page 12: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Model - ActiveRecord

I support for basic entity relations (1:1, 1:n, n:n) withdynamic accessorsbackyard_mouse_hole.mice #=> Array of Mouse objects

I parametrised queries against SQL injection, e.g. find allmice whose name is sth entered by the user:

Mouse.find(:all, :conditions => ["name = ?",mouse_name)

I validators: an object won’t be saved in db unless it passesall validation rules, e.g. uniqueness of a given field:validates_uniqueness_of :login

I object life cycle callbacks, e.g.before_create, before_save

I Single Table Inheritance, polymorphic associations

Introduction to Ruby on Rails

Page 13: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

View - ActionView

I multiple template typesI oldest and basic: erb (embedded ruby), similar to e.g. jspI remote javascript templatesI xml templates

I easy reuse of view elementsI file inclusion – layouts, templates, partialsI multiple standard "helpers" – common html element

generators (e.g. form elements, paginators)

I ridiculously easy AJAX integration – prototype,scriptaculous

Introduction to Ruby on Rails

Page 14: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Controller - ActionController

I all actions grouped logically in controller objects (actionsare public methods of the controller class)

I conventionsI name based url – action mapping

e.g. url: myhost.com/mice/show/1action: ’show’ action in MiceController, params: id=1

I name based action - template file mappinge.g. action ’show’ in MiceController - template fileapp/views/mice/show.rhtml

I can be organised in an inheritance hierarchyreduces code duplication and simplifies code

I action callbacks –before_filter, after_filter, around_filter

I built-in mechanism of url rewrites – routesI easy access to request data, session, cookies

Introduction to Ruby on Rails

Page 15: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 16: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Rapid Development

I built-in webserverI generators – save the fingersI scaffold – kick-off startI plugins, libraries, tons of contributed code

Introduction to Ruby on Rails

Page 17: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Debugging

I verbose log outputI breakpoint debuggerI script/console

Introduction to Ruby on Rails

Page 18: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Testing

I test database + fixturesI unit tests - tests for modelsI functional - tests for controllersI integration - tests for workflowI testing directly in browser - Selenium

Introduction to Ruby on Rails

Page 19: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Agile Project Heartbeat

I test coverage - rcovI continuous integrationI iterative db schema control - migrationsI automated deployment - capistrano

Introduction to Ruby on Rails

Page 20: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 21: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Platform Independence

I win, lin, macI Apache, Lighttpd, mongrelI gem package manager

Introduction to Ruby on Rails

Page 22: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

IDEs

I textmateI vimI RadRails, Eclipse + RDT, AptanaI plugins for Idea, NetBeansI SCiTeI jEdit

Introduction to Ruby on Rails

Page 23: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Scaling

I it scales – a must-see slideshow about scaling twitter (600req/s!): http://www.slideshare.net/Blaine/scaling-twitter

I view cachingI page cachingI action cachingI fragment caching

I sql cachingI memcached

I shared nothing architecture - multiplying applicationservers

Introduction to Ruby on Rails

Page 24: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Hosting

I http://wiki.rubyonrails.org/rails/pages/RailsWebHostsI US - a variety of shared hosting offersI Poland - poor shared hosting options hosted.plI Root VPS

Introduction to Ruby on Rails

Page 25: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Success Stories

several ror powered high traffic sitesI http://twitter.com/ - community siteI http://www.odeo.com/ - music sharingI http://www.43things.com/, http://www.43places.com/,

http://www.43people.com/I 37Signals: BaseCamp, BackPack, Ta-Da List, Writeboard,

CampFireI http://www.strongspace.com/ - storing and sharing files

Introduction to Ruby on Rails

Page 26: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Design Principles

MVC architecture

Agile Development

Usability and Success Stories

Community and Resources

Introduction to Ruby on Rails

Page 27: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Books

I in English:I ruby: "Programming Ruby 2nd Edition" (the Pickaxe) Dave

Thomas, with Chad Fowler and Andy HuntI "Agile Web Development with Rails" Dave Thomas and

David Heinemeier HanssonI "Rails Recipes" Chad FowlerI many, many more: http://www.rubyonrails.org/books

I in Polish:I "Programowanie w jezyku Ruby. Wydanie II" Dave Thomas,

Chad Fowler, Andy HuntI "Rails. Przepisy" Chad FowlerI "Ruby on Rails. Wprowadzenie" Bruce A. Tate, Curt HibbsI "Ruby on Rails. Cwiczenia" Michał Sobczak

Introduction to Ruby on Rails

Page 28: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Links

I main site: http://www.rubyonrails.orgI PL site: http://www.rubyonrails.pl/I general mailing list:

http://groups.google.com/group/rubyonrails-talkI wiki: http://wiki.rubyonrails.org/railsI api: api.rubyonrails.orgI 74 Quality Ruby on Rails Resources and Tutorials:

http://www.softwaredeveloper.com/features/74-ruby-on-rails-resources-tutorials-050207/

Introduction to Ruby on Rails

Page 29: Introduction to Ruby on Rails

Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources

Events

I Rails ConfI Rails DayI local communities & meetingsI Ruby on Rails workshops:

http://rubyonrailsworkshops.com/I academic presence

Introduction to Ruby on Rails

Page 30: Introduction to Ruby on Rails

Questions?

pics:http://www.midcoast.com/ holo/Seth%20in%20Thailand/pages/Rail%20tracks.html,

http://mfrost.typepad.com/cute_overload/2007/05/this_site_is_dr.html