Slide 1

Slide 1 text

Ruby is Just Rails A Guide to Procrastination and Happiness

Slide 2

Slide 2 text

Stu Rails makes insanely easy Seemingly Magical

Slide 3

Slide 3 text

Protect entire system from unauthorized requests Seemingly Magical

Slide 4

Slide 4 text

Log every page load Seemingly Magical

Slide 5

Slide 5 text

Update full-text search engine every time a record is added Seemingly Magical

Slide 6

Slide 6 text

it’s not magic Magic It’s not magic (maybe at first)

Slide 7

Slide 7 text

http:/ /lotr.wikia.com/wiki/File:Gandalf-2.jpg

Slide 8

Slide 8 text

http:/ /www.ibeta.eu/blog/darth-vader-on-tedxeutropolis/

Slide 9

Slide 9 text

Wizard Like Powers Business People consider us wizards with our ability to create business value

Slide 10

Slide 10 text

Rails Helps us Along Rails helps us along

Slide 11

Slide 11 text

Rails Helps us Along Blocks

Slide 12

Slide 12 text

Rails Helps us Along Before Filters

Slide 13

Slide 13 text

Rails Helps us Along Callbacks

Slide 14

Slide 14 text

Rails Helps us Along Inheritance

Slide 15

Slide 15 text

Rails Helps us Along It all starts with a block

Slide 16

Slide 16 text

The Block

Slide 17

Slide 17 text

10.times do puts "YOLO" end

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

@users.each do |user| <%= user.email %> end

Slide 20

Slide 20 text

User.create do |u| u.name = 'jwo' end

Slide 21

Slide 21 text

@users.map {|u| u.total_due }.sum

Slide 22

Slide 22 text

Rails Helps us Along code that gets delayed and executed later

Slide 23

Slide 23 text

Block Rules Every Ruby method can receive a block

Slide 24

Slide 24 text

Block Rules The method has to yield to the block to be e ective

Slide 25

Slide 25 text

Block Rules Can take parameters and won’t scream if you send in too few

Slide 26

Slide 26 text

args.each do |key,value| end

Slide 27

Slide 27 text

Advanced Blocks Advanced Block Tips

Slide 28

Slide 28 text

Block Rules Don’t use ‘return’ in a block

Slide 29

Slide 29 text

Block Rules Use Ruby’s implicit returns instead

Slide 30

Slide 30 text

Advanced Blocks Add retries for volatile actions

Slide 31

Slide 31 text

10.times.retry do SomeNetworkCommunicationThingyHere.new end https://github.com/schneems/rrrretry

Slide 32

Slide 32 text

Before Filters

Slide 33

Slide 33 text

Before Filters Used in Controllers

Slide 34

Slide 34 text

Before Filters Rails actions are your interactions with users (get/post data)

Slide 35

Slide 35 text

Before Filters Can be before or after

Slide 36

Slide 36 text

Before Filters If your before filter returns, or redirects the action won’t happen

Slide 37

Slide 37 text

before_filter do |controller| unless controller.send(:logged_in?) redirect_to new_login_url end end

Slide 38

Slide 38 text

class ApplicationController before_filter :authenticate_user def authenticate_user unless logged_in? redirect_to new_login_url end end end

Slide 39

Slide 39 text

class PostsController < AppController # already secured end

Slide 40

Slide 40 text

class PublicPostsController < AppController skip_before_filter :authenticate_user end

Slide 41

Slide 41 text

Callbacks

Slide 42

Slide 42 text

About Callbacks Code you can define to run later works in Models too

Slide 43

Slide 43 text

About Callbacks It’s like HollaBack, but not

Slide 44

Slide 44 text

Callback Example Stack Overflow Example: Downcasing email

Slide 45

Slide 45 text

class User < ActiveRecord::Base before_validation do |user| user.email = user.email.downcase end end

Slide 46

Slide 46 text

Callback Example Real World Example: Full Text Searching (websolr)

Slide 47

Slide 47 text

class Post < ActiveRecord::Base searchable do text :title, :body end end https://github.com/sunspot/sunspot

Slide 48

Slide 48 text

Post.search do fulltext 'best pizza' end https://github.com/sunspot/sunspot

Slide 49

Slide 49 text

after_destroy do |searchable| searchable.remove_from_index end https://github.com/sunspot/sunspot/blob/master/ sunspot_rails/lib/sunspot/rails/searchable.rb

Slide 50

Slide 50 text

Workshop Time

Slide 51

Slide 51 text

Workshop Create a beeper alert program

Slide 52

Slide 52 text

Workshop Criteria if alarm is critical, send SMS

Slide 53

Slide 53 text

Workshop Criteria if alarm is warning, send email

Slide 54

Slide 54 text

I’m JWo Thanks! @jwo Jesse Wolgamott.com