mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
added support to decorate a ActiveRecord collection
ActiveRecordCollection.decorate returns a DecoratorEnumerableProxy
This commit is contained in:
parent
d72f2a1ee8
commit
aad32faa3d
4 changed files with 39 additions and 1 deletions
|
@ -3,4 +3,15 @@ module Draper::ModelSupport
|
||||||
@decorator ||= "#{self.class.name}Decorator".constantize.decorate(self)
|
@decorator ||= "#{self.class.name}Decorator".constantize.decorate(self)
|
||||||
block_given? ? yield(@decorator) : @decorator
|
block_given? ? yield(@decorator) : @decorator
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -12,4 +12,16 @@ describe Draper::ModelSupport do
|
||||||
a.should eql "Awesome Title"
|
a.should eql "Awesome Title"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class Base
|
class Base
|
||||||
|
|
||||||
|
def self.limit
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,14 @@
|
||||||
class Product < ActiveRecord::Base
|
class Product < ActiveRecord::Base
|
||||||
|
include Draper::ModelSupport
|
||||||
|
|
||||||
|
def self.scoped
|
||||||
|
[Product.new]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.model_name
|
||||||
|
"Product"
|
||||||
|
end
|
||||||
|
|
||||||
def self.find(id)
|
def self.find(id)
|
||||||
return Product.new
|
return Product.new
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue