Skip to content

Commit

Permalink
add namespaced model support
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaya committed Oct 31, 2011
1 parent 1c3d566 commit 4ef3d2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/draper/base.rb
Expand Up @@ -47,8 +47,8 @@ def self.find(input, context = {})
# 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
Expand Down
14 changes: 14 additions & 0 deletions spec/draper/base_spec.rb
Expand Up @@ -58,6 +58,20 @@ class BusinessDecorator < Draper::Base
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
Expand Down
10 changes: 10 additions & 0 deletions spec/draper/model_support_spec.rb
Expand Up @@ -26,4 +26,14 @@
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

0 comments on commit 4ef3d2b

Please sign in to comment.