Last active
December 11, 2015 00:58
-
-
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'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
extend DecentDecorate | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sample controller | |
class UsersController < ApplicationController | |
# this will raise an exception unless you define UserDecorator with draper | |
decorate(:users){ User.all } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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