Created
October 17, 2012 01:20
-
-
Save calvincorreli/3903202 to your computer and use it in GitHub Desktop.
Code to render a view from inside a model
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
def render_to_string | |
I18n.with_locale(locale) do | |
Renderer.render( | |
file: "purchase_mailer/invoice", | |
layout: nil, | |
assigns: { charge: self, purchase: purchase, product: product, account: account, payments: payments } | |
) | |
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
class Renderer | |
def self.render(options) | |
(@@renderer ||= self.new).render(options) | |
end | |
def view_context_class | |
Class.new(ActionView::Base) do | |
include ApplicationHelper | |
include EmailHelper | |
include Rails.application.routes.url_helpers | |
end | |
end | |
def _prefixes | |
[] | |
end | |
def render(options) | |
view_context_class.new(ActionController::Base.view_paths, options.delete(:assigns), self).render(options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment