Ruby and rails - Advanced Training (Cybage)

93
Ruby and Rails Advanced Training @gautamrege @joshsoftware

description

Cybage Advanced Training - customised

Transcript of Ruby and rails - Advanced Training (Cybage)

Page 1: Ruby and rails - Advanced Training (Cybage)

Ruby and Rails Advanced Training

@gautamrege @joshsoftware

Page 2: Ruby and rails - Advanced Training (Cybage)

Agenda - Day 1Discuss Ruby basics

Syntax, classes, modules. Meta-programming. Closures

Rails concepts Discussion around Rails design patterns. ActiveSupport Concern Rails engines

Page 3: Ruby and rails - Advanced Training (Cybage)

Agenda - Day 2Discuss your current Applications.

Gems currently in use

Resolving problems.

ActiveResource

Rails Security

Data Caching

Performace monitoring and improvement

Page 4: Ruby and rails - Advanced Training (Cybage)

Ruby is easy, right?

Page 5: Ruby and rails - Advanced Training (Cybage)

Ruby is easy, right?

1 + 1

Page 6: Ruby and rails - Advanced Training (Cybage)

Ruby is easy, right?

1 + 1

1.+(1)

Page 7: Ruby and rails - Advanced Training (Cybage)

Ruby is easy, right?

1 + 1

1.+(1)

1.+ 1

Page 8: Ruby and rails - Advanced Training (Cybage)

Ruby is easy, right?

1 + 1

1.+(1)

1.+ 1

1.send(:+, 1)

Page 9: Ruby and rails - Advanced Training (Cybage)

How many objects?

Page 10: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

Page 11: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

Page 12: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 13: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 14: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 15: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 16: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 17: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3)

Page 18: Ruby and rails - Advanced Training (Cybage)

How many objects?

1 + 2 + 3

( 1.+(2) ).+(3) FIVE objects

Page 19: Ruby and rails - Advanced Training (Cybage)

Everything in Ruby

is an object

on which we call a method

to which we pass parameters

and an optional block of code

Page 20: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

Page 21: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

a[1] # What is the data type of a?

Page 22: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

a[1] # What is the data type of a?

a = [‘a’, ‘b’] # Array

Page 23: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

a[1] # What is the data type of a?

a = [‘a’, ‘b’] # Array

a = { 1 => ‘a’} # Hash

Page 24: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

a[1] # What is the data type of a?

a = [‘a’, ‘b’] # Array

a = { 1 => ‘a’} # Hash

a = “abc” # String

Page 25: Ruby and rails - Advanced Training (Cybage)

So, you know Ruby?

a[1] # What is the data type of a?

a = [‘a’, ‘b’] # Array

a = { 1 => ‘a’} # Hash

a = “abc” # String

a = Proc.new { | x | p x } # proc

Page 26: Ruby and rails - Advanced Training (Cybage)

Multiplication

Page 27: Ruby and rails - Advanced Training (Cybage)

Multiplication

[1, 2, 3] * 2

Page 28: Ruby and rails - Advanced Training (Cybage)

Multiplication

[1, 2, 3] * 2

# => [1, 2, 3, 1, 2, 3]

Page 29: Ruby and rails - Advanced Training (Cybage)

Multiplication

[1, 2, 3] * 2

# => [1, 2, 3, 1, 2, 3]

[1, 2, 3] * “%”

Page 30: Ruby and rails - Advanced Training (Cybage)

Multiplication

[1, 2, 3] * 2

# => [1, 2, 3, 1, 2, 3]

[1, 2, 3] * “%”

# => “1%2%3”

Page 31: Ruby and rails - Advanced Training (Cybage)

There’s always an easier way

Summation of [1, 2, 3, 4]

Page 32: Ruby and rails - Advanced Training (Cybage)

Non-ruby way!sum = 0 !

for i in [1, 2, 3, 4] sum += i end !

p sum

Page 33: Ruby and rails - Advanced Training (Cybage)

The Amateur!

sum = 0 !

[1, 2, 3, 4].each do |i| sum += i end !

p sum

Page 34: Ruby and rails - Advanced Training (Cybage)

The Professional!

!

[1, 2, 3, 4].inject(0) do |sum, i| sum + i end

Page 35: Ruby and rails - Advanced Training (Cybage)

The Expert!

!

[1, 2, 3, 4].inject(:+)

Page 36: Ruby and rails - Advanced Training (Cybage)

Exceptions… never! class Base

def method_missing(name, *args, &blk)

puts “Unknown method #{name}called.”

end

end

b = Base.new

b.wtf

Page 37: Ruby and rails - Advanced Training (Cybage)

What’s in a name?# User(id: integer, name: string)

class User < ActiveRecord::Base

end u = User.name

u.name

u.name = “Gautam”

