Ruby On Rails Boris Nadion astrails.com Ruby On Rails - - PowerPoint PPT Presentation

ruby on rails
SMART_READER_LITE
LIVE PREVIEW

Ruby On Rails Boris Nadion astrails.com Ruby On Rails - - PowerPoint PPT Presentation

Ruby On Rails Boris Nadion astrails.com Ruby On Rails http://www.flickr.com/photos/jocelyndurston/ why Ruby on Rails? it is beautiful credit: http://www.flickr.com/photos/psilver/ it is almost pure english read the english, not a code


slide-1
SLIDE 1

Ruby On Rails

Boris Nadion astrails.com

slide-2
SLIDE 2

Ruby On Rails

slide-3
SLIDE 3

http://www.flickr.com/photos/jocelyndurston/

slide-4
SLIDE 4

why Ruby on Rails?

slide-5
SLIDE 5

it is beautiful

slide-6
SLIDE 6

credit: http://www.flickr.com/photos/psilver/

slide-7
SLIDE 7

it is almost pure english

slide-8
SLIDE 8

read the english, not a code

slide-9
SLIDE 9

Account.transaction(quentin, alice) do quentine.withdraw(100) alice.deposite(100) end

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Project.last.milestones.create(:due_date => 10.days.from_now, :name => "html integration")

slide-13
SLIDE 13

class UsersController < ApplicationController before_filter :login_required, :only => [:edit, :update] session :off, :only => :feed end

slide-14
SLIDE 14

really beautiful, isn’t it?

slide-15
SLIDE 15

"When I am working on a problem, I never think about beauty ... but when I have finished, if the solution is not beautiful, I know it is wrong." Buckminster Fuller

slide-16
SLIDE 16

ruby

slide-17
SLIDE 17

http://www.flickr.com/photos/chough/

slide-18
SLIDE 18

ruby - is a language

slide-19
SLIDE 19
  • created by Yakihiro “matz”

Matsumoto

  • initial public release at 1995
  • free to use, copy, modify, and

distribute ruby

slide-20
SLIDE 20

www.ruby-lang.org

slide-21
SLIDE 21

www.jruby.org

slide-22
SLIDE 22

ruby runs on desktops

slide-23
SLIDE 23

ruby runs on servers

slide-24
SLIDE 24

rails

slide-25
SLIDE 25

credit: http://www.flickr.com/photos/ecstaticist/

slide-26
SLIDE 26

rails - is a framework

slide-27
SLIDE 27
  • created by David Heinemeier

Hansson

  • initial public release at 2003
  • more then 1400 contributors

rails

slide-28
SLIDE 28

a lot of ways

slide-29
SLIDE 29

really really good framework to create in a simple, easy, and a beautiful way

slide-30
SLIDE 30

productivity

slide-31
SLIDE 31

easier and faster

slide-32
SLIDE 32

less code

slide-33
SLIDE 33

import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; public class TimeServer { private static class TellTime extends Thread { private Socket soc; public TellTime(Socket soc) { super(); this.soc = soc; } public void run() { try { this.soc.getOutputStream().write( new Date().thisoString().getBytes()); } catch (Exception e) { } finally { try { this.soc.close(); } catch (IOException e1) { } } } } public static void main(String args[]) throws Exception { ServerSocket server = new ServerSocket(12345); while (true) { new TellTime(server.accept()).start(); } } } require "socket" server = TCPServer.new(12345) while (session = server.accept) Thread.new(session) do |my_session| my_session.puts Time.new my_session.close end end

slide-34
SLIDE 34

less code - less bugs

slide-35
SLIDE 35

less code - less maintenance

slide-36
SLIDE 36

less effort

slide-37
SLIDE 37

credit: http://www.flickr.com/photos/slworking/

slide-38
SLIDE 38

perfect for web startups

slide-39
SLIDE 39

maintainability

slide-40
SLIDE 40
  • make it quickly
  • ask your users
  • change and update often
slide-41
SLIDE 41

get to the point

slide-42
SLIDE 42

MVC

slide-43
SLIDE 43

model controller view

slide-44
SLIDE 44

separation of concerns

slide-45
SLIDE 45

