tests for decorating enumerable proxy

This commit is contained in:
stevenbristol 2012-08-30 09:00:37 -04:00
parent ba08ed571b
commit 59363d407c
5 changed files with 57 additions and 4 deletions

View File

@ -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}')")

View File

@ -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

View File

@ -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'

View File

@ -0,0 +1,3 @@
class EnumerableProxy < Draper::DecoratedEnumerableProxy
end

View File

@ -0,0 +1,6 @@
class ProductsDecorator < Draper::DecoratedEnumerableProxy
def some_method
"some method works"
end
end