Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Defacing Your Views

Chris Mar
August 10, 2012

Defacing Your Views

Deface is a gem to make targeted changes to views without altering the source file. It was developed by the Spree core team to help when multiple engines attempt to override the same view file.

DC Rug August 9, 2012

Chris Mar

August 10, 2012
Tweet

More Decks by Chris Mar

Other Decks in Programming

Transcript

  1. CHRIS MAR • Spree Core Team Member • github.com/cmar •

    @cmar • @spreecommerce • @spreeworks Friday, August 10, 12
  2. MAIN RAILS APPLICATION ENGINE #1 ENGINE #2 CHANGE CHANGE If

    there was only a gem for that! Friday, August 10, 12
  3. ORIGINAL ERB DEFACE PRE-PARSER PSEUDO ERB XML NOKOGIRI PSEUDO ERB

    XML DEFACE OVERRIDES Friday, August 10, 12
  4. ORIGINAL ERB DEFACE PRE-PARSER PSEUDO ERB XML NOKOGIRI PSEUDO ERB

    XML DEFACE OVERRIDES DEFACE POST-PARSER FINAL ERB Friday, August 10, 12
  5. 1 2 3 4 5 6 7 8 <div id="product-images"

    data-hook="product_images"> <div id="main-image" data-hook> <%= render 'image' %> </div> <div id="thumbs" data-hook> <%= render 'thumbnails', :product => @product %> </div> </div> Deface::Override.new(:virtual_path => "spree/products/show", :name => "tweak_thumbs", :insert_bottom => "div#thumbs", :text => ‘<span>Hello World</span>’) 1 2 3 4 Friday, August 10, 12
  6. 1 2 3 4 5 6 7 8 9 <div

    id="product-images" data-hook="product_images"> <div id="main-image" data-hook> <%= render 'image' %> </div> <div id="thumbnails" data-hook> <%= render 'thumbnails', :product => @product %> </div> </div> FINAL Friday, August 10, 12
  7. 1 2 3 4 5 6 7 8 9 <div

    id="product-images" data-hook="product_images"> <div id="main-image" data-hook> <%= render 'image' %> </div> <div id="thumbnails" data-hook> <%= render 'thumbnails', :product => @product %> </div> </div> <span>Hello World</span> FINAL Friday, August 10, 12
  8. MATCHERS • CSS3 selectors supported by Nokogiri • <% %>

    matched as code[erb-silent] • code[erb-loud]:contains(‘end‘) matches <%= end %> Friday, August 10, 12
  9. ACTIONS • insert_(after, before, top, bottom) • remove • (replace,

    surround)_contents • (set, add_to, remove_from)_attributes Friday, August 10, 12
  10. SOURCE • content to be inserted • erb • partials

    • text • HAML Friday, August 10, 12
  11. MAGIC ActionView::Template.class_eval do alias_method :initialize_without_deface, :initialize def initialize(source, identifier, handler,

    details) ... processed_source = Deface::Override.apply(source, details, true) ... initialize_without_deface(processed_source, identifier, handler, details) end end Friday, August 10, 12
  12. #RUBYLOCO • LOudoun COunty WFH + Ruby • Last Friday

    Lunch in Leesburg • @ruby_loco Friday, August 10, 12