Created
December 20, 2012 04:10
-
-
Save jrochkind/4342928 to your computer and use it in GitHub Desktop.
presenter helper method with yield, basic idea taken from Ryan Bates https://github.com/railscasts/287-presenters-from-scratch/blob/master/profile-after/app/helpers/application_helper.rb#L2
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
# some ./app/helpers/some_helper.rb, in my case actually in an engine gem | |
module SomeGemHelper | |
def my_gem_decorate(model) | |
# Here I'm hard-coding to always decorate with MyDecorator, | |
# but it could also be passed in as a method arg, or guessed | |
# from the model.class name, or from a differnet model attribute | |
# like model.presenter_class, or taken from configuration, or | |
# some combination -- whatever meets the needs of your design. | |
decorated = MyDecorator.new(model, self) | |
yield if block_given? | |
decorated | |
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
# Now in any old view template, check this out: | |
<% my_gem_decorate(@model_obj) do |model_obj| %> | |
<p><%= model_obj.decorator_method %></p> | |
<%# Pass it to a partial, the partial gets the decorated | |
# object, great. %> | |
<%= render "some_partial", :object => model_obj %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment