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

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

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.