Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created December 20, 2012 03:48
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jrochkind/4342817 to your computer and use it in GitHub Desktop.
Still SimpleDelegator based Decorator, but with an ActionView::Context view_context passed in too
require 'delegate'
class Base
def foo
"foo"
end
def bar
"bar"
end
end
class BaseDecorator < SimpleDelegator
def initialize(base, view_context)
super(base)
@view_context = view_context
end
def _h
@view_context
end
end
class MyDecorator < BaseDecorator
def new_method
"new_method"
end
def foo
"Overridden #{super}"
end
def a_rails_helper
_h.tag("br")
end
end
class DecoratorTest < ActionView::TestCase
def setup
@base = Base.new
# Hey, ActionView::TestCase gives us a `view` method that
# returns an ActionView::Context, pretty convenient
# for our tests.
@decorated = MyDecorator.new(@base, view)
end
def test_pass_through_methods
assert_equal "bar", @decorated.bar
end
def test_decorator_can_add_method
assert_equal "new_method", @decorated.new_method
end
def test_override_with_super
assert_equal "Overridden foo", @decorated.foo
end
def test_can_access_view_context_method
assert_equal tag("br"), @decorated.a_rails_helper
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment