Decent exposure: Controladores sin @ivars

770 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
770
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Decent exposure: Controladores sin @ivars

    1. 1. decent_exposure Controladores sin @ivars Leo Soto M.
    2. 2. Controladores sin @ivars
    3. 3. Controladores sin @ivars ¿Por qué?
    4. 4. @ivars == estado dentro de una clase
    5. 5. @ivars en controladores == HACK
    6. 6. Problemas prácticos de los controladores
    7. 7. 1. Duplicación
    8. 8. class PostsController def index @posts = Post.all end def create @post = Post.new(params[:post]) @post.save end def new @post = Post.new end
    9. 9. def show @post = Post.find(params[:id]) end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) @post.update_attributes(params[:post]) end def destroy @post = Post.find(params[:id]) @post.destroy endend
    10. 10. def show @post = Post.find(params[:id]) end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) @post.update_attributes(params[:post]) end def destroy @post = Post.find(params[:id]) @post.destroy endend
    11. 11. 2. ¿Qué variables puedo usar?
    12. 12. controlador.rbdef foo @x = ... @y = ... @z = ...enddef bar @z = ...enddef baz @x = ... @z = ...end
    13. 13. foo.html.erb...<% render :partial => “some_partial” %>...
    14. 14. baz.html.erb...<% render :partial => “some_partial” %>...
    15. 15. _some_partial.html.erb...<div id=“BOOM”> <%= @y.to_s %></div>...
    16. 16. ¿Soluciones?
    17. 17. controlador.rbbefore_filter :load_ydef load_y; @y = ... ; enddef foo @x = ... @z = ...enddef bar @z = ...enddef baz @x = ... @z = ...end
    18. 18. controlador.rbhelper_method :ydef y; @y ||= ... ; enddef foo @x = ... @z = ...enddef bar @z = ...enddef baz @x = ... @z = ...end
    19. 19. Introduciendo decent_exposure
    20. 20. controlador.rbexpose(:y) { ... }def foo @x = ... @z = ...enddef bar @z = ...enddef baz @x = ... @z = ...end
    21. 21. controlador.rbexpose(:y) { ... }expose(:x) { ... }expose(:z) { ... }
    22. 22. Un ejemplo real
    23. 23. class Admin::PromotionsController < AdminController expose(:promotions) { Promotion.all } expose(:promotion) do params[:id] ? Promotion.find(params[:id]) : Promotion.new(params[:promotion]) end def create if promotion.save redirect_to :action => :index else render :new end endend
    24. 24. class Admin::PromotionsController < AdminController expose(:promotions) { Promotion.all } expose(:promotion) def create if promotion.save redirect_to :action => :index else render :new end endend
    25. 25. https://github.com/voxdolo/decent_exposure

    ×