Ruby and Rails
Advanced Training
@gautamrege
@joshsoftware
Agenda - Day 1
Discuss Ruby basics
Syntax, classes, modules.
Meta-programming.
Closures
Rails concepts
Discussion around R...
Agenda - Day 2
Discuss your current Applications.
Gems currently in use
Resolving problems.
ActiveResource
Rails Security
...
Ruby is easy, right?
Ruby is easy, right?
1 + 1
Ruby is easy, right?
1 + 1
1.+(1)
Ruby is easy, right?
1 + 1
1.+(1)
1.+ 1
Ruby is easy, right?
1 + 1
1.+(1)
1.+ 1
1.send(:+, 1)
How many objects?
How many objects?
1 + 2 + 3
How many objects?
1 + 2 + 3
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
How many objects?
1 + 2 + 3
( 1.+(2) ).+(3)
FIVE
objects
Everything in Ruby
is an object
on which we call a method
to which we pass parameters
and an optional block of code
So, you know Ruby?
So, you know Ruby?
a[1] # What is the data type of a?
So, you know Ruby?
a[1] # What is the data type of a?
a = [‘a’, ‘b’] # Array
So, you know Ruby?
a[1] # What is the data type of a?
a = [‘a’, ‘b’] # Array
a = { 1 => ‘a’} # Hash
So, you know Ruby?
a[1] # What is the data type of a?
a = [‘a’, ‘b’] # Array
a = { 1 => ‘a’} # Hash
a = “abc” # String
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 =...
Multiplication
Multiplication
[1, 2, 3] * 2
Multiplication
[1, 2, 3] * 2
# => [1, 2, 3, 1, 2, 3]
Multiplication
[1, 2, 3] * 2
# => [1, 2, 3, 1, 2, 3]
[1, 2, 3] * “%”
Multiplication
[1, 2, 3] * 2
# => [1, 2, 3, 1, 2, 3]
[1, 2, 3] * “%”
# => “1%2%3”
There’s always an
easier way
Summation of [1, 2, 3, 4]
Non-ruby way!
sum = 0
!
for i in [1, 2, 3, 4]
sum += i
end
!
p sum
The Amateur!
sum = 0
!
[1, 2, 3, 4].each do |i|
sum += i
end
!
p sum
The Professional!
!
[1, 2, 3, 4].inject(0) do |sum, i|
sum + i
end
The Expert!
!
[1, 2, 3, 4].inject(:+)
Exceptions… never!
class Base
def method_missing(name, *args, &blk)
puts “Unknown method #{name}
called.”
end
end
b = Base...
What’s in a name?
# User(id: integer, name: string)
class User < ActiveRecord::Base
end
u = User.name
u.name
u.name = “Gau...
Accessing Data
class User
def initialize
@name = “someone”
end
end u = User.name
u.name
u.name = “Gautam”
Protected & Private
Private methods are inherited
Protected method can be called on
another object in same lineage
What th...
Module Mixins
class Shaktiman
include Spiderman
include Superman
end
irb> Shaktiman.ancestors
=> [ Shaktiman, Superman, Sp...
Closures
sum = 0
[1, 2, 3, 4].each do |x|
sum += i
end
p sum
Anonymous functions?
Local variables scope?
Closures
!
Context sensitive block of code
Rails Design
Patterns
Rails Design
Patterns
Singleton
Rails Design
Patterns
Singleton
Factory
Rails Design
Patterns
Singleton
Factory
Observer
Rails Design
Patterns
Singleton
Factory
Observer
…
Rails Design
Patterns
Singleton
Factory
Observer
…
You cannot learn
Design Patterns!
!
You need to experience
them
SOLID Design
Patterns
SOLID principles
S - Single Responsibilty
O - Open / Closed
L - Liskov Substituion
I - Interface Segregation
D - Dependenc...
Single
Responsibility
Serves only one purpose.
Models - manage data
Controllers - manage logic
No logic in views!
Open / Closed
Open for Extension
Closed for modification
Open / Closed
Open for Extension
Closed for modification
ParseFeed
Open / Closed
Open for Extension
Closed for modification
ParseFeed
Atom RSS
ParseFeed
Liskov Substituion
Functionality should work for all
derived classes.
Let q(x) be a property provable about
objects x of t...
Interface
Segregation
Classification based on behaviour
Vehicle
AirRoad
TrainCar RocketAirplane
Dependency
Inversion
High level modules should not
depend on low level modules
Modules should depend on
abstractions
Detai...
ActiveSupport
Concern
Without ActiveSupport
Concerns
module Foo
def self.included(base)
base.class_eval do
scope :disable, -> { where(disabled: ...
With ActiveSupport
Concerns
module Foo
extend ActiveSupport::Concern
included do
scope :disable, -> { where(disabled: true...
Dependent Modules
Dependent Modules
module Foo
def self.included(base)
base.class_eval do
def self.foo_method
end
end
end
end
Dependent Modules
module Foo
def self.included(base)
base.class_eval do
def self.foo_method
end
end
end
end
module Bar
def...
Dependent Modules
module Foo
def self.included(base)
base.class_eval do
def self.foo_method
end
end
end
end
module Bar
def...
Dependent Modules
module Foo
def self.included(base)
base.class_eval do
def self.foo_method
end
end
end
end
module Bar
def...
Module
dependencies
Module
dependencies
module Bar
include Foo
included do
def self.foo_method
end
end
class Base
include Bar
end
Module
dependencies
module Bar
include Foo
included do
def self.foo_method
end
end
class Base
include Bar
end
self = Bar
s...
Module
dependencies
module Bar
include Foo
included do
def self.foo_method
end
end
class Base
include Bar
end
self = Bar
s...
Module
dependencies
module Bar
extend ActiveSupport::Concern
include Foo
included do
def self.foo_method
end
end
class Bas...
Rails Engines
Engine power
Velocity App
CarRocket
Some custom
Functionality
CarRocket
Some custom
functionality
CarRocket
Some custom
functionality
Astronaut
Shuttles
M
M
CarRocket
Some custom
functionality
Astronaut
Shuttles
M
M
Driver
Rto
M
1
CarRocket
Some custom
functionality
Astronaut
Shuttles
M
M
Driver
Rto
M
1
Company
Quick Reference
rails plugin new <name> —mountable
rails g scaffold <model> <name>:<type>
test/dummy: testing engine.
GamePlay - Car
Create Car engine
Create RTO scaffold
name:string code:string
Create Driver scaffold
name:string rto_id:int...
Testing - Car
rake db:migrate
cd test/dummy
rails s
http://localhost:300/car
GamePlay-Rocket
Create Rocket engine
Create Astronaut scaffold
name:string degree:text
Create Shuttle scaffold
name:string...
Testing - Rocket
rake db:migrate
cd test/dummy
rails s
http://localhost:3000/rocket
Application Stuff
rails new velocity
update Gemfile
gem ‘car’, path: <path>
gem ‘rocket’, path: <path>
config/routes:
moun...
Application Logic
Company scaffold
name:string make_cars:boolean
make_rockets:boolean
owner:string
Using Application
models in engines.
Add migration for owner_id
class Shuttle
belongs_to :owner, class: “Company”
end
NEVE...
More ???
Devise
Authentication
Gemfile
gem ‘devise’
rails g devise:install
rails g device user
rake db:migrate
Inherit from App
# rocket/app/controller/rocket/application_controller.rb
class Rocket::ApplicationController <
Applicatio...
Checklist
Automatic namespace resolution.
Views that access models require to be fully
resolved.
rake <engine>:install:mig...
Discussions!
ActiveResource
Rails Security
Caching
Performance
Q & A
ActiveResource
Rails Security
Authenticity Token
Protect from forgery
CSRF
SQL injection
XSS - html_safe & raw
http://guides.rubyonrails....
Rails Caching
Cache Store:
memory, file, memcached, ehcache, custom
Page & Action caching (DEPRECATED)
http://signalvnoise...
Rails Performance
NewRelic Monitoring
Log file and debugging
Load Testing
Concurrency testing
Performance Benchmark using ...
Upcoming SlideShare
Loading in …5
×

Ruby and rails - Advanced Training (Cybage)

503
-1

Published on

Cybage Advanced Training - customised

Published in: Software
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
503
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Ruby and rails - Advanced Training (Cybage)

  1. 1. Ruby and Rails Advanced Training @gautamrege @joshsoftware
  2. 2. Agenda - Day 1 Discuss Ruby basics Syntax, classes, modules. Meta-programming. Closures Rails concepts Discussion around Rails design patterns. ActiveSupport Concern Rails engines
  3. 3. Agenda - Day 2 Discuss your current Applications. Gems currently in use Resolving problems. ActiveResource Rails Security Data Caching Performace monitoring and improvement
  4. 4. Ruby is easy, right?
  5. 5. Ruby is easy, right? 1 + 1
  6. 6. Ruby is easy, right? 1 + 1 1.+(1)
  7. 7. Ruby is easy, right? 1 + 1 1.+(1) 1.+ 1
  8. 8. Ruby is easy, right? 1 + 1 1.+(1) 1.+ 1 1.send(:+, 1)
  9. 9. How many objects?
  10. 10. How many objects? 1 + 2 + 3
  11. 11. How many objects? 1 + 2 + 3
  12. 12. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  13. 13. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  14. 14. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  15. 15. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  16. 16. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  17. 17. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3)
  18. 18. How many objects? 1 + 2 + 3 ( 1.+(2) ).+(3) FIVE objects
  19. 19. Everything in Ruby is an object on which we call a method to which we pass parameters and an optional block of code
  20. 20. So, you know Ruby?
  21. 21. So, you know Ruby? a[1] # What is the data type of a?
  22. 22. So, you know Ruby? a[1] # What is the data type of a? a = [‘a’, ‘b’] # Array
  23. 23. So, you know Ruby? a[1] # What is the data type of a? a = [‘a’, ‘b’] # Array a = { 1 => ‘a’} # Hash
  24. 24. So, you know Ruby? a[1] # What is the data type of a? a = [‘a’, ‘b’] # Array a = { 1 => ‘a’} # Hash a = “abc” # String
  25. 25. 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
  26. 26. Multiplication
  27. 27. Multiplication [1, 2, 3] * 2
  28. 28. Multiplication [1, 2, 3] * 2 # => [1, 2, 3, 1, 2, 3]
  29. 29. Multiplication [1, 2, 3] * 2 # => [1, 2, 3, 1, 2, 3] [1, 2, 3] * “%”
  30. 30. Multiplication [1, 2, 3] * 2 # => [1, 2, 3, 1, 2, 3] [1, 2, 3] * “%” # => “1%2%3”
  31. 31. There’s always an easier way Summation of [1, 2, 3, 4]
  32. 32. Non-ruby way! sum = 0 ! for i in [1, 2, 3, 4] sum += i end ! p sum
  33. 33. The Amateur! sum = 0 ! [1, 2, 3, 4].each do |i| sum += i end ! p sum
  34. 34. The Professional! ! [1, 2, 3, 4].inject(0) do |sum, i| sum + i end
  35. 35. The Expert! ! [1, 2, 3, 4].inject(:+)
  36. 36. Exceptions… never! class Base def method_missing(name, *args, &blk) puts “Unknown method #{name} called.” end end b = Base.new b.wtf
  37. 37. What’s in a name? # User(id: integer, name: string) class User < ActiveRecord::Base end u = User.name u.name u.name = “Gautam”
  38. 38. Accessing Data class User def initialize @name = “someone” end end u = User.name u.name u.name = “Gautam”
  39. 39. Protected & Private Private methods are inherited Protected method can be called on another object in same lineage What the …
  40. 40. Module Mixins class Shaktiman include Spiderman include Superman end irb> Shaktiman.ancestors => [ Shaktiman, Superman, Spiderman …]
  41. 41. Closures sum = 0 [1, 2, 3, 4].each do |x| sum += i end p sum Anonymous functions? Local variables scope?
  42. 42. Closures ! Context sensitive block of code
  43. 43. Rails Design Patterns
  44. 44. Rails Design Patterns Singleton
  45. 45. Rails Design Patterns Singleton Factory
  46. 46. Rails Design Patterns Singleton Factory Observer
  47. 47. Rails Design Patterns Singleton Factory Observer …
  48. 48. Rails Design Patterns Singleton Factory Observer … You cannot learn Design Patterns! ! You need to experience them
  49. 49. SOLID Design Patterns
  50. 50. SOLID principles S - Single Responsibilty O - Open / Closed L - Liskov Substituion I - Interface Segregation D - Dependency Inversion
  51. 51. Single Responsibility Serves only one purpose. Models - manage data Controllers - manage logic No logic in views!
  52. 52. Open / Closed Open for Extension Closed for modification
  53. 53. Open / Closed Open for Extension Closed for modification ParseFeed
  54. 54. Open / Closed Open for Extension Closed for modification ParseFeed Atom RSS ParseFeed
  55. 55. 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
  56. 56. Interface Segregation Classification based on behaviour Vehicle AirRoad TrainCar RocketAirplane
  57. 57. Dependency Inversion High level modules should not depend on low level modules Modules should depend on abstractions Details should depend on abstractions
  58. 58. ActiveSupport Concern
  59. 59. 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
  60. 60. With ActiveSupport Concerns module Foo extend ActiveSupport::Concern included do scope :disable, -> { where(disabled: true) } end end class Base include Foo end
  61. 61. Dependent Modules
  62. 62. Dependent Modules module Foo def self.included(base) base.class_eval do def self.foo_method end end end end
  63. 63. Dependent Modules module 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
  64. 64. Dependent Modules module 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
  65. 65. Dependent Modules module 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 #!!
  66. 66. Module dependencies
  67. 67. Module dependencies module Bar include Foo included do def self.foo_method end end class Base include Bar end
  68. 68. Module dependencies module Bar include Foo included do def self.foo_method end end class Base include Bar end self = Bar self != Base
  69. 69. Module dependencies module Bar include Foo included do def self.foo_method end end class Base include Bar end self = Bar self != Base
  70. 70. Module dependencies module Bar extend ActiveSupport::Concern include Foo included do def self.foo_method end end class Base include Bar end no including extra modues self = Base
  71. 71. Rails Engines
  72. 72. Engine power Velocity App CarRocket Some custom Functionality
  73. 73. CarRocket Some custom functionality
  74. 74. CarRocket Some custom functionality Astronaut Shuttles M M
  75. 75. CarRocket Some custom functionality Astronaut Shuttles M M Driver Rto M 1
  76. 76. CarRocket Some custom functionality Astronaut Shuttles M M Driver Rto M 1 Company
  77. 77. Quick Reference rails plugin new <name> —mountable rails g scaffold <model> <name>:<type> test/dummy: testing engine.
  78. 78. GamePlay - Car Create Car engine Create RTO scaffold name:string code:string Create Driver scaffold name:string rto_id:integer has_licence:boolean
  79. 79. Testing - Car rake db:migrate cd test/dummy rails s http://localhost:300/car
  80. 80. GamePlay-Rocket Create 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
  81. 81. Testing - Rocket rake db:migrate cd test/dummy rails s http://localhost:3000/rocket
  82. 82. Application Stuff rails new velocity update Gemfile gem ‘car’, path: <path> gem ‘rocket’, path: <path> config/routes: mount Car::Engine, at: ‘/car’ mount Rocket::Engine, at: ‘/engine’
  83. 83. Application Logic Company scaffold name:string make_cars:boolean make_rockets:boolean owner:string
  84. 84. 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.
  85. 85. More ???
  86. 86. Devise Authentication Gemfile gem ‘devise’ rails g devise:install rails g device user rake db:migrate
  87. 87. 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
  88. 88. Checklist Automatic 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.
  89. 89. Discussions! ActiveResource Rails Security Caching Performance Q & A
  90. 90. ActiveResource
  91. 91. Rails Security Authenticity Token Protect from forgery CSRF SQL injection XSS - html_safe & raw http://guides.rubyonrails.org/security.html
  92. 92. Rails Caching Cache 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
  93. 93. Rails Performance NewRelic Monitoring Log file and debugging Load Testing Concurrency testing Performance Benchmark using benchmark-ips http://guides.rubyonrails.org/v3.2.13/ performance_testing.html
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×