mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
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:
commit
a9b10d7022
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
|
||||||
|
@ -22,4 +32,4 @@ class Product < ActiveRecord::Base
|
||||||
def block
|
def block
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue