Your SlideShare is downloading. ×
GLRB - Decent exposure
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

GLRB - Decent exposure

1,923
views

Published on

Brief introduction to the Rails idiom enabled by the decent_explosure gem.

Brief introduction to the Rails idiom enabled by the decent_explosure gem.

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,923
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Version of a longer and likely much more profane talk at the Great Lakes Ruby Bash on April 17th.

    Pretty simple but I think it’s a neat approach.
  • Rails plugin that represents an idiom
  • Rails plugin that represents an idiom
  • Rails plugin that represents an idiom

  • motivations
  • motivations


  • how to make that a bit better
  • inspirations
  • inspirations
  • As well as abusing the hell out of resource_controller


  • can be overridden by default_exposure




































  • Version of a longer and likely much more profane talk at the Great Lakes Ruby Bash on April 17th.

    Pretty simple but I think it’s a neat approach.
  • Transcript

    • 1. decent_exposure @mattyoho yoho@hashrocket.com
    • 2. Hashrocket Chicago is hiring.
    • 3. Join us.
    • 4. @voxdolo/Stephen Caudill
    • 5. http://github.com/voxdolo/decent_exposure
    • 6. idiom
    • 7. finder calls everywhere
    • 8. instance variables in views
    • 9. @yuck
    • 10. gem install decent_exposure
    • 11. rspec’s let
    • 12. context "validation" do let(:post) { Factory.build(:post) } it "requires a poster" do post.poster = nil post.should_not be_valid post.errors.on(:poster).should be_present end end
    • 13. resource_controller abuse
    • 14. class FooController < AppController def show @foo = Foo.find(params[:id]) end end
    • 15. class FooController < AppController expose :foo def show end end
    • 16. Foo.find(params[:foo_id] || params[:id])
    • 17. class FooController < AppController expose(:foo) { user.foos.first } def show end end
    • 18. memoization
    • 19. memorization
    • 20. memoization
    • 21. def widget @widget ||= Widget.find(123) end
    • 22. hide_action/helper_method
    • 23. custom default
    • 24. class MyFooController < AppController default_exposure do Foo.for_user(current_user)find end end
    • 25. the code
    • 26. module DecentExposure def inherited(klass) closured_exposure = default_exposure klass.class_eval do default_exposure(&closured_exposure) end super end attr_accessor :_default_exposure def default_exposure(&block) self._default_exposure = block if block_given? _default_exposure end def expose(name, &block) closured_exposure = default_exposure define_method name do @_resources ||= {} @_resources[name] ||= if block_given? instance_eval(&block) else instance_exec(name, &closured_exposure) end end helper_method name hide_action name end alias let expose end
    • 27. attr_accessor :_default_exposure def default_exposure(&block) self._default_exposure = block if block_given? _default_exposure end
    • 28. def inherited(klass) closured_exposure = default_exposure klass.class_eval do default_exposure(&closured_exposure) end super end
    • 29. def expose(name, &block) closured_exposure = default_exposure define_method name do @_resources ||= {} @_resources[name] ||= if block_given? instance_eval(&block) else instance_exec(name, &closured_exposure) end end helper_method name hide_action name end alias let expose
    • 30. instance_eval/instance_exec
    • 31. foo.instance_eval(&block)
    • 32. self = foo
    • 33. Trivia!
    • 34. views
    • 35. app/views/posts/post.html.erb <p> <b>Poster:</b> <%= post.poster_id %> </p> <p> <b>Title:</b> <%=h post.title %> </p> <p> <b>Origin:</b> <%=h post.origin %> </p> <p> <b>Message:</b> <%=h post.message %> </p>
    • 36. class PostsController < AppController expose(:post) { Post.first } def show render :template => ‘posts/post’ end end
    • 37. app/views/posts/post.html.erb <p> <b>Poster:</b> <%= post.poster_id %> </p> <p> <b>Title:</b> <%=h post.title %> </p> <p> <b>Origin:</b> <%=h post.origin %> </p> <p> <b>Message:</b> <%=h post.message %> </p>
    • 38. app/views/posts/post.html.erb <p> <b>Poster:</b> <%= post.poster_id %> </p> <p> <b>Title:</b> <%=h post.title %> </p> helper_method <p> <b>Origin:</b> <%=h post.origin %> </p> <p> <b>Message:</b> <%=h post.message %> </p>
    • 39. app/views/posts/post.html.erb <p> <b>Poster:</b> <%= post.poster_id %> </p> <p> <b>Title:</b> <%=h post.title %> </p> How many block invocations? <p> <b>Origin:</b> <%=h post.origin %> </p> <p> <b>Message:</b> <%=h post.message %> </p>
    • 40. What about exceptions?
    • 41. class PostsController < AppController expose(:post) do Foo.find(params[:id]) end end
    • 42. class PostsController < AppController expose(:post) do Foo.find_by_id(params[:id]) end end
    • 43. class PostsController < AppController expose(:post) do begin rescue => e end end end
    • 44. rescue_from ActiveRecord::RecordNotFound, :with => :omg_wtf
    • 45. working with Rails
    • 46. ActionController::Base.class_eval do extend DecentExposure superclass_delegating_accessor :_default_exposure default_exposure do |name| model_class = name.to_s.classify.constantize model_class.find(params["#{name}_id"] || params['id']) end end
    • 47. testing
    • 48. assigns[:posts]
    • 49. assigns[:posts]
    • 50. testing
    • 51. integration testing
    • 52. Questions? yoho@hashrocket.com