Skip to content

Commit

Permalink
Refactored tests to work through helper accessor and implemented to_m…
Browse files Browse the repository at this point in the history
…odel to feed form_for
  • Loading branch information
jcasimir committed Jul 23, 2011
1 parent 6c78b6c commit 2ed64ce
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
17 changes: 10 additions & 7 deletions lib/draper/base.rb
Expand Up @@ -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

Expand Down Expand Up @@ -38,18 +37,22 @@ def helpers
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
select_methods.each do |method|
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
Expand Down
2 changes: 1 addition & 1 deletion lib/draper/system.rb
@@ -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
45 changes: 22 additions & 23 deletions spec/base_spec.rb
Expand Up @@ -5,8 +5,8 @@
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
Expand Down Expand Up @@ -53,23 +53,6 @@
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
Expand Down Expand Up @@ -129,10 +112,26 @@ class DecoratorWithDeniesAndAllows < Draper::Base
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
10 changes: 10 additions & 0 deletions spec/samples/application_controller.rb
@@ -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
2 changes: 1 addition & 1 deletion spec/samples/application_helper.rb
@@ -1,5 +1,5 @@
module ApplicationHelper
def hello
def hello_world
"Hello, World!"
end
end
17 changes: 17 additions & 0 deletions spec/samples/decorator_with_application_helper.rb
@@ -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
12 changes: 0 additions & 12 deletions spec/samples/decorator_with_denies.rb
@@ -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

0 comments on commit 2ed64ce

Please sign in to comment.