Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails is Just Ruby

Rails is Just Ruby

RailsConf 2013 - Intro Track


Jesse Wolgamott

April 29, 2013

More Decks by Jesse Wolgamott

Other Decks in Programming


  1. Ruby is Just Rails A Guide to Procrastination and Happiness

  2. Stu Rails makes insanely easy Seemingly Magical

  3. Protect entire system from unauthorized requests Seemingly Magical

  4. Log every page load Seemingly Magical

  5. Update full-text search engine every time a record is added

    Seemingly Magical
  6. it’s not magic Magic It’s not magic (maybe at first)

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

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

  9. Wizard Like Powers Business People consider us wizards with our

    ability to create business value
  10. Rails Helps us Along Rails helps us along

  11. Rails Helps us Along Blocks

  12. Rails Helps us Along Before Filters

  13. Rails Helps us Along Callbacks

  14. Rails Helps us Along Inheritance

  15. Rails Helps us Along It all starts with a block

  16. The Block

  17. 10.times do puts "YOLO" end

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

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

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

  22. Rails Helps us Along code that gets delayed and executed

  23. Block Rules Every Ruby method can receive a block

  24. Block Rules The method has to yield to the block

    to be e ective
  25. Block Rules Can take parameters and won’t scream if you

    send in too few
  26. args.each do |key,value| end

  27. Advanced Blocks Advanced Block Tips

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

  29. Block Rules Use Ruby’s implicit returns instead

  30. Advanced Blocks Add retries for volatile actions

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

  32. Before Filters

  33. Before Filters Used in Controllers

  34. Before Filters Rails actions are your interactions with users (get/post

  35. Before Filters Can be before or after

  36. Before Filters If your before filter returns, or redirects the

    action won’t happen
  37. before_filter do |controller| unless controller.send(:logged_in?) redirect_to new_login_url end end

  38. class ApplicationController before_filter :authenticate_user def authenticate_user unless logged_in? redirect_to new_login_url

    end end end
  39. class PostsController < AppController # already secured end

  40. class PublicPostsController < AppController skip_before_filter :authenticate_user end

  41. Callbacks

  42. About Callbacks Code you can define to run later works

    in Models too
  43. About Callbacks It’s like HollaBack, but not

  44. Callback Example Stack Overflow Example: Downcasing email

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

    end end
  46. Callback Example Real World Example: Full Text Searching (websolr)

  47. class Post < ActiveRecord::Base searchable do text :title, :body end

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

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

  50. Workshop Time

  51. Workshop Create a beeper alert program

  52. Workshop Criteria if alarm is critical, send SMS

  53. Workshop Criteria if alarm is warning, send email

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