dry up your views

50
smartbomb oo r r DRY your views! Lachie Cox [email protected]

Upload: lachie

Post on 15-Jan-2015

1.353 views

Category:

Technology


1 download

DESCRIPTION

My presentation at RORO sydney september meetup.

TRANSCRIPT

Page 1: DRY up your views

smartbomb

o or r

DRY your views!Lachie Cox

[email protected]

Page 2: DRY up your views

smartbombsmartbomb

DRY‣ don’t repeat yourself, mmkay?

Page 3: DRY up your views

smartbombsmartbomb

M V C‣v is for vexing

Page 4: DRY up your views

smartbombsmartbomb

DRYing models is

easy

Page 5: DRY up your views

smartbombsmartbomb

DRYing ctrllrs is

OK

Page 6: DRY up your views

smartbombsmartbomb

DRYing views is

vexatious

Page 7: DRY up your views

smartbombsmartbomb

WET views

sca!olding

Page 8: DRY up your views

smartbombsmartbomb

Page 9: DRY up your views

smartbombsmartbomb

WET views

erb

layouts

partials

Page 10: DRY up your views

smartbombsmartbomb

Page 11: DRY up your views

smartbombsmartbomb

<% labeled_form_for :thing, things_path do |f| %> <%= f.text_field :name %> <%= f.text_field :aspect %> <%= f.check_box :existing %> <%= submit_tag 'Save' %><% end %>

Page 12: DRY up your views

smartbombsmartbomb

<% labeled_form_for :thing, things_path do |f| %>

<%= f.text_field :name %>

<%= f.text_field :aspect %>

<%= f.check_box :existing %>

<%= submit_tag 'Save' %>

<% end %>

WASTE

Page 13: DRY up your views

smartbombsmartbomb

waste baaaad

Page 14: DRY up your views

smartbombsmartbomb

components

engines

Page 15: DRY up your views

smartbombsmartbomb

Page 17: DRY up your views

smartbombsmartbomb

Don’t Fear Helpers

Make them APIs

Page 18: DRY up your views

smartbombsmartbomb

Make them semantic

Page 19: DRY up your views

smartbombsmartbomb

helpers as api

<%= describe person,:blank_message => "Nobody" %>

Page 20: DRY up your views

smartbombsmartbomb

helpers as api

<% rounded_box do %> <h2>Products</h2><% end %>

Page 21: DRY up your views

smartbombsmartbomb

stencil

‣ http://stencil.rubyforge.org

Page 22: DRY up your views

smartbombsmartbomb

semantic helpers

as objects

Page 23: DRY up your views

smartbombsmartbomb

a cart stencil

class CartStencil < Stencil ... def to_s render(:partial => 'shared/cart', :object => @cart) end def summary if @cart.blank? "Your cart is empty. Buy stuff!" else "You have #{@cart.size} items in your cart." end endend

Page 24: DRY up your views

smartbombsmartbomb

a cart stencil

<% cart = cart() %> <!-- CartStencil.new -->

<%= cart.summary %>

<%= cart %> <!-- cart.to_s -->

Page 25: DRY up your views

smartbombsmartbomb

more than a stencil

module CartControllerHelper ...end

class UserController include CartControllerHelper load_cart :only => [:show]end

Page 26: DRY up your views

smartbombsmartbomb

Vcart

(stencil)

Ccart

(mixin)

Page 27: DRY up your views

smartbombsmartbomb

Stencil+

Mixin

CSS

+javascript

+

&c.

Page 28: DRY up your views

smartbombsmartbomb

lightweight

widgetty

thingo

Page 29: DRY up your views

smartbombsmartbomb

Page 30: DRY up your views

smartbombsmartbomb

DRY

javascript

Page 31: DRY up your views

smartbombsmartbomb

RJS

Page 32: DRY up your views

smartbombsmartbomb

Page 33: DRY up your views

smartbombsmartbomb

document.getElementById("foobar")

$("foobar")

Page 34: DRY up your views

smartbombsmartbomb

use prototype‣ until optimisation

Page 35: DRY up your views

smartbombsmartbomb

<%= link_to_function 'declaim', 'alert("yay for smarties!")' %>

<%= link_to 'delete', smarty_path(42), :method => :delete %>

Page 36: DRY up your views

smartbombsmartbomb

Page 37: DRY up your views

smartbombsmartbomb

srsly, wtf?

<a onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;" href="/smarties/1">delete</a>

Page 38: DRY up your views

smartbombsmartbomb

Page 39: DRY up your views

smartbombsmartbomb

be

less

obtrusive

Page 40: DRY up your views

smartbombsmartbomb

use

less

code

Page 41: DRY up your views

smartbombsmartbomb

OMG

javascript

has classes

too!

Page 42: DRY up your views

smartbombsmartbomb

use

&

reuse

Page 43: DRY up your views

smartbombsmartbomb

lowpro‣ http://danwebb.net/lowpro

Page 44: DRY up your views

smartbombsmartbomb

reusable

behaviours

Page 45: DRY up your views

smartbombsmartbomb

Page 46: DRY up your views

smartbombsmartbomb

Page 47: DRY up your views

smartbombsmartbomb

<a class="forgotten" href="/accounts/forgotten">I've forgotten my password.</a>

HTML

Page 48: DRY up your views

smartbombsmartbomb

Event.addBehavior({ '.forgotten': Toggler('forgotten_password','cancel')})

<a class="forgotten" href="/accounts/forgotten">I've forgotten my password.</a>

javascript

HTML

Page 49: DRY up your views

smartbombsmartbomb

Page 50: DRY up your views

smartbombsmartbomb

DRY

its good for the soul