Skip to content

Commit 4d1cef4

Browse files
committed
Merge pull request drapergem#71 from ayamomiji/namespaced-model
Namespaced model support
2 parents ffa7901 + 4ef3d2b commit 4d1cef4

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

lib/draper/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def self.find(input, context = {})
4747
# to query.
4848
#
4949
# @param [Symbol] class_name snakecase name of the decorated class, like `:product`
50-
def self.decorates(input)
51-
self.model_class = input.to_s.camelize.constantize
50+
def self.decorates(input, options = {})
51+
self.model_class = options[:class] || input.to_s.camelize.constantize
5252
model_class.send :include, Draper::ModelSupport
5353
define_method(input){ @model }
5454
end

spec/draper/base_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class BusinessDecorator < Draper::Base
5858
pd = ProductDecorator.new(source)
5959
pd.send(:product).should == source
6060
end
61+
62+
context("namespaced model supporting") do
63+
let(:source){ Namespace::Product.new }
64+
65+
it "sets the model class for the decorator" do
66+
decorator = Namespace::ProductDecorator.new(source)
67+
decorator.model_class.should == Namespace::Product
68+
end
69+
70+
it "creates a named accessor for the wrapped model" do
71+
pd = Namespace::ProductDecorator.new(source)
72+
pd.send(:product).should == source
73+
end
74+
end
6175
end
6276

6377
context(".model / .to_model") do

spec/draper/model_support_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
subject.decorate.to_ary[0].model.should be_a(Product)
2727
end
2828
end
29+
30+
describe '#decorate - decorate collections of namespaced AR objects' do
31+
subject { Namespace::Product.limit }
32+
its(:decorate) { should be_kind_of(Draper::DecoratedEnumerableProxy) }
33+
34+
it "should decorate the collection" do
35+
subject.decorate.size.should == 1
36+
subject.decorate.to_ary[0].model.should be_a(Namespace::Product)
37+
end
38+
end
2939
end
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require './spec/support/samples/product'
2+
3+
module Namespace
4+
class Product < ActiveRecord::Base
5+
include Draper::ModelSupport
6+
7+
def self.first
8+
@@first ||= Namespace::Product.new
9+
end
10+
11+
def self.last
12+
@@last ||= Namespace::Product.new
13+
end
14+
15+
def self.all
16+
[Namespace::Product.new, Namespace::Product.new]
17+
end
18+
19+
def self.scoped
20+
[Namespace::Product.new]
21+
end
22+
23+
def self.model_name
24+
"Namespace::Product"
25+
end
26+
27+
def self.find(id)
28+
return Namespace::Product.new
29+
end
30+
31+
def self.sample_class_method
32+
"sample class method"
33+
end
34+
35+
def hello_world
36+
"Hello, World"
37+
end
38+
39+
def goodnight_moon
40+
"Goodnight, Moon"
41+
end
42+
43+
def title
44+
"Sample Title"
45+
end
46+
47+
def block
48+
yield
49+
end
50+
end
51+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require './spec/support/samples/product'
2+
3+
module Namespace
4+
class ProductDecorator < Draper::Base
5+
decorates :product, :class => Namespace::Product
6+
end
7+
end

0 commit comments

Comments
 (0)