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

View without Action @ SHRUG

jede
November 14, 2011

View without Action @ SHRUG

A presentation about using a Ruby DSL (crafty) instead of ActionView in a Rails project.

jede

November 14, 2011
Tweet

More Decks by jede

Other Decks in Programming

Transcript

  1. <h1>This is <%= @cow %>!</h1> class Cow < ActiveRecord::Base def

    to_s name end end 8 onsdag 14 september 11
  2. module CowsView class Show include Crafty::HTML::All def initialize(options) @cow =

    options[:cow] end def describe(cow) "the #{cow.color} cow named #{cow.name}" end def render '<!DOCTYPE HTML>' << html do head do title "Cow app - #{describe(@cow)}" end body do h2 "Look, its #{describe(@cow)}! What's she say'n?" @cow.mu_amount.times do span class: 'sound' do 'mu' end end end end end end end 11 onsdag 14 september 11
  3. module LinkWidget def link_to(title, object, options = {}) attributes =

    {href: url_for(object)} attributes['data-confirm'] = options[:confirm] if options[:confirm] if options[:method] attributes['rel'] = 'nofollow' attributes['data-method'] = options[:method] end attributes['class'] = options[:class] if options[:class] a(attributes) do title end end end 12 onsdag 14 september 11
  4. class ApplicationController < ActionController::Base config_accessor :view_module def default_view_module self.class.to_s.gsub('Controller', 'View').constantize

    end def view self.view_module ||= default_view_module end def render_view(name, options = {}) options.reverse_merge!({ flash: flash, current_url: current_url, request_forgery_protection_token: request_forgery_protection_token, form_authenticity_token: form_authenticity_token }) render text: view::const_get(name.to_s.camelcase).new(options).render end private def current_url request.fullpath end end 13 onsdag 14 september 11
  5. Number of iterations = 50000 user system total real crafty

    7.630000 0.140000 7.770000 ( 7.750502) builder 17.420000 0.110000 17.530000 ( 17.513536) haml 17.450000 0.180000 17.630000 ( 17.600038) erector 15.400000 0.110000 15.510000 ( 15.491134) tagz 32.860000 0.650000 33.510000 ( 33.461828) nokogiri 27.450000 0.210000 27.660000 ( 27.608287) 15 onsdag 14 september 11