-
-
Save dgilperez/8847922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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