33
Creating & Maintain Web Applications Avi Kedar, 8-2013

Web Development using Ruby on Rails

Embed Size (px)

Citation preview

Page 1: Web Development using Ruby on Rails

Creating & Maintain Web Applications

Avi Kedar, 8-2013

Page 2: Web Development using Ruby on Rails

New web approach

Programmers sucks

ails on b (Dynamic, KISS)

Development booster

Deploy booster

Rails on

Ruby

Rails

Page 3: Web Development using Ruby on Rails

Server

Data Layer

Pre

se

nta

tion

Logic

Page 4: Web Development using Ruby on Rails

Server

Data Layer

Pre

se

nta

tion

Logic

Page 5: Web Development using Ruby on Rails

Front End

SPA

MV*

Responsive

OpenId

Routing

Duplex

Page 6: Web Development using Ruby on Rails

Front End

Presentation

Logic

Da

taServer

Logic +

Data

Server

Logic +

Data

• Queue

• Distributed

Cache

• CDN

• Storage

• Processing

Cloud

ServicesJSON

REST

JSON

REST

JSON

REST

Page 7: Web Development using Ruby on Rails

Front End

Server

Logic + Data

Cloud

Services

• Stateless

• Data oriented

• Small functioning parts

• Less scale-up

• Easy to integrate

• Fast implementation

(80/20)

Page 8: Web Development using Ruby on Rails

Technology helps creating good projects, it doesn’t lead.

Test your code, use patterns, avoid stickiness, use third-parties

Page 9: Web Development using Ruby on Rails

• Launched in 2005

• Combination of Perl, Smalltalk and Lisp

• Created by Yukihiro Matsumoto

• Open Source

Often people, especially computer engineers, focus on the

machines. They think, "By doing this, the machine will run

faster. By doing this, the machine will run more effectively.

By doing this, the machine will something something

something." They are focusing on machines. But in fact we

need to focus on humans, on how humans care about doing

programming or operating the application of the machines.

We are the masters. They are the slaves.

Yukihiro Matsumoto

Page 10: Web Development using Ruby on Rails

• Executes on runtime

• “Free hand”, no type restrictions

• Strong reflection abilities

• Metaprogramming

Dynamic

No pre-compile, no build, truly open for extensions

Page 11: Web Development using Ruby on Rails

• Executes on runtime

• “Free hand”, no type restrictions

• Strong reflection abilities

• Metaprogramming

Dynamic

a = [1, 'hi', 3.14, 1, 2, [4, 5]]

hash = { water:'wet', fire:'hot' }

l = lambda { "Do or do not" }

class Time

def yesterday

self - 86400

end

end

module Debug

def whoAmI?

return "#{self.type.name} (\##{self.id}): #{self.to_s}"

end

end

class Toolbar

include Debug

# ...

end

Page 12: Web Development using Ruby on Rails

• Executes on runtime

• “Free hand”, no type restrictions

• Strong reflection abilities

• Metaprogramming

Dynamic1.methods

[:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, ...]

obj.respond_to?(:each)

obj.is_a?(FixNum)

obj.instance_of?(i)

obj.nil?

"hello".class

"hello".variables

Page 13: Web Development using Ruby on Rails

• Executes on runtime

• “Free hand”, no type restrictions

• Strong reflection abilities

• Metaprogramming

Dynamicobj.send :say_hello, 'Matz'

define_method :hello do |my_arg|

end

obj.instance_variable_set(:@x,10)

Page 14: Web Development using Ruby on Rails

• Executes on runtime

• “Free hand”, no type restrictions

• Strong reflection abilities

• Metaprogramming

DynamicCOLORS =

