decent_exposure

      @mattyoho
  yoho@hashrocket.com
Hashrocket Chicago is hiring.
Join us.
@voxdolo/Stephen Caudill
http://github.com/voxdolo/decent_exposure
idiom
finder calls everywhere
instance variables in views
@yuck
gem install decent_exposure
rspec’s let
context "validation" do
 let(:post) { Factory.build(:post) }
 it "requires a poster" do
   post.poster = nil
   post.shoul...
resource_controller abuse
class FooController < AppController

 def show
  @foo = Foo.find(params[:id])
 end
end
class FooController < AppController
 expose :foo

 def show
 end
end
Foo.find(params[:foo_id] || params[:id])
class FooController < AppController
 expose(:foo) { user.foos.first }

 def show
 end
end
memoization
memorization
memoization
def widget
 @widget ||= Widget.find(123)
end
hide_action/helper_method
custom default
class MyFooController < AppController
 default_exposure do
   Foo.for_user(current_user)find
 end
end
the code
module DecentExposure
 def inherited(klass)
  closured_exposure = default_exposure
  klass.class_eval do
   default_exposu...
attr_accessor :_default_exposure

def default_exposure(&block)
 self._default_exposure = block if block_given?
 _default_e...
def inherited(klass)
 closured_exposure = default_exposure
 klass.class_eval do
  default_exposure(&closured_exposure)
 en...
def expose(name, &block)
 closured_exposure = default_exposure
 define_method name do
   @_resources     ||= {}
   @_resour...
instance_eval/instance_exec
foo.instance_eval(&block)
self = foo
Trivia!
views
app/views/posts/post.html.erb



<p>
 <b>Poster:</b>
 <%= post.poster_id %>
</p>

<p>
 <b>Title:</b>
 <%=h post.title %>
<...
class PostsController < AppController
 expose(:post) { Post.first }

 def show
  render :template => ‘posts/post’
 end
end
app/views/posts/post.html.erb



<p>
 <b>Poster:</b>
 <%= post.poster_id %>
</p>

<p>
 <b>Title:</b>
 <%=h post.title %>
<...
app/views/posts/post.html.erb



<p>
 <b>Poster:</b>
 <%= post.poster_id %>
</p>

<p>
 <b>Title:</b>
 <%=h post.title %>
<...
app/views/posts/post.html.erb



<p>
 <b>Poster:</b>
 <%= post.poster_id %>
</p>

<p>
 <b>Title:</b>
 <%=h post.title %>
<...
What about exceptions?
class PostsController < AppController

 expose(:post) do
  Foo.find(params[:id])
 end
end
class PostsController < AppController

 expose(:post) do
  Foo.find_by_id(params[:id])
 end
end
class PostsController < AppController

 expose(:post) do
  begin
  rescue => e
  end
 end
end
rescue_from ActiveRecord::RecordNotFound, :with => :omg_wtf
working with Rails
ActionController::Base.class_eval do
 extend DecentExposure
 superclass_delegating_accessor :_default_exposure
 default_ex...
testing
assigns[:posts]
assigns[:posts]
testing
integration testing
Questions?




 yoho@hashrocket.com
GLRB - Decent exposure
Upcoming SlideShare
Loading in...5
×

GLRB - Decent exposure

1,957

Published on

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,957
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

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&amp;#x2019;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&amp;#x2019;s a neat approach.
  • GLRB - Decent exposure

    1. 1. decent_exposure @mattyoho yoho@hashrocket.com
    2. 2. Hashrocket Chicago is hiring.
    3. 3. Join us.
    4. 4. @voxdolo/Stephen Caudill
    5. 5. http://github.com/voxdolo/decent_exposure
    6. 6. idiom
    7. 7. finder calls everywhere
    8. 8. instance variables in views
    9. 9. @yuck
    10. 10. gem install decent_exposure
    11. 11. rspec’s let
    12. 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. 13. resource_controller abuse
    14. 14. class FooController < AppController def show @foo = Foo.find(params[:id]) end end
    15. 15. class FooController < AppController expose :foo def show end end
    16. 16. Foo.find(params[:foo_id] || params[:id])
    17. 17. class FooController < AppController expose(:foo) { user.foos.first } def show end end
    18. 18. memoization
    19. 19. memorization
    20. 20. memoization
    21. 21. def widget @widget ||= Widget.find(123) end
    22. 22. hide_action/helper_method
    23. 23. custom default
    24. 24. class MyFooController < AppController default_exposure do Foo.for_user(current_user)find end end
    25. 25. the code
    26. 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. 27. attr_accessor :_default_exposure def default_exposure(&block) self._default_exposure = block if block_given? _default_exposure end
    28. 28. def inherited(klass) closured_exposure = default_exposure klass.class_eval do default_exposure(&closured_exposure) end super end
    29. 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. 30. instance_eval/instance_exec
    31. 31. foo.instance_eval(&block)
    32. 32. self = foo
    33. 33. Trivia!
    34. 34. views
    35. 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. 36. class PostsController < AppController expose(:post) { Post.first } def show render :template => ‘posts/post’ end end
    37. 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. 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. 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. 40. What about exceptions?
    41. 41. class PostsController < AppController expose(:post) do Foo.find(params[:id]) end end
    42. 42. class PostsController < AppController expose(:post) do Foo.find_by_id(params[:id]) end end
    43. 43. class PostsController < AppController expose(:post) do begin rescue => e end end end
    44. 44. rescue_from ActiveRecord::RecordNotFound, :with => :omg_wtf
    45. 45. working with Rails
    46. 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. 47. testing
    48. 48. assigns[:posts]
    49. 49. assigns[:posts]
    50. 50. testing
    51. 51. integration testing
    52. 52. Questions? yoho@hashrocket.com

    ×