260 likes | 437 Views
Why rails?. Carlos Kirkconnell. Google. Happiness leads to Productivity Happiness Matters. “Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.” Yukihiro Matsumoto
E N D
Why rails? Carlos Kirkconnell
“Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.” Yukihiro Matsumoto “Ruby makes me happy because it allows me to write beautiful code. Aesthetics are strongly linked to enjoyment. It’s not just about getting the job done”.David Heinemeier Hansson
“You can recognize truth by its beauty and simplicity. When you get it right, it is obvious that it is right.”Richard Feynman, Scientist
Account.transaction(carlos, vera)do carlos.withdraw(100) vera.deposit(100)end
classAccount< ActiveRecord::Basevalidates_presence_of:subdomain,:name,:email_address,:passwordvalidates_uniqueness_of:subdomainvalidates_acceptance_of:terms_of_service,:on=>:createvalidates_confirmation_of:password,:email_address,:on=>:createend
classProject< ActiveRecord::Basebelongs_to:portfoliohas_one:project_managerhas_many:milestoneshas_and_belongs_to_many:categoriesend
classProject< ActiveRecord::Basebelongs_to:portfoliohas_one:project_manager,:class_name=>'Person'has_many:milestones,:dependent=>truehas_and_belongs_to_many:categories,:join_table=>'categorizations'end
classAccount< ActiveRecord::Basehas_many:peopledodeffind_or_create_by_name(name) first_name, last_name = name.split last_name = last_name.join '' find_or_create_by_first_name_and_last_name(first_name, last_name)endendendperson =Account.find(:first).people.find_or_create_by_name("Gordon Freeman")person.first_name # => "Gordon"person.last_name # => "Freeman"
classPost< ActiveRecord::Basehas_many:commentsendclassComment< ActiveRecord::Basedefself.search(query) find(:all,:conditions=>["body = ?", query])endend# SELECT * FROM comments WHERE post_id = 1 AND body = 'hi'greetings =Post.find(1).comments.search('hi')
Real MVC Model Controller View .html View .xml View .atom
class PhotographersController < ApplicationController# GET /photographers# GET /photographers.xmldef index @photographers = Photographer.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render:xml => @photographers }endend# GET /photographers/1# GET /photographers/1.xmldef show @photographer = Photographer.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render:xml => @photographer }endendend
REST post get put delete HTTP create read update delete Active Record insert select update delete SQL
class Plan < ActiveRecord::Basehas_and_belongs_to_many:usersendclass User < ActiveRecord::Basehas_and_belongs_to_many:plansend
class Plan < ActiveRecord::Basehas_many:subscriptionshas_many:users, :through => :subscriptionsendclass User < ActiveRecord::Basehas_many:subscriptionshas_many:plans, :through => :subscriptionsendclass Subscription < ActiveRecord::Basebelongs_to:userbelongs_to:planvalidates_presence_of:subscription_dateend
Rails is RESTful by default • we get a free HTTP API • support for multiple mime types • simpler controllers • a better designed model
All controllers have 7 actions • index => GET /photographers • show => GET /photographers/2 • new => GET /photographers/new • edit => GET /photographers/2/edit • create => POST /photographers • update => PUT /photographers/2 • delete => DELETE /photographers/2
RJS and Ajax page[:graph].src = graph_path(@sync.burndown_id)page.replace_html :notice, flash[:notice]flash.discardpage.visual_effect :shake, :graph
Rails invites you to... • write unit, functional & integration tests • use design patterns (Active Record, MVC, Observers) • work on different environments (development, test, production) • use migrations to manage db changes • validate forms • log errors
At the cost of... • speed, ruby is interpreted... • no intellisense • no dialogs, wizards, or IDE • learning a lot about html and css