Pulling larger example classes out into spec/samples

This commit is contained in:
Jeff Casimir 2011-07-10 21:46:30 -04:00
parent 235dfd5933
commit 6a6f7e68da
7 changed files with 65 additions and 39 deletions

View File

@ -3,7 +3,7 @@ module Draper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TextHelper
include ApplicationHelper if defined?(ApplicationHelper)
include ApplicationHelper #if defined?(ApplicationHelper)
require 'active_support/core_ext/class/attribute'
class_attribute :denied, :allowed
@ -11,6 +11,11 @@ module Draper
DEFAULT_DENIED = Object.new.methods << :method_missing
self.denied = DEFAULT_DENIED
def initialize(subject)
self.source = subject
build_methods
end
def self.denies(*input_denied)
raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using denies" if input_denied.empty?
@ -20,15 +25,14 @@ module Draper
def self.allows(*input_allows)
raise ArgumentError, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
#raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denies == DEFAULT_EXCLUSIONS)
raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denied == DEFAULT_DENIED)
self.allowed = input_allows
end
def initialize(subject)
self.source = subject
build_methods
end
def self.decorate(input)
input.respond_to?(:each) ? input.map{|i| new(i)} : new(input)
end
private
def select_methods
self.allowed || (source.public_methods - denied)

View File

@ -13,14 +13,19 @@ describe Draper::Base do
subject.gsub("Sample"){|match| "Super"}.should == "Super String"
end
it "should return a collection of wrapped objects when given a collection of source objects" do
pending("need to fix the proxying of blocks")
sources = ["one", "two", "three"]
output = Draper::Base.new(sources)
output.should respond_to(:each)
output.size.should == sources.size
output.each{ |decorated| decorated.should be_instance_of(Draper::Base) }
debugger
context ".draper" do
it "should return a collection of wrapped objects when given a collection of source objects" do
sources = ["one", "two", "three"]
output = Draper::Base.decorate(sources)
output.should respond_to(:each)
output.size.should == sources.size
output.each{ |decorated| decorated.should be_instance_of(Draper::Base) }
end
it "should return a single wrapped object when given a single source object" do
output = Draper::Base.decorate(source)
output.should be_instance_of(Draper::Base)
end
end
it "echos the methods of the wrapped class" do
@ -37,21 +42,6 @@ describe Draper::Base do
describe "a sample usage with denies" do
before(:all) do
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
end
let(:subject_with_denies){ DecoratorWithDenies.new(source) }
@ -83,12 +73,6 @@ describe Draper::Base do
end
describe "a sample usage with allows" do
before(:all) do
class DecoratorWithAllows < Draper::Base
allows :upcase
end
end
let(:subject_with_allows){ DecoratorWithAllows.new(source) }
it "should echo the allowed method" do
@ -114,14 +98,14 @@ describe Draper::Base do
}
let(:using_allows_then_denies){
class DecoratorWithInvalidMixing < Draper::Base
class DecoratorWithAllowsAndDenies < Draper::Base
allows :upcase
denies :downcase
end
}
let(:using_denies_then_allows){
class DecoratorWithInvalidMixing < Draper::Base
class DecoratorWithDeniesAndAllows < Draper::Base
denies :downcase
allows :upcase
end
@ -143,4 +127,11 @@ describe Draper::Base do
expect {using_denies_then_allows}.should raise_error(ArgumentError)
end
end
context "in a Rails application" do
it "should include ApplicationHelper if one exists" do
decorator = DecoratorApplicationHelper.decorate(Object.new)
decorator.uses_hello == "Hello, World!"
end
end
end

View File

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

View File

@ -0,0 +1,5 @@
class DecoratorApplicationHelper < Draper::Base
def uses_hello
self.hello
end
end

View File

@ -0,0 +1,3 @@
class DecoratorWithAllows < Draper::Base
allows :upcase
end

View File

@ -0,0 +1,15 @@
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

View File

@ -1,9 +1,12 @@
require 'rubygems'
require 'bundler'
require './spec/samples/application_helper.rb'
Bundler.require
Dir.glob('./spec/samples/*') {|file| require file}
require 'active_support'
require 'action_view'
require 'bundler/setup'
require 'draper'
Dir["spec/support/**/*.rb"].each do |file|