Introduction to Ruby on Rails
Agnieszka Figiel
blog.agnessa.eu
Kraków Ruby Users Group
May 19th 2007
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Ruby on Rails
"Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern."
www.rubyonrails.org - Ruby on Rails official site 2005 David Heinemeier Hansson opinionated software: "it’s a very pragmatic, very targeted framework with a strong sense of direction. You might not share its vision, but it undeniably has one." DHH for Linux Journal (http://www.linuxjournal.com/article/8686)
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
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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Model - View - Controller
separate data (model) from user interface (view) Model
data access and business logic independent of the view and controller
View
data presentation and user interaction read-only access to the model
Controller
handling events operating on model and view
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Database Persistance
OR mapping Active Record design pattern migrations incremental schema management multiple db adapters MySQL, PostgreSQL, SQLite, SQL Server, IBM DB2, Informix, Oracle, Firebird/Interbase, LDAP, SybaseASA
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Full Stack Framework
MVC suite built-in webserver default db adapter integrated logger AJAX, web services, email test framework plugins
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Convention over Configuration
fixed directory structure everything has its place – source files, libs, plugins, database files, documentation etc file naming conventions e.g. camel case class name, underscore file name database naming conventions table names, primary and foreign keys standard configuration files e.g. database connections, environment setting definitions (development, production, test)
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
DRY - Don’t Repeat Yourself
reusing code e.g. view elements reusing data e.g. no need to declare table field names – can be read from the database making each line of code work harder e.g. mini languages for specific domains, like object-relational mapping metaprogramming dynamically created methods
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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Model - ActiveRecord
conventions:
name based OR mapping: mice db table -> Mouse class primary key: auto-incremented numeric field called "id" foreign key: "[singular_of_foreign_table_name]_id", e.g. "cat_id"
dynamic getters, setters, finders dynamic means the existance of a field in db is enough to manipulate it, e.g. dynamic getter like: mouse.name 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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Model - ActiveRecord
support for basic entity relations (1:1, 1:n, n:n) with dynamic accessors
backyard_mouse_hole.mice #=> Array of Mouse objects
parametrised queries against SQL injection, e.g. find all mice whose name is sth entered by the user:
Mouse.find(:all, :conditions => ["name = ?", mouse_name)
validators: an object won’t be saved in db unless it passes all validation rules, e.g. uniqueness of a given field:
validates_uniqueness_of :login
object life cycle callbacks, e.g.
before_create, before_save
Single Table Inheritance, polymorphic associations
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
View - ActionView
multiple template types
oldest and basic: erb (embedded ruby), similar to e.g. jsp remote javascript templates xml templates
easy reuse of view elements
file inclusion – layouts, templates, partials multiple standard "helpers" – common html element generators (e.g. form elements, paginators)
ridiculously easy AJAX integration – prototype, scriptaculous
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Controller - ActionController
all actions grouped logically in controller objects (actions are public methods of the controller class) conventions
name based url – action mapping e.g. url: myhost.com/mice/show/1 action: ’show’ action in MiceController, params: id=1 name based action - template file mapping e.g. action ’show’ in MiceController - template file app/views/mice/show.rhtml
can be organised in an inheritance hierarchy reduces code duplication and simplifies code action callbacks –
before_filter, after_filter, around_filter
built-in mechanism of url rewrites – routes easy access to request data, session, cookies
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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Rapid Development
built-in webserver generators – save the fingers scaffold – kick-off start plugins, libraries, tons of contributed code
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Debugging
verbose log output breakpoint debugger script/console
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Testing
test database + fixtures unit tests - tests for models functional - tests for controllers integration - tests for workflow testing directly in browser - Selenium
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Agile Project Heartbeat
test coverage - rcov continuous integration iterative db schema control - migrations automated deployment - capistrano
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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Platform Independence
win, lin, mac Apache, Lighttpd, mongrel gem package manager
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
IDEs
textmate vim RadRails, Eclipse + RDT, Aptana plugins for Idea, NetBeans SCiTe jEdit
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Scaling
it scales – a must-see slideshow about scaling twitter (600 req/s!): http://www.slideshare.net/Blaine/scaling-twitter view caching
page caching action caching fragment caching
sql caching
memcached
shared nothing architecture - multiplying application servers
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Hosting
http://wiki.rubyonrails.org/rails/pages/RailsWebHosts US - a variety of shared hosting offers Poland - poor shared hosting options hosted.pl Root VPS
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 sites http://twitter.com/ - community site http://www.odeo.com/ - music sharing http://www.43things.com/, http://www.43places.com/, http://www.43people.com/ 37Signals: BaseCamp, BackPack, Ta-Da List, Writeboard, CampFire http://www.strongspace.com/ - storing and sharing files
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
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Books
in English:
ruby: "Programming Ruby 2nd Edition" (the Pickaxe) Dave Thomas, with Chad Fowler and Andy Hunt "Agile Web Development with Rails" Dave Thomas and David Heinemeier Hansson "Rails Recipes" Chad Fowler many, many more: http://www.rubyonrails.org/books
in Polish:
"Programowanie w jezyku Ruby. Wydanie II" Dave Thomas, ˛ Chad Fowler, Andy Hunt "Rails. Przepisy" Chad Fowler "Ruby on Rails. Wprowadzenie" Bruce A. Tate, Curt Hibbs ´ "Ruby on Rails. Cwiczenia" Michał Sobczak
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Links
main site: http://www.rubyonrails.org PL site: http://www.rubyonrails.pl/ general mailing list: http://groups.google.com/group/rubyonrails-talk wiki: http://wiki.rubyonrails.org/rails api: api.rubyonrails.org 74 Quality Ruby on Rails Resources and Tutorials: http://www.softwaredeveloper.com/features/74-ruby-onrails-resources-tutorials-050207/
Introduction to Ruby on Rails
Design Principles
MVC architecture
Agile Development
Usability and Success Stories
Community and Resources
Events
Rails Conf Rails Day local communities & meetings Ruby on Rails workshops: http://rubyonrailsworkshops.com/ academic presence
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