Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
RailsConf 2014 Talk, Tuesday, 2:30, Ballroom 4, Concerns, Decorators, Presenters, Service Objects, Helpers, Help Me Decide!
Draper Decorator
Code: Github Fat Code Refactoring Techniques Pull Request Draper
Example: Added Draper Decorator class
MicropostDecorator
. Methodposted_ago
dries up code in 2 views.Why?
one model, but multiple controllers/views.
to a given model.
To quote the Draper Readme:
Decorators are the ideal place to:
name
method thatcombines
first_name
andlast_name
attributesurl
fieldinto a hyperlink
What?
Steps
gem 'draper'= and run =bundle
rails generate decorator User
, which createsapp/decorators/user_decorator.rb
andspec/decorators/user_decorator_spec.rb
.h.
in front of calls to helpers.essentially on the model.
model.decorate
to get a decorated instance of the model.Draper Applicable Situations
only one view.
given model, then the decorator is a good place to put the method.
Draper Advantages
applies to the connection of the model to the view.
helper methods as well as the model.
into it's decorated instance, either manually or automatically, at
the controller layer.
in feature specs.
Draper Disadvantages
still getting fatter.
should be broken into it's own PORO.
disadvantage in that the Decorator can turn into a junk-drawer of
loosely coupled methods.