Question

I have STI model

#a/m/document.rb
class Document < ActiveRecord::Base
end

#a/m/document/static.rb
class Document::Static < Document
end

#a/m/document/dynamic.rb
class Document::Dynamic < Document
end

I'm using draper gem to decorate my model

# a/d/document_decorator.rb
class DocumentDecorator <  ApplicationDecorator
end

 # a/d/document/static_decorator.rb
 class Document::StaticDecorator < DocumentDecorator
   def foo
     'it works 1'
   end
 end


 # a/d/document/dynamic_decorator.rb
 class Document::DynamicDecorator < DocumentDecorator
   def foo
     'it works 2'
   end
 end

is there posibble way to tell draper to automatically decorate model with propriate STI class decorator ? Like this:

a = Document.last   #<Document::Static ...
a.type              #Document::Static
b = DocumentDecorator.decorate(a)
b.class             # Document::StaticDecorator
b.foo               # "it works 1"
Was it helpful?

Solution

Took me while to discover that I can do just

resource.decorate

and it will find propriet decorator

a = Document.last   #<Document::Static ...
a.type              #Document::Static
b = a.decorate          
b.class             # Document::StaticDecorator

if you need to explicitly decorate object with Document decorator do this

a = Document.last   #<Document::Static ...
a.type              #Document::Static
b = DocumentDecorator.decorate a
b.class             # DocumentDecorator
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top