Draper.undecorate safely undecorates any object

This allows you to pass any object to Draper.undecorate knowing you will get the original object back
even if the object passed in does is not already decorated.

Closes #600
This commit is contained in:
Amiel Martin 2014-04-22 14:05:55 -07:00
parent f260f211f2
commit 44bda4b19e
3 changed files with 29 additions and 0 deletions

View File

@ -22,6 +22,7 @@ require 'draper/decorated_association'
require 'draper/helper_support'
require 'draper/view_context'
require 'draper/collection_decorator'
require 'draper/undecorate'
require 'draper/decorates_assigned'
require 'draper/railtie' if defined?(Rails)

9
lib/draper/undecorate.rb Normal file
View File

@ -0,0 +1,9 @@
module Draper
def self.undecorate(object)
if object.respond_to?(:decorated?) && object.decorated?
object.object
else
object
end
end
end

View File

@ -0,0 +1,19 @@
require 'spec_helper'
describe Draper, '.undecorate' do
it 'undecorates a decorated object' do
object = Model.new
decorator = Draper::Decorator.new(object)
expect(Draper.undecorate(decorator)).to equal object
end
it 'passes a non-decorated object through' do
object = Model.new
expect(Draper.undecorate(object)).to equal object
end
it 'passes a non-decorator object through' do
object = Object.new
expect(Draper.undecorate(object)).to equal object
end
end