Merge pull request #68 from jovoto-team/master

Add a decorate helper method to perform decoration in the view template.
This commit is contained in:
Jeff Casimir 2011-10-28 17:11:48 -07:00
commit a9b10d7022
4 changed files with 39 additions and 1 deletions

View File

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

View File

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

View File

@ -1,4 +1,9 @@
module ActiveRecord
class Base
def self.limit
self
end
end
end

View File

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