Rails: Calling render() outside your Controllers

I found myself needing to render a .rxml template from my Model today (Ok, stop the MVC preach already…) anyways.. found a few deadends, but finally got down to use method_missing to see what’s needed by ActionViewer::Base. Not sure if there’s an easier way…

 

# 
# lib/render_anywhere.rb
#
# Render templates outside of a controller or view.
#
# Simply mixin this module to your existing class, for example:
#
# class MyTemplater < ActiveRecord::Base
# include RenderAnywhere
#
# And you can use render() method that works the same as ActionView::Base#render
#
# obj = MyTemplater.new
# obj.html = obj.render :file => '/shared/header'
#
#
module RenderAnywhere

class DummyController
def logger
RAILS_DEFAULT_LOGGER
end
def headers
{}
end
end

def render(options, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.render options
end

def template_exists?(path, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.pick_template_extension(path) rescue false
end
end

Hope this is helpful to someone