Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Created June 23, 2017 15:14
Show Gist options
  • Save rebelweb/231d1a68ecb233bc4d8b159a18aa0866 to your computer and use it in GitHub Desktop.
Save rebelweb/231d1a68ecb233bc4d8b159a18aa0866 to your computer and use it in GitHub Desktop.
Decorator using simple delegator that follows most of draper syntax
require 'delegate'
class ApplicationDecorator < SimpleDelegator
def self.decorate(instance)
return nil if instance.blank?
new instance
end
def self.decorate_collection(collection)
return [] if collection.nil? || collection.length.zero?
collection.map { |instance| new instance }
end
private
def h
ApplicationController.helpers
end
def r
Rails.application.routes.url_helpers
end
def object
__getobj__
end
def company
Company.first
end
end
@rebelweb
Copy link
Author

I only used the basics of draper. The only change is this:

def person
  PersonDecorator.decorate(object.person)
end

instead of this

  decorates_association :person, with: PersonDecorator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment