diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 3e76fab..00f56f2 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -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) diff --git a/spec/base_spec.rb b/spec/base_spec.rb index febda50..4c8f873 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/samples/application_helper.rb b/spec/samples/application_helper.rb new file mode 100644 index 0000000..3613715 --- /dev/null +++ b/spec/samples/application_helper.rb @@ -0,0 +1,5 @@ +module ApplicationHelper + def hello + "Hello, World!" + end +end \ No newline at end of file diff --git a/spec/samples/decorator_application_helper.rb b/spec/samples/decorator_application_helper.rb new file mode 100644 index 0000000..daae87c --- /dev/null +++ b/spec/samples/decorator_application_helper.rb @@ -0,0 +1,5 @@ +class DecoratorApplicationHelper < Draper::Base + def uses_hello + self.hello + end +end diff --git a/spec/samples/decorator_with_allows.rb b/spec/samples/decorator_with_allows.rb new file mode 100644 index 0000000..1fc0921 --- /dev/null +++ b/spec/samples/decorator_with_allows.rb @@ -0,0 +1,3 @@ +class DecoratorWithAllows < Draper::Base + allows :upcase +end \ No newline at end of file diff --git a/spec/samples/decorator_with_denies.rb b/spec/samples/decorator_with_denies.rb new file mode 100644 index 0000000..42e6f3f --- /dev/null +++ b/spec/samples/decorator_with_denies.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5b687fb..2712d57 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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|