From 3670ef76eee946bdb91f4ed95942cc80e89e25a7 Mon Sep 17 00:00:00 2001 From: Jeff Casimir Date: Fri, 28 Oct 2011 20:38:43 -0400 Subject: [PATCH] Wrap the .all method to return a DecoratedEnumerableProxy --- lib/draper/base.rb | 8 ++++++++ spec/draper/base_spec.rb | 10 ++++++++++ spec/support/samples/product.rb | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index c4cdb0c..c978fc7 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -95,6 +95,14 @@ module Draper def self.decorate(input, context = {}) input.respond_to?(:each) ? Draper::DecoratedEnumerableProxy.new(input, self, context) : new(input, context) end + + # Fetch all instances of the decorated class and decorate them. + # + # @param [Object] context (optional) + # @return [Draper::DecoratedEnumerableProxy] + def self.all(context = {}) + Draper::DecoratedEnumerableProxy.new(model_class.all, self, context) + end # Access the helpers proxy to call built-in and user-defined # Rails helpers. Aliased to `.h` for convinience. diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index 9877b15..5c15d72 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -237,6 +237,16 @@ describe Draper::Base do subject_one.should_not == subject_two end + context '#all' do + it "should return a decorated collection" do + ProductDecorator.all.first.should be_instance_of ProductDecorator + end + + it "should accept a context" do + collection = ProductDecorator.all(:admin) + collection.first.context.should == :admin + end + end end describe "a sample usage with denies" do diff --git a/spec/support/samples/product.rb b/spec/support/samples/product.rb index 940627e..66ef9d1 100644 --- a/spec/support/samples/product.rb +++ b/spec/support/samples/product.rb @@ -1,5 +1,9 @@ class Product < ActiveRecord::Base include Draper::ModelSupport + + def self.all + [Product.new, Product.new] + end def self.scoped [Product.new]