Permalink
Browse files

initial commit

  • Loading branch information...
rwilcox committed Oct 20, 2011
0 parents commit 92c042948e18c6f0ed9dbf7d7827e1b41b24a720
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
@@ -0,0 +1,4 @@
source "http://rubygems.org"
# Specify your gem's dependencies in delegate_presenter.gemspec
gemspec
@@ -0,0 +1,34 @@
Introduction
=================================
This gem includes a base class, `DelegatePresenter::Base` which all your presenter classes should inherit from.
So, creating a new presenter class:
class TodoPresenter < DelegatePresenter::Base
def todo_name
s("<br>", self.description)
end
end
Wait, *what* did you just do?
=================================
You mean that little s hack? That's a way to keep yourself from going sane having to `.html_escape` everything.
[See my blog article on this subject](http://rwilcox.tumblr.com/post/10546160404/presenter-pattern-rails-3-and-html-safe)
So, what is DelegatePresenter, really?
================================
DelegatePresenter does two things:
1. Inherits from Ruby Standard Library's SimpleDelegator. This simple class solves many of the problems I've seen with decorators in the past. (calling object.method everywhere)
2. Makes Rails helpers available to you via the helpers method, gives you s (above) and h (your old Rails 2 friend)
Installation
================================
TODO: write me
@@ -0,0 +1 @@
require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "delegate_presenter/version"
Gem::Specification.new do |s|
s.name = "delegate_presenter"
s.version = DelegatePresenter::VERSION
s.authors = ["Ryan Wilcox"]
s.email = ["rwilcox@wilcoxd.com"]
s.homepage = ""
s.summary = %q{TODO: Write a gem summary}
s.description = %q{TODO: Write a gem description}
s.rubyforge_project = "delegate_presenter"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
# s.add_runtime_dependency "rest-client"
end
@@ -0,0 +1,33 @@
require "delegate_presenter/version"
module DelegatePresenter
class Base < SimpleDelegator
def initialize( object_to_delegate_to )
super(object_to_delegate_to)
end
def record_id
self.__getobj__.id
end
def helpers
ApplicationController.all_helpers
end
def s(*args)
output = "".html_safe
args.each {|current_arg|
output << current_arg.html_safe
}
output
end
def h(str)
helpers.send(:h, str)
end
end
end
@@ -0,0 +1,3 @@
module DelegatePresenter
VERSION = "0.0.1"
end
@@ -0,0 +1,21 @@
#-*- tab-width: 2; indent-tabs-mode: nil; x-auto-expand-tabs: true; x-counterpart: ../../app/presenters/application_presenter.rb; x-typographers-quotes: false; -*-
require 'spec_helper'
describe "DelegatePresenter::Base" do
describe "HTML building helpers" do
before do
@ap = DelegatePresenter::Base.new(1)
end
it "handles HTML elements that should not be escaped" do
@ap.s("<h1>Hi</h1>") == "<h1>Hi</h1>"
end
it "handles an h in the middle to indicate that a particular string actually should be escaped" do
@ap.s("<blink>hello</blink>", " world ", @ap.h("<BAD TAG>")).should == "<blink>hello</blink> world &lt;BAD TAG&gt;"
end
end
end
No changes.

0 comments on commit 92c0429

Please sign in to comment.