business logic in models

slide-46
SLIDE 46

credit: http://www.flickr.com/photos/simonpais/

slide-47
SLIDE 47

data representation in views

slide-48
SLIDE 48

credit: http://www.flickr.com/photos/practicalowl/

slide-49
SLIDE 49

requests handling in controllers

slide-50
SLIDE 50

credit: http://www.flickr.com/photos/shagy6six6

slide-51
SLIDE 51
  • pinionated
slide-52
SLIDE 52

rails tells you how the things should be done

slide-53
SLIDE 53

convention over configuration

slide-54
SLIDE 54

you can always do it in your way

slide-55
SLIDE 55

if you’re stuck

slide-56
SLIDE 56

credit: bill barber, http://www.flickr.com/photos/21861018@N00/2516333685/

slide-57
SLIDE 57

probably you are doing something wrong

slide-58
SLIDE 58

MVC

slide-59
SLIDE 59

ActiveRecord - model

http://en.wikipedia.org/wiki/Active_record_pattern

slide-60
SLIDE 60

talks with database via adapters

slide-61
SLIDE 61

all major DBs

  • MySQL
  • SQLite
  • PostgreSQL
  • MS SQL
  • DB2
  • Oracle

and many others

slide-62
SLIDE 62

ActiveRecord builds complex SQL queries for you, efficiently

slide-63
SLIDE 63

three environments

  • development
  • production
  • test
slide-64
SLIDE 64

development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: mysql username: sqluser password: ilovemysql123 encoding: utf8 socket: /tmp/mysql.sock

slide-65
SLIDE 65

company = Company.new company.name = "Astrails" company.save company.id # => 12 user = company.employees.create(:name => "Boris") user.name # => "Boris"

slide-66
SLIDE 66

associations

  • ne-to-one
  • ne-to-many

many-to-many

slide-67
SLIDE 67

class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager has_many :milestones has_many :tasks, :through => :milestones end

slide-68
SLIDE 68

validations

slide-69
SLIDE 69

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

slide-70
SLIDE 70

callbacks

slide-71
SLIDE 71

class Account < ActiveRecord::Base attr_accessor :password def before_create self.crypted_password = crypt(password) end end

slide-72
SLIDE 72

before_update after_create

before_validation_on_update

after_find ....

slide-73
SLIDE 73

migrations

slide-74
SLIDE 74

class RemoveUnnecessaryItemAttributes < ActiveRecord::Migration def self.up remove_column :items, :incomplete_items_count remove_column :items, :completed_items_count end def self.down add_column :items, :incomplete_items_count add_column :items, :completed_items_count end end

slide-75
SLIDE 75

many other features

slide-76
SLIDE 76

credit: http://www.flickr.com/photos/shagy6six6

slide-77
SLIDE 77

controller

slide-78
SLIDE 78

class ProjectsController < ActionController::Base def create @project = Project.new(params[:project]) if @project.save flash[:notice] = "Project created" # The entry was saved correctly, redirect to index redirect_to projects_path else flash.now[:notice] = "Fix errors and try again" render :action => :new end end end

slide-79
SLIDE 79

CRUD

http://en.wikipedia.org/wiki/Create,_read,_update_and_delete

slide-80
SLIDE 80

create read update delete

slide-81
SLIDE 81

# create def create end # read def show end # update def update end # delete def destroy end

slide-82
SLIDE 82

# create def create end # read def show end # update def update end # delete def destroy end # form for new object def new end # form for edit object def edit end # list of objects def index end

slide-83
SLIDE 83

unique URL for every action

slide-84
SLIDE 84

GET /books {:action=>"index", :controller=>"books"} POST /books {:action=>"create", :controller=>"books"} GET /books/new {:action=>"new", :controller=>"books"} GET /books/:id/edit {:action=>"edit", :controller=>"books"} GET /books/:id {:action=>"show", :controller=>"books"} PUT /books/:id {:action=>"update", :controller=>"books"} DELETE /books/:id {:action=>"destroy", :controller=>"books"}

slide-85
SLIDE 85

filters

slide-86
SLIDE 86

class UsersController < ApplicationController before_filter :login_required, :only => [:edit, :update] protected def login_required # ... end end

