From 59363d407c532742ce1940c6e03939e57e82d49e Mon Sep 17 00:00:00 2001 From: stevenbristol Date: Thu, 30 Aug 2012 09:00:37 -0400 Subject: [PATCH] tests for decorating enumerable proxy --- lib/draper/decorated_enumerable_proxy.rb | 10 +++-- .../draper/decorated_enumerable_proxy_spec.rb | 40 +++++++++++++++++++ spec/spec_helper.rb | 2 + spec/support/samples/enumerable_proxy.rb | 3 ++ spec/support/samples/products_decorator.rb | 6 +++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 spec/draper/decorated_enumerable_proxy_spec.rb create mode 100644 spec/support/samples/enumerable_proxy.rb create mode 100644 spec/support/samples/products_decorator.rb diff --git a/lib/draper/decorated_enumerable_proxy.rb b/lib/draper/decorated_enumerable_proxy.rb index c6f89f6..ed539eb 100644 --- a/lib/draper/decorated_enumerable_proxy.rb +++ b/lib/draper/decorated_enumerable_proxy.rb @@ -4,8 +4,6 @@ module Draper include Enumerable delegate :as_json, :collect, :map, :each, :[], :all?, :include?, :first, :last, :shift, :to => :decorated_collection - - # Initialize a new collection decorator instance by passing in # an instance of a collection. Pass in an optional @@ -22,6 +20,9 @@ module Draper def self.decorate(collection, options = {}) new( collection, discern_class_from_my_class(options.delete(:klass)), options) end + class << self + alias_method :decorates, :decorate + end def initialize(collection, klass, options = {}) @wrapped_collection, @klass, @options = collection, klass, options @@ -89,14 +90,15 @@ module Draper end alias_method :to_source, :source - def h + def helpers Draper::ViewContext.current end + alias_method :h, :helpers private def self.discern_class_from_my_class default_class return default_class if default_class - name = InvoiceDecorator.to_s.gsub("Decorator", "") + name = self.to_s.gsub("Decorator", "") "#{name.singularize}Decorator".constantize rescue NameError raise NameError("You must supply a class (as the klass option) for the members of your collection or the class must be inferable from the name of this class ('#{new.class}')") diff --git a/spec/draper/decorated_enumerable_proxy_spec.rb b/spec/draper/decorated_enumerable_proxy_spec.rb new file mode 100644 index 0000000..91c1050 --- /dev/null +++ b/spec/draper/decorated_enumerable_proxy_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Draper::DecoratedEnumerableProxy do + before(:each){ ApplicationController.new.view_context } + subject{ ProductsDecorator.new(source, ProductDecorator) } + let(:source){ Product.new } + let(:non_active_model_source){ NonActiveModelProduct.new } + + context(".helpers") do + it "have a valid view_context" do + subject.helpers.should be + end + + it "is aliased to .h" do + subject.h.should == subject.helpers + end + end + + context(".decorates") do + it "sets the model for the decorated" do + EnumerableProxy.new([source], ProductDecorator).first.model.should == source + end + + it "decorates an empty array with the klass" do + EnumerableProxy.decorates([], klass: ProductDecorator).should be + end + + it "discerns collection items decorator by the name of the decorator" do + ProductsDecorator.decorates([]).should be + end + + it "methods in decorated empty array should work" do + ProductsDecorator.decorates([]).some_method.should == "some method works" + end + + it "raises when decorates an empty array without the klass" do + lambda{EnumerableProxy.decorates([])}.should raise_error + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 345fab9..8845be5 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,11 +15,13 @@ require './spec/support/samples/decorator_with_application_helper' require './spec/support/samples/decorator_with_denies' require './spec/support/samples/decorator_with_denies_all' require './spec/support/samples/decorator_with_special_methods' +require './spec/support/samples/enumerable_proxy' require './spec/support/samples/namespaced_product' require './spec/support/samples/namespaced_product_decorator' require './spec/support/samples/non_active_model_product' require './spec/support/samples/product' require './spec/support/samples/product_decorator' +require './spec/support/samples/products_decorator' require './spec/support/samples/sequel_product' require './spec/support/samples/specific_product_decorator' require './spec/support/samples/some_thing' diff --git a/spec/support/samples/enumerable_proxy.rb b/spec/support/samples/enumerable_proxy.rb new file mode 100644 index 0000000..6699bb1 --- /dev/null +++ b/spec/support/samples/enumerable_proxy.rb @@ -0,0 +1,3 @@ +class EnumerableProxy < Draper::DecoratedEnumerableProxy + +end \ No newline at end of file diff --git a/spec/support/samples/products_decorator.rb b/spec/support/samples/products_decorator.rb new file mode 100644 index 0000000..4cd6bf5 --- /dev/null +++ b/spec/support/samples/products_decorator.rb @@ -0,0 +1,6 @@ +class ProductsDecorator < Draper::DecoratedEnumerableProxy + + def some_method + "some method works" + end +end \ No newline at end of file