Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created December 20, 2012 04:10
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jrochkind/4342928 to your computer and use it in GitHub Desktop.
# 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
# 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