Page 38: Ruby and rails - Advanced Training (Cybage)

Accessing Dataclass User

def initialize

@name = “someone”

end

end u = User.name

u.name

u.name = “Gautam”

Page 39: Ruby and rails - Advanced Training (Cybage)

Protected & Private

Private methods are inherited

Protected method can be called on another object in same lineage

What the …

Page 40: Ruby and rails - Advanced Training (Cybage)

Module Mixinsclass Shaktiman

include Spiderman

include Superman

end

irb> Shaktiman.ancestors

=> [ Shaktiman, Superman, Spiderman …]

Page 41: Ruby and rails - Advanced Training (Cybage)

Closures

sum = 0

[1, 2, 3, 4].each do |x|

sum += i

end

p sum

Anonymous functions? Local variables scope?

Page 42: Ruby and rails - Advanced Training (Cybage)

Closures!

Context sensitive block of code

Page 43: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Page 44: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Singleton

Page 45: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Singleton

Factory

Page 46: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Singleton

Factory

Observer

Page 47: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Singleton

Factory

Observer

Page 48: Ruby and rails - Advanced Training (Cybage)

Rails Design Patterns

Singleton

Factory

Observer

You cannot learn Design Patterns!

!

You need to experience them

Page 49: Ruby and rails - Advanced Training (Cybage)

SOLID Design Patterns

Page 50: Ruby and rails - Advanced Training (Cybage)

SOLID principles

S - Single Responsibilty

O - Open / Closed

L - Liskov Substituion

I - Interface Segregation

D - Dependency Inversion

Page 51: Ruby and rails - Advanced Training (Cybage)

Single Responsibility

Serves only one purpose.

Models - manage data

Controllers - manage logic

No logic in views!

Page 52: Ruby and rails - Advanced Training (Cybage)

Open / Closed

Open for Extension

Closed for modification

Page 53: Ruby and rails - Advanced Training (Cybage)

Open / Closed

Open for Extension

Closed for modification

ParseFeed

Page 54: Ruby and rails - Advanced Training (Cybage)

Open / Closed

Open for Extension

Closed for modification

ParseFeed

Atom RSS

ParseFeed

Page 55: Ruby and rails - Advanced Training (Cybage)

Liskov Substituion

Functionality should work for all derived classes.

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is subtype of T

Page 56: Ruby and rails - Advanced Training (Cybage)

Interface Segregation

Classification based on behaviour

Vehicle

AirRoad

TrainCar RocketAirplane

Page 57: Ruby and rails - Advanced Training (Cybage)

Dependency Inversion

High level modules should not depend on low level modules

Modules should depend on abstractions

Details should depend on abstractions

Page 58: Ruby and rails - Advanced Training (Cybage)

ActiveSupport Concern

Page 59: Ruby and rails - Advanced Training (Cybage)

Without ActiveSupport Concerns

module Foo

def self.included(base)

base.class_eval do

scope :disable, -> { where(disabled: true) }

end

end

end

class Base include Foo end

Page 60: Ruby and rails - Advanced Training (Cybage)

With ActiveSupport Concerns

module Foo

extend ActiveSupport::Concern

included do

scope :disable, -> { where(disabled: true) }

end

endclass Base include Foo end

Page 61: Ruby and rails - Advanced Training (Cybage)

Dependent Modules

Page 62: Ruby and rails - Advanced Training (Cybage)

Dependent Modulesmodule Foo

def self.included(base)

base.class_eval do

def self.foo_method

end

end

end

end

Page 63: Ruby and rails - Advanced Training (Cybage)

Dependent Modulesmodule Foo

def self.included(base)

base.class_eval do

def self.foo_method

end

end

end

end

module Bar

def self.included(base)

base.foo_method

end

end

Page 64: Ruby and rails - Advanced Training (Cybage)

Dependent Modulesmodule Foo

def self.included(base)

base.class_eval do

def self.foo_method

end

end

end

end

module Bar

def self.included(base)

base.foo_method

end

end

class Base include Foo include Bar end

Page 65: Ruby and rails - Advanced Training (Cybage)

Dependent Modulesmodule Foo

def self.included(base)

base.class_eval do

def self.foo_method

end

end

end

end

module Bar

def self.included(base)

base.foo_method

end

end

class Base include Foo include Bar end

add module dependency #!!

Page 66: Ruby and rails - Advanced Training (Cybage)

Module dependencies

Page 67: Ruby and rails - Advanced Training (Cybage)

Module dependencies

module Bar

include Foo

included do

def self.foo_method

end

end

class Base include Bar end

Page 68: Ruby and rails - Advanced Training (Cybage)

Module dependencies

module Bar

include Foo

included do

def self.foo_method

end

end

class Base include Bar end

self = Bar self != Base

Page 69: Ruby and rails - Advanced Training (Cybage)

