SlideShare ist ein Scribd-Unternehmen logo
1 von 53
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.should_not be_valid
   post.errors.on(:poster).should be_present
 end
end
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_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
attr_accessor :_default_exposure

def default_exposure(&block)
 self._default_exposure = block if block_given?
 _default_exposure
end
def inherited(klass)
 closured_exposure = default_exposure
 klass.class_eval do
  default_exposure(&closured_exposure)
 end
 super
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
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 %>
</p>

<p>
 <b>Origin:</b>
 <%=h post.origin %>
</p>

<p>
 <b>Message:</b>
 <%=h post.message %>
</p>
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 %>
</p>

<p>
 <b>Origin:</b>
 <%=h post.origin %>
</p>

<p>
 <b>Message:</b>
 <%=h post.message %>
</p>
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>
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>
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_exposure do |name|
  model_class = name.to_s.classify.constantize
  model_class.find(params["#{name}_id"] || params['id'])
 end
end
testing
assigns[:posts]
assigns[:posts]
testing
integration testing
Questions?




 yoho@hashrocket.com

Weitere ähnliche Inhalte

Was ist angesagt?

2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
Fabien Potencier
 

Was ist angesagt? (20)

PHP 5.3 in practice
PHP 5.3 in practicePHP 5.3 in practice
PHP 5.3 in practice
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
 
Modularizing Rails Apps with Cells
Modularizing Rails Apps with CellsModularizing Rails Apps with Cells
Modularizing Rails Apps with Cells
 
Como programar un blog REST
Como programar un blog RESTComo programar un blog REST
Como programar un blog REST
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Generators
GeneratorsGenerators
Generators
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
PhpSpec extension points
PhpSpec extension pointsPhpSpec extension points
PhpSpec extension points
 
Chaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscoreChaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscore
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
PhpBB meets Symfony2
PhpBB meets Symfony2PhpBB meets Symfony2
PhpBB meets Symfony2
 
Ecmascript 6
Ecmascript 6Ecmascript 6
Ecmascript 6
 
23.simple login with sessions in laravel 5
23.simple login with sessions in laravel 523.simple login with sessions in laravel 5
23.simple login with sessions in laravel 5
 
Clever Joomla! Templating Tips and Tricks
Clever Joomla! Templating Tips and TricksClever Joomla! Templating Tips and Tricks
Clever Joomla! Templating Tips and Tricks
 
Generating Power with Yield
Generating Power with YieldGenerating Power with Yield
Generating Power with Yield
 
REST in practice with Symfony2
REST in practice with Symfony2REST in practice with Symfony2
REST in practice with Symfony2
 

Andere mochten auch (14)

Xx
XxXx
Xx
 
Language Leaders
Language LeadersLanguage Leaders
Language Leaders
 
Where God Wants Me
Where God Wants MeWhere God Wants Me
Where God Wants Me
 
Simple Past Tense 2 Pps[1]
Simple Past Tense 2 Pps[1]Simple Past Tense 2 Pps[1]
Simple Past Tense 2 Pps[1]
 
SPRINT! single player - wat zijn de spelregels?
SPRINT! single player - wat zijn de spelregels?SPRINT! single player - wat zijn de spelregels?
SPRINT! single player - wat zijn de spelregels?
 
Amozesh
AmozeshAmozesh
Amozesh
 
How to use SPRINT! elearning games for retail banking
How to use SPRINT! elearning games for retail bankingHow to use SPRINT! elearning games for retail banking
How to use SPRINT! elearning games for retail banking
 
Gcse German Edexcel
Gcse German EdexcelGcse German Edexcel
Gcse German Edexcel
 
Writing Portfolio Emily Goulding Aug 09
Writing Portfolio Emily Goulding Aug 09Writing Portfolio Emily Goulding Aug 09
Writing Portfolio Emily Goulding Aug 09
 
Birds
BirdsBirds
Birds
 
Meszaros. Cap 9. Parte 2
Meszaros. Cap 9. Parte 2Meszaros. Cap 9. Parte 2
Meszaros. Cap 9. Parte 2
 
Manifiesto Comunista Ilustrado
Manifiesto Comunista IlustradoManifiesto Comunista Ilustrado
Manifiesto Comunista Ilustrado
 
Gcse German Edexcel2
Gcse German Edexcel2Gcse German Edexcel2
Gcse German Edexcel2
 
Funciones trigonometricas 2008
Funciones trigonometricas 2008Funciones trigonometricas 2008
Funciones trigonometricas 2008
 

Ähnlich wie GLRB - Decent exposure

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Drupal 7 module development
Drupal 7 module developmentDrupal 7 module development
Drupal 7 module development
Adam Kalsey
 
Resource and view
Resource and viewResource and view
Resource and view
Papp Laszlo
 

Ähnlich wie GLRB - Decent exposure (20)

Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Designing Ruby APIs
Designing Ruby APIsDesigning Ruby APIs
Designing Ruby APIs
 
What's new in Rails 4
What's new in Rails 4What's new in Rails 4
What's new in Rails 4
 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2
 
AnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFacesAnkaraJUG Kasım 2012 - PrimeFaces
AnkaraJUG Kasım 2012 - PrimeFaces
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick Sutterer
 
Drupal 7 module development
Drupal 7 module developmentDrupal 7 module development
Drupal 7 module development
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Resource and view
Resource and viewResource and view
Resource and view
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack Support
 
New in php 7
New in php 7New in php 7
New in php 7
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
Ruby meetup-dry
Ruby meetup-dryRuby meetup-dry
Ruby meetup-dry
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

GLRB - Decent exposure

Hinweis der Redaktion

  1. 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.
  2. Rails plugin that represents an idiom
  3. Rails plugin that represents an idiom
  4. Rails plugin that represents an idiom
  5. motivations
  6. motivations
  7. how to make that a bit better
  8. inspirations
  9. inspirations
  10. As well as abusing the hell out of resource_controller
  11. can be overridden by default_exposure
  12. 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.