{ black: "000", green: "0f0", blue: "00f“, white: "fff" }

class String

COLORS.each do |color,code|

define_method "in_#{color}" do

"<span style=\"color: ##{code}\">#{self}</span>"

end

end

end

"Hello, World!".in_blue

=> "<span style=\"color: #00f\">Hello, World!</span>"

Page 15: Web Development using Ruby on Rails

• Convention, convention, convention

• Write less

• ?, !, @, @@, ||=

• Read it

KISSYou don’t write code in Ruby

You’re using Ruby style and practices

to write better code

… loops, blocks, dependencies, conditions, etc. …

Page 16: Web Development using Ruby on Rails

• Convention, convention, convention

• Write less

• ?, !, @, @@, ||=

• Read it

KISS

forget_about { } ( )

Always returns value

Always receive block

Person.new "Bob", 33

class Time

def yesterday

self - 86400

end

end

if number > 0

"#{number} is positive"

else

"#{number} is negative"

end

def demonstrate_block number

yield number

end

puts demonstrate_block(1) do |number|

number + 1

end

Page 17: Web Development using Ruby on Rails

• Convention, convention, convention

• Write less

• ?, !, @, @@, ||=

• Read it

KISS person1.empty?

person1.save!

@first_name = “John”

@@employees_count = 55

DEFAULT_LOCATION = “Israel”

id ||= Guid.new

Page 18: Web Development using Ruby on Rails

• Convention, convention, convention

• Write less

• ?, !, @, @@, ||=

• Read it

KISS

(1..10).collect {|x| x*x}

menu.delete_if {|key,value| value=='hot'}

credit_card.pay! unless products.empty?

5.times { send_sms_to "xxx" }

Page 19: Web Development using Ruby on Rails

• Launched in 2005

• Created by David Hansson

• Open Source

• Stack framework

Test

MVC

Web ServerAction

MailerRake

RESTfull ActiveRecord

Page 20: Web Development using Ruby on Rails

• Convention, convention, convention

• Reuse, collaboration

• Test. Period.

• Generators

Dev Booster • IDE (simply

console)

• magic

• wizards

• config

• attributes

• dll’s

• threads

• IOC

• unused files, code

• MVC

• REST

• data layer

• Easy to integrate

• it looks stupid

NO YES

Page 21: Web Development using Ruby on Rails

• Convention, convention, convention

• Reuse, collaboration

• Test. Period.

• Generators

Dev Booster • IDE (simply

console)

• magic

• wizards

• config

• attributes

• dll’s

• threads

• IOC

• unused files, code

• MVC

• REST

• data layer

• it looks stupid

NO YES

index.html.haml

show.js.coffee

Page 22: Web Development using Ruby on Rails

• Convention, convention, convention

• Reuse, collaboration

• Test. Period.

• Generators

Dev Booster

Gem file

You need an enormous reason

to code something yourself.

Easy to integrate

Easy to use

Fully documented

Active community

Page 23: Web Development using Ruby on Rails

• Convention, convention, convention

• Reuse, collaboration

• Test. Period.

• Generators

Dev Booster

• Rspec for unit tests

• Factory-girl for data

generator

• Capybara for integration test

• Built in mocking system

• Integrated with Rails, generators

and build machines, huge amount of artifacts

When “I sign in” do

within "#login-form" do

fill_in 'Login', with: '[email protected]'

fill_in 'Password', with: 'password'

end

click_link 'Sign in'

end

Capybara

Rspec

Team-

city

Factory-

girl

Fantom

Browser

Ra

ils M

oc

kin

g &

Ge

ne

rato

rs

JS, CSS, HTML, Ruby…

Page 24: Web Development using Ruby on Rails

• Convention, convention, convention

• Reuse, collaboration

• Test. Period.

• Generators

Dev Booster • rails g controller Tweets

• rails g controller Tags report_tag like_tag

• rails g model Presentations title:string slides_count:integer

• rails g rspec:controller Products

• rails g scaffold OnlineGames game:string score:integer

g

Page 25: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast deploy

Deploy Booster

Rails Application

Page 26: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• built in relational ORM

• Part of the MVC (Model)

• No SQL syntax, all using ActiveRecord built in functions (Ruby):

ActiveRecord

users = User.all

Manage Databases

• DB create/alter tables, indexes, etc. via ActiveRecord (Ruby):

create_table :products do |t|t.string :namet.string :part_number

end

• Database structure and versioning using migrations

20111226120533_create_delayed_jobs.rb

david = User.where(name:'David')

david.age = 36

david.save!

Page 27: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• Full versions and dependencies management

• Group of gems per environment

• Install and upgrade all using single command

Gems

bundle install

bundle update

gem 'SystemTimer', require: 'system_timer'

gem 'feedback_popup'

gem 'i18n-js'

group :production do

gem 'asset_sync'

gem 'turbo-sprockets-rails3'

gem 'coffee-rails'

gem 'closure-compiler'

gem 'yui-compressor'

end

Page 28: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• Single place to manage all your css, js, images and third-

parties

• Stack based manager called Assets Pipeline (easy to extend)

(uglifier, unite to a single file, validate)

• Control the order of bootstraping

Assets

Vendors

• Manage all third-parties client side plugins (js and css)

(jquery, angular-js, twitter-bootstrap, etc.)

• Fully integrated with the assets pipeline

Page 29: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• Support multiple environment separation using yaml config

• Built-in test, development and production environment

• Full separation: DB, dependencies, testing, data-seed

Multi -environment

Page 30: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• As fast as 3 steps to create a full active machine

Fast Setup

git pull <branch>

bundle

rake db:migrate

• Single Responsibilty:

o DB using SQLite

o Server built in

o UT support built in

• Can illustrate production using multi-env

rails s –e production

Page 31: Web Development using Ruby on Rails

• Full responsibilityo DB

o Gems

o Assets

o Vendors

• Multi-environment

• Fast Setup

• Fast deploy

Deploy Booster• Using git as source control and deploy tool

• Using artifacts for measure and investigate your code (UT, code

coverage, memory lakes, etc.)

• Fully integrated with Saas hosting platforms:

o deploy in one click

o Deploy software not infrastructure

Add-ons: db-backups, logs, ssl, etc.

• Scale-out in not a forbidden word:

o Build for the cloud, so easy to integrate:

Queue, CDN, storage, etc.

o Scale your project in a single click

Fast Deploy

Build. Deploy. Scale.

git push production 2.4

Page 32: Web Development using Ruby on Rails

Client side manage the flow and contains all presentation logic and view

Web servers should be data-oriented focusing on easy of scaling out and delivery speed

Ruby designed for programmers not machine: helps creating beautiful code, self-explained and dynamic

Rails enable to quickly create web servers:

Convention

Reuse

Single responsibility

Quick setup and deploy

Built-in scale out

Page 33: Web Development using Ruby on Rails

Avi Kedar, 8-2013