Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Rails OO views

  1. 1. Loading...
  2. 2. Markdown vs. MongoDB A Journey through bleeding edge technologies for the NeXT Web
  3. 3. RubyMotion vs. Backbone A Journey through bleeding edge technologies for the NeXT Web
  4. 4. Rails Views for the OO-savvy Rubyist
  5. 5. M V C
  6. 6. V C
  7. 7. Look at: Rails current implementation
  8. 8. def show @post = Post.find(params[:id]) @title = @post.title end
  9. 9. <h1><%= @post.title %></h1> <p><%= @post.body %></p>
  10. 10. <html> <head><title><%= @title %></title></head> <body><%= yield %></body> </html>
  11. 11. And tell me boy, what‘s wrong with my @ivars My name‘s Guybrush threepwood. I‘m new in town Matz told me their only for Object‘s internal state Who‘s in charge here? Nice talking to you.
  12. 12. So how should I use @ivars?
  13. 13. When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class).
  14. 14. The internal state is held in instance variables. The external state is exposed through methods we‘re call- ing attributes.
  15. 15. Dave Thomas, Programming Ruby
  16. 16. Deep in the Rails source...
  17. 17. # Define some internal variables that # should not be propagated to the view. self.protected_instance_variables = [ :@_status, :@_headers, :@_params, :@_env, :@_response, :@_request, :@_view_runtime, :@_stream, :@_url_options, :@_action_has_layout ]
  18. 18. # Define some internal variables that # should not be propagated to the view. self.protected_instance_variables = [ :@_status, :@_headers, :@_params, :@_env, :@_response, :@_request, :@_view_runtime, :@_stream, :@_url_options, :@_action_has_layout ]
  19. 19. protected instance variables
  20. 20. protected - instance - variables
  21. 21. And tell me boy, what ya doing about this? I‘ll stick with "helper_method". Give me some "decent_exposure"? It‘s "obviews"! I‘m made of rubber, you‘re made of glue.
  22. 22. helper_method pros: rails standard cons: verbose, bad semantics, class level
  23. 23. helper_method pros: rails standard cons: verbose, bad semantics, class level
  24. 24. helper_method pros: rails standard cons: verbose, bad semantics, class level
  25. 25. decent_exposure pros: easy syntax, good defaults cons: class level
  26. 26. decent_exposure pros: easy syntax, good defaults cons: class level
  27. 27. decent_exposure pros: easy syntax, good defaults cons: class level
  28. 28. obviews pros: easy syntax, action level cons: unpublished
  29. 29. obviews pros: easy syntax, action level cons: unpublished
  30. 30. obviews pros: easy syntax, action level cons: unpublished!
  31. 31. We want examples!
  32. 32. We want examples! class PostsController < ApplicationController expose :posts def posts; Post.all; end end
  33. 33. We want examples! def show expose post end def post; posts.find(params[:id]) end
  34. 34. We want examples! def show expose post: posts.find(params[:id]) end
  35. 35. We want examples! <h1><%= pos title %></h1> t. <p><%= post.body %></p>
  36. 36. Obviews
  37. 37. Obviews on rubygems shipping today!
  38. 38. Thank you !1
  39. 39. twitter: @elia github: @elia blog: dev.mikamai.com blog: elia.schito.me MIKAMAI No monkeys were harmed during the making of this keynote
  40. 40. twitter: @elia github: @elia blog: dev.mikamai.com blog: elia.schito.me MIKAMAI No monkeys were harmed during the making of this keynote

×