diff --git a/lib/draper/model_support.rb b/lib/draper/model_support.rb index 292ee13..57cdb3a 100644 --- a/lib/draper/model_support.rb +++ b/lib/draper/model_support.rb @@ -3,4 +3,15 @@ module Draper::ModelSupport @decorator ||= "#{self.class.name}Decorator".constantize.decorate(self) block_given? ? yield(@decorator) : @decorator end + + module ClassMethods + def decorate(context = {}) + @decorator_proxy ||= "#{model_name}Decorator".constantize.decorate(self.scoped) + block_given? ? yield(@decorator_proxy) : @decorator_proxy + end + end + + def self.included(base) + base.extend(ClassMethods) + end end diff --git a/spec/draper/model_support_spec.rb b/spec/draper/model_support_spec.rb index 4a317d8..6cd88bf 100644 --- a/spec/draper/model_support_spec.rb +++ b/spec/draper/model_support_spec.rb @@ -12,4 +12,16 @@ describe Draper::ModelSupport do a.should eql "Awesome Title" end end + + describe '#decorate - decorate collections of AR objects' do + subject { Product.limit } + its(:decorate) { should be_kind_of(Draper::DecoratedEnumerableProxy) } + + it "should decorate the collection" do + subject.decorate.size.should == 1 + subject.decorate.to_ary[0].model.should be_a(Product) + end + + + end end diff --git a/spec/samples/active_record.rb b/spec/samples/active_record.rb index e34cbb4..06a2ac6 100644 --- a/spec/samples/active_record.rb +++ b/spec/samples/active_record.rb @@ -1,4 +1,9 @@ module ActiveRecord class Base + + def self.limit + self + end + end end diff --git a/spec/samples/product.rb b/spec/samples/product.rb index 4cea434..940627e 100644 --- a/spec/samples/product.rb +++ b/spec/samples/product.rb @@ -1,4 +1,14 @@ class Product < ActiveRecord::Base + include Draper::ModelSupport + + def self.scoped + [Product.new] + end + + def self.model_name + "Product" + end + def self.find(id) return Product.new end @@ -22,4 +32,4 @@ class Product < ActiveRecord::Base def block yield end -end \ No newline at end of file +end