Module dependencies

module Bar

include Foo

included do

def self.foo_method

end

end

class Base include Bar end

self = Bar self != Base

Page 70: Ruby and rails - Advanced Training (Cybage)

Module dependencies

module Bar

extend ActiveSupport::Concern

include Foo

included do

def self.foo_method

end

endclass Base include Bar end

no including extra modues

self = Base

Page 71: Ruby and rails - Advanced Training (Cybage)

Rails Engines

Page 72: Ruby and rails - Advanced Training (Cybage)

Engine power

Velocity App

CarRocket

Some custom Functionality

Page 73: Ruby and rails - Advanced Training (Cybage)

CarRocket

Some custom functionality

Page 74: Ruby and rails - Advanced Training (Cybage)

CarRocket

Some custom functionality

Astronaut

Shuttles

M

M

Page 75: Ruby and rails - Advanced Training (Cybage)

CarRocket

Some custom functionality

Astronaut

Shuttles

M

M

Driver

Rto

M

1

Page 76: Ruby and rails - Advanced Training (Cybage)

CarRocket

Some custom functionality

Astronaut

Shuttles

M

M

Driver

Rto

M

1

Company

Page 77: Ruby and rails - Advanced Training (Cybage)

Quick Reference

rails plugin new <name> —mountable

rails g scaffold <model> <name>:<type>

test/dummy: testing engine.

Page 78: Ruby and rails - Advanced Training (Cybage)

GamePlay - CarCreate Car engine

Create RTO scaffold

name:string code:string

Create Driver scaffold

name:string rto_id:integer has_licence:boolean

Page 79: Ruby and rails - Advanced Training (Cybage)

Testing - Car

rake db:migrate

cd test/dummy

rails s

http://localhost:300/car

Page 80: Ruby and rails - Advanced Training (Cybage)

GamePlay-RocketCreate Rocket engine

Create Astronaut scaffold

name:string degree:text

Create Shuttle scaffold

name:string launch_from:text

Create Space_flights scaffold

shuttle_id:integer astronaut_id:integer launch_on:date

Page 81: Ruby and rails - Advanced Training (Cybage)

Testing - Rocket

rake db:migrate

cd test/dummy

rails s

http://localhost:3000/rocket

Page 82: Ruby and rails - Advanced Training (Cybage)

Application Stuffrails new velocity

update Gemfile

gem ‘car’, path: <path>

gem ‘rocket’, path: <path>

config/routes:

mount Car::Engine, at: ‘/car’

mount Rocket::Engine, at: ‘/engine’

Page 83: Ruby and rails - Advanced Training (Cybage)

Application Logic

Company scaffold

name:string make_cars:boolean make_rockets:boolean owner:string

Page 84: Ruby and rails - Advanced Training (Cybage)

Using Application models in engines.Add migration for owner_id

class Shuttle

belongs_to :owner, class: “Company”

end

NEVER hardcode associations directly in engines. Use class option.

Page 85: Ruby and rails - Advanced Training (Cybage)

More ???

Page 86: Ruby and rails - Advanced Training (Cybage)

Devise Authentication

Gemfile

gem ‘devise’

rails g devise:install

rails g device user

rake db:migrate

Page 87: Ruby and rails - Advanced Training (Cybage)

Inherit from App# rocket/app/controller/rocket/application_controller.rb

class Rocket::ApplicationController < ApplicationController

end

# shuttles_controller.rb

class ShuttleController < ApplicationControler

before_action :authenticate_user!

end

Page 88: Ruby and rails - Advanced Training (Cybage)

ChecklistAutomatic namespace resolution.

Views that access models require to be fully resolved.

rake <engine>:install:migrations

rake railties:install:migrations

Controllers permit parameters

No hardcoded associations from top-level app.

Page 89: Ruby and rails - Advanced Training (Cybage)

Discussions!

ActiveResource

Rails Security

Caching

Performance

Q & A

Page 90: Ruby and rails - Advanced Training (Cybage)

ActiveResource

Page 91: Ruby and rails - Advanced Training (Cybage)

Rails Security Authenticity Token

Protect from forgery

CSRF

SQL injection

XSS - html_safe & raw

http://guides.rubyonrails.org/security.html

Page 92: Ruby and rails - Advanced Training (Cybage)

Rails CachingCache Store:

memory, file, memcached, ehcache, custom

Page & Action caching (DEPRECATED)

http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works

Fragment caching

Query caching

http://guides.rubyonrails.org/caching_with_rails.html

Page 93: Ruby and rails - Advanced Training (Cybage)

Rails PerformanceNewRelic Monitoring

Log file and debugging

Load Testing

Concurrency testing

Performance Benchmark using benchmark-ips

http://guides.rubyonrails.org/v3.2.13/performance_testing.html