From 4ef3d2b7cd1a47042ac83c9795f93516e70f1dea Mon Sep 17 00:00:00 2001 From: ayaya Date: Tue, 1 Nov 2011 01:58:37 +0800 Subject: [PATCH] add namespaced model support --- lib/draper/base.rb | 4 ++-- spec/draper/base_spec.rb | 14 ++++++++++++++ spec/draper/model_support_spec.rb | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index c238d25..d76e13d 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -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 diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index 558d1a4..1886fa1 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -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 diff --git a/spec/draper/model_support_spec.rb b/spec/draper/model_support_spec.rb index 1a554b9..9f239ab 100644 --- a/spec/draper/model_support_spec.rb +++ b/spec/draper/model_support_spec.rb @@ -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