· 1 min read

Screencast: Einführung in Draper

Downloads in verschiedenen Formaten:

mp4 m4v webm ogg

Resourcen:

bash

[bash] rails g draper:decorator user [/bash]

Gemfile

[ruby] gem ‘draper’ [/ruby]

app/decorators/user_decorator.rb

[ruby] class UserDecorator < ApplicationDecorator decorates :user allows :username def avatar site_link h.image_tag(“avatars/#{avatar_name}”, class: “avatar”) end def linked_name site_link(model.full_name.present? ? model.full_name : model.username) end def website handle_none model.url do h.link_to model.url, model.url end end def twitter handle_none model.twitter_name do h.link_to model.twitter_name, “http://twitter.com/#{model.twitter\_name}” end end def bio handle_none model.bio do markdown(model.bio) end end def member_since model.created_at.strftime(“%B %e, %Y”) end private def handle_none(value) if value.present? yield else h.content_tag :span, “None given”, class: “none” end end def site_link(content) h.link_to_if model.url.present?, content, model.url end

def avatar_name if model.avatar_image_name.present? model.avatar_image_name else “default.png” end end end [/ruby]

app/decorators/application_decorator.rb

[ruby] class ApplicationDecorator < Draper::Base def markdown(text) Redcarpet.new(text, :hard_wrap, :filter_html, :autolink).to_html.html_safe end end [/ruby]

app/controllers/users_controller.rb

[ruby] def show @user = UserDecorator.find(params[:id]) end [/ruby]

users/show.html.erb

[html]

<%= @user.avatar %>

<%= @user.linked_name %>

Username:
<%= @user.username %>
Member Since:
<%= @user.member_since %>
Website:
<%= @user.website %>
Twitter:
<%= @user.twitter %>
Bio:
<%= @user.bio %>
[/html]

Back to Blog