tests for decorating enumerable proxy
This commit is contained in:
parent
ba08ed571b
commit
59363d407c
|
@ -5,8 +5,6 @@ module Draper
|
||||||
|
|
||||||
delegate :as_json, :collect, :map, :each, :[], :all?, :include?, :first, :last, :shift, :to => :decorated_collection
|
delegate :as_json, :collect, :map, :each, :[], :all?, :include?, :first, :last, :shift, :to => :decorated_collection
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Initialize a new collection decorator instance by passing in
|
# Initialize a new collection decorator instance by passing in
|
||||||
# an instance of a collection. Pass in an optional
|
# an instance of a collection. Pass in an optional
|
||||||
# context into the options hash is stored for later use.
|
# context into the options hash is stored for later use.
|
||||||
|
@ -22,6 +20,9 @@ module Draper
|
||||||
def self.decorate(collection, options = {})
|
def self.decorate(collection, options = {})
|
||||||
new( collection, discern_class_from_my_class(options.delete(:klass)), options)
|
new( collection, discern_class_from_my_class(options.delete(:klass)), options)
|
||||||
end
|
end
|
||||||
|
class << self
|
||||||
|
alias_method :decorates, :decorate
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(collection, klass, options = {})
|
def initialize(collection, klass, options = {})
|
||||||
@wrapped_collection, @klass, @options = collection, klass, options
|
@wrapped_collection, @klass, @options = collection, klass, options
|
||||||
|
@ -89,14 +90,15 @@ module Draper
|
||||||
end
|
end
|
||||||
alias_method :to_source, :source
|
alias_method :to_source, :source
|
||||||
|
|
||||||
def h
|
def helpers
|
||||||
Draper::ViewContext.current
|
Draper::ViewContext.current
|
||||||
end
|
end
|
||||||
|
alias_method :h, :helpers
|
||||||
|
|
||||||
private
|
private
|
||||||
def self.discern_class_from_my_class default_class
|
def self.discern_class_from_my_class default_class
|
||||||
return default_class if default_class
|
return default_class if default_class
|
||||||
name = InvoiceDecorator.to_s.gsub("Decorator", "")
|
name = self.to_s.gsub("Decorator", "")
|
||||||
"#{name.singularize}Decorator".constantize
|
"#{name.singularize}Decorator".constantize
|
||||||
rescue NameError
|
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}')")
|
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}')")
|
||||||
|
|
|
@ -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
|
|
@ -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'
|
||||||
require './spec/support/samples/decorator_with_denies_all'
|
require './spec/support/samples/decorator_with_denies_all'
|
||||||
require './spec/support/samples/decorator_with_special_methods'
|
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'
|
||||||
require './spec/support/samples/namespaced_product_decorator'
|
require './spec/support/samples/namespaced_product_decorator'
|
||||||
require './spec/support/samples/non_active_model_product'
|
require './spec/support/samples/non_active_model_product'
|
||||||
require './spec/support/samples/product'
|
require './spec/support/samples/product'
|
||||||
require './spec/support/samples/product_decorator'
|
require './spec/support/samples/product_decorator'
|
||||||
|
require './spec/support/samples/products_decorator'
|
||||||
require './spec/support/samples/sequel_product'
|
require './spec/support/samples/sequel_product'
|
||||||
require './spec/support/samples/specific_product_decorator'
|
require './spec/support/samples/specific_product_decorator'
|
||||||
require './spec/support/samples/some_thing'
|
require './spec/support/samples/some_thing'
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
class EnumerableProxy < Draper::DecoratedEnumerableProxy
|
||||||
|
|
||||||
|
end
|
|
@ -0,0 +1,6 @@
|
||||||
|
class ProductsDecorator < Draper::DecoratedEnumerableProxy
|
||||||
|
|
||||||
|
def some_method
|
||||||
|
"some method works"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue