Refactored tests to work through helper accessor and implemented to_model to feed form_for

This commit is contained in:
Jeff Casimir 2011-07-23 06:59:09 -07:00
parent 6c78b6cca6
commit 2ed64ce56f
7 changed files with 61 additions and 44 deletions

View File

@ -2,15 +2,14 @@ module Draper
class Base
require 'active_support/core_ext/class/attribute'
class_attribute :denied, :allowed, :source_class
attr_accessor :source
DEFAULT_DENIED = Object.new.methods
self.denied = DEFAULT_DENIED
def initialize(subject)
subject.inspect
self.class.source_class = subject.class
self.source = subject
def initialize(input)
input.inspect
self.class.source_class = input.class
@model = input
build_methods
end
@ -38,10 +37,14 @@ module Draper
def self.model_name
ActiveModel::Name.new(source_class)
end
def to_model
@model
end
private
def select_methods
self.allowed || (source.public_methods - denied)
self.allowed || (@model.public_methods - denied)
end
def build_methods
@ -49,7 +52,7 @@ module Draper
unless self.respond_to?(method)
(class << self; self; end).class_eval do
define_method method do |*args, &block|
source.send method, *args, &block
@model.send method, *args, &block
end
end
end

View File

@ -1,7 +1,7 @@
module Draper
class System
def self.setup
ActionController::Base.send(:extend, Draper::AllHelpers)
ActionController::Base.send(:extend, Draper::AllHelpers) if defined?(ActionController::Base)
end
end
end

View File

@ -5,8 +5,8 @@ describe Draper::Base do
subject{ Draper::Base.new(source) }
let(:source){ "Sample String" }
it "should return the wrapped object when asked for source" do
subject.source.should == source
it "should return the wrapped object when converted to a model" do
subject.to_model.should == source
end
it "should wrap source methods so they still accept blocks" do
@ -53,23 +53,6 @@ describe Draper::Base do
it "should not clobber other decorators' methods" do
subject.should respond_to(:upcase)
end
it "should be able to use the content_tag helper" do
subject_with_denies.sample_content.to_s.should == "<span>Hello, World!</span>"
end
it "should be able to use the link_to helper" do
subject_with_denies.sample_link.should == "<a href=\"/World\">Hello</a>"
end
it "should be able to use the pluralize helper" do
pending("Figure out odd interaction when the wrapped source object already has the text_helper methods (ie: a String)")
subject_with_denies.sample_truncate.should == "Once..."
end
it "should nullify method_missing to prevent AR from being cute" do
pending("How to test this without AR? Ugh.")
end
end
describe "a sample usage with allows" do
@ -129,10 +112,26 @@ describe Draper::Base do
end
context "in a Rails application" do
it "should include ApplicationHelper if one exists" do
pending
decorator = DecoratorApplicationHelper.decorate(Object.new)
decorator.uses_hello == "Hello, World!"
let(:decorator){ DecoratorWithApplicationHelper.decorate(Object.new) }
it "should have access to ApplicationHelper helpers" do
decorator.uses_hello_world == "Hello, World!"
end
it "should be able to use the content_tag helper" do
decorator.sample_content.to_s.should == "<span>Hello, World!</span>"
end
it "should be able to use the link_to helper" do
decorator.sample_link.should == "<a href=\"/World\">Hello</a>"
end
it "should be able to use the pluralize helper" do
decorator.sample_truncate.should == "Once..."
end
it "should nullify method_missing to prevent AR from being cute" do
pending("How to test this without AR? Ugh.")
end
end
end

View File

@ -0,0 +1,10 @@
class ApplicationController
extend ActionView::Helpers
extend ActionView::Helpers::TagHelper
extend ActionView::Helpers::UrlHelper
extend ApplicationHelper
def self.all_helpers
self
end
end

View File

@ -1,5 +1,5 @@
module ApplicationHelper
def hello
def hello_world
"Hello, World!"
end
end

View File

@ -0,0 +1,17 @@
class DecoratorWithApplicationHelper < Draper::Base
def uses_hello_world
h.hello_world
end
def sample_content
h.content_tag :span, "Hello, World!"
end
def sample_link
h.link_to "Hello", "/World"
end
def sample_truncate
h.truncate("Once upon a time", :length => 7)
end
end

View File

@ -1,15 +1,3 @@
class DecoratorWithDenies < Draper::Base
denies :upcase
def sample_content
content_tag :span, "Hello, World!"
end
def sample_link
link_to "Hello", "/World"
end
def sample_truncate
ActionView::Helpers::TextHelper.truncate("Once upon a time", :length => 7)
end
end