26
Why rails? Carlos Kirkconnell

Why rails? Carlos Kirkconnell. Google Happiness leads to Productivity Happiness Matters

Embed Size (px)

Citation preview

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

“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

class Account < ActiveRecord::Baseend

class Account < ActiveRecord::Base validates_presence_of :subdomain, :name, :email_address, :password validates_uniqueness_of :subdomain validates_acceptance_of :terms_of_service, :on => :create validates_confirmation_of :password, :email_address, :on => :create end

class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager has_many :milestones has_and_belongs_to_many :categoriesend

class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager, :class_name => 'Person' has_many :milestones, :dependent => true has_and_belongs_to_many :categories, :join_table => 'categorizations'end

class Account < ActiveRecord::Base has_many :people do def find_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) end endend

person = Account.find(:first).people.find_or_create_by_name("Gordon Freeman")person.first_name # => "Gordon"person.last_name # => "Freeman"

class Post < ActiveRecord::Base has_many :commentsend

class Comment < ActiveRecord::Base def self.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

ModelModel

ControlControllerler

ViewView.html.html

ViewView.xml.xml

ViewView.ato.atomm

class PhotographersController < ApplicationController # GET /photographers # GET /photographers.xml def index @photographers = Photographer.find(:all)

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @photographers } end end

# GET /photographers/1 # GET /photographers/1.xml def show @photographer = Photographer.find(params[:id])

respond_to do |format| format.html # show.html.erb format.xml { render :xml => @photographer } end end

end

creatcreatee

readread updatupdatee

deletdeletee

postpost getget putput deletdeletee

insertinsert selectselect updatupdatee

deletdeletee

HTTP

Active Record

SQL

REST

RESTIt’s all about CRUD

class Plan < ActiveRecord::Base has_and_belongs_to_many :usersend

class User < ActiveRecord::Base has_and_belongs_to_many :plansend

class Plan < ActiveRecord::Base has_many :subscriptions has_many :users, :through => :subscriptionsend

class User < ActiveRecord::Base has_many :subscriptions has_many :plans, :through => :subscriptionsend

class Subscription < ActiveRecord::Base belongs_to :user belongs_to :plan validates_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

page[:graph].src = graph_path(@sync.burndown_id)page.replace_html :notice, flash[:notice]flash.discardpage.visual_effect :shake, :graph

RJS and Ajax

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

Quesions?