add namespaced model support

This commit is contained in:
ayaya 2011-11-01 01:58:37 +08:00
parent 1c3d5667b8
commit 4ef3d2b7cd
3 changed files with 26 additions and 2 deletions

View File

@ -47,8 +47,8 @@ module Draper
# to query.
#
# @param [Symbol] class_name snakecase name of the decorated class, like `:product`
def self.decorates(input)
self.model_class = input.to_s.camelize.constantize
def self.decorates(input, options = {})
self.model_class = options[:class] || input.to_s.camelize.constantize
model_class.send :include, Draper::ModelSupport
define_method(input){ @model }
end

View File

@ -58,6 +58,20 @@ describe Draper::Base do
pd = ProductDecorator.new(source)
pd.send(:product).should == source
end
context("namespaced model supporting") do
let(:source){ Namespace::Product.new }
it "sets the model class for the decorator" do
decorator = Namespace::ProductDecorator.new(source)
decorator.model_class.should == Namespace::Product
end
it "creates a named accessor for the wrapped model" do
pd = Namespace::ProductDecorator.new(source)
pd.send(:product).should == source
end
end
end
context(".model / .to_model") do

View File

@ -26,4 +26,14 @@ describe Draper::ModelSupport do
subject.decorate.to_ary[0].model.should be_a(Product)
end
end
describe '#decorate - decorate collections of namespaced AR objects' do
subject { Namespace::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(Namespace::Product)
end
end
end