why rails?

26
Why rails? Carlos Kirkconnell

Upload: thu

Post on 05-Jan-2016

43 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Why rails?

Why rails?Carlos Kirkconnell

Page 2: Why rails?

Google

Page 3: Why rails?

Happiness leads to Productivity

Happiness Matters

Page 4: Why rails?

“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

Page 5: Why rails?

“You can recognize truth by its beauty and simplicity. When you get it right, it is obvious

that it is right.”Richard Feynman, Scientist

Page 6: Why rails?

Account.transaction(carlos, vera) do carlos.withdraw(100) vera.deposit(100)end

Page 7: Why rails?
Page 8: Why rails?

class Account < ActiveRecord::Baseend

Page 9: Why rails?
Page 10: Why rails?

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

Page 11: Why rails?

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

Page 12: Why rails?

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

Page 13: Why rails?

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"

Page 14: Why rails?

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')

Page 15: Why rails?

Real MVC

ModelModel

ControlControllerler

ViewView.html.html

ViewView.xml.xml

ViewView.ato.atomm

Page 16: Why rails?

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

Page 17: Why rails?

creatcreatee

readread updatupdatee

deletdeletee

postpost getget putput deletdeletee

insertinsert selectselect updatupdatee

deletdeletee

HTTP

Active Record

SQL

REST

Page 18: Why rails?

RESTIt’s all about CRUD

Page 19: Why rails?

class Plan < ActiveRecord::Base has_and_belongs_to_many :usersend

class User < ActiveRecord::Base has_and_belongs_to_many :plansend

Page 20: Why rails?

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

Page 21: Why rails?

Rails is RESTful by default

•we get a free HTTP API

•support for multiple mime types

•simpler controllers

•a better designed model

Page 22: Why rails?

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 23: Why rails?

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

RJS and Ajax

Page 24: Why rails?

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

Page 25: Why rails?

At the cost of...

•speed, ruby is interpreted...

•no intellisense

•no dialogs, wizards, or IDE

•learning a lot about html and css

Page 26: Why rails?

Quesions?