Skip to content

Instantly share code, notes, and snippets.

@madsheep
Last active December 11, 2015 00:58
Show Gist options
  • Save madsheep/4519822 to your computer and use it in GitHub Desktop.
Save madsheep/4519822 to your computer and use it in GitHub Desktop.
Decent exposure decreases coupling in your controller and views. Draper helps you stop writing those messy helpers. I find forcing myself to decorate every object a good practice - we call this a 'decent decoration'
class ApplicationController < ActionController::Base
extend DecentDecorate
end
# you need a 'gem "draper"' in your Gemfile for this to work
module DecentDecorate
def decorate(name, klass = nil, &block)
klass = "#{(klass || name).to_s.classify}Decorator".constantize
expose(name){ klass.decorate self.instance_eval(&block) }
end
end
# sample controller
class UsersController < ApplicationController
# this will raise an exception unless you define UserDecorator with draper
decorate(:users){ User.all }
end
@madsheep
Copy link
Author

This is just a hack - if you need something more sophisticated you c'd try a gem my colleague created: https://github.com/netguru/decent_decoration

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