slide-87
SLIDE 87

web APIs

slide-88
SLIDE 88

def index @people = Person.find(:all) respond_to do |format| format.html format.xml { render :xml => @people.to_xml } format.js { render :json => @people.to_json } end end

slide-89
SLIDE 89

views

slide-90
SLIDE 90

credit: http://www.flickr.com/photos/practicalowl/

slide-91
SLIDE 91

templates

slide-92
SLIDE 92
  • HAML
  • ERB
  • Liquid
  • Amrita
  • Markaby
slide-93
SLIDE 93
  • HAML
  • ERB
  • Liquid
  • Amrita
  • Markaby
slide-94
SLIDE 94

%html %body User = @user.name %ul %li Age = @user.age %li Interests = @user.interests.join(', ') %li Occupation = @user.occupation

slide-95
SLIDE 95

<html> <body> User <%= @user.name %>: <ul> <li>Age: <%= @user.age %></li> <li>Interests: <%= @user.interests.join(', ') %></li> <li>Occupation: <%= @user.occupation %></li> </ul> </body> </html>

slide-96
SLIDE 96

<%= any_ruby_code %> <% any_ruby_code %>

slide-97
SLIDE 97

helpers

slide-98
SLIDE 98

simplify html generation

slide-99
SLIDE 99

<% form_for @person do |f| %> <%= f.text_field :first_name %> <%= f.text_field :last_name %> <%= f.submit 'Create' %> <% end %>

slide-100
SLIDE 100

<% form_for @person do |f| %> <%= f.text_field :first_name %> <%= f.text_field :last_name %> <%= f.submit 'Create' %> <% end %> <form action="/people/create" method="post"> <input id="person_first_name" name="person[first_name]" type="text" /> <input id="person_last_name" name="person[last_name]" type="text" /> <input name="commit" type="submit" value="Create" /> </form>

slide-101
SLIDE 101

xml

slide-102
SLIDE 102

# Builder xml.instruct! :xml, :version=>"1.0" xml.instruct! :rss, :version=>"2.0" xml.channel{ for hat in @hats xml.hat do xml.title(hat.name) xml.description(hat.description) end end }

slide-103
SLIDE 103

putting all together

slide-104
SLIDE 104

web server controller model view db

request dispatch/routing query uses renders displays

simplified flow

slide-105
SLIDE 105

short demo

slide-106
SLIDE 106

test your code

slide-107
SLIDE 107

JUnit

slide-108
SLIDE 108

RSpec

slide-109
SLIDE 109

describe "Sum computation" do it "should return 2" do (1 + 1).should == 2 end end ./spec/basic_test.rb

slide-110
SLIDE 110

describe "Sum computation" do it "should return 2" do (1 + 1).should == 2 end end > spec ./spec/basic_test.rb . Finished in 0.040745 seconds 1 example, 0 failures ./spec/basic_test.rb

slide-111
SLIDE 111

describe "Sum computation" do it "should return 2" do (1 + 2).should == 2 end end > spec ./spec/basic_test.rb F 1) 'Sum computation should return 2' FAILED expected: 2, got: 3 (using ==) ./span.basic_test:3: Finished in 0.00762 seconds 1 example, 1 failure ./spec/basic_test.rb

slide-112
SLIDE 112

describe User do before(:each) do @user = User.new end it "should be invalid without a username" do @user.should_not be_valid @user.username = 'someusername' @user.should be_valid end end

slide-113
SLIDE 113

TDD & BDD

http://en.wikipedia.org/wiki/Test-driven_development http://en.wikipedia.org/wiki/Behavior_Driven_Development

slide-114
SLIDE 114

lucky

slide-115
SLIDE 115

software

slide-116
SLIDE 116

Ruby On Rails

slide-117
SLIDE 117
  • MVC
  • maintainability
  • time to market
  • lower development cost
  • fun
slide-118
SLIDE 118

what’s next?

slide-119
SLIDE 119
  • download and install ruby
  • download and install rails
  • build your rails application
slide-120
SLIDE 120

welcome to the club

slide-121
SLIDE 121

thank you q&a

Boris Nadion astrails.com