public
Last active

Offline Template for Rails 2.3.x

  • Download Gist
offline_template.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# Public: Template to render views outside the context of a controller.
#
# Useful for rendering Rails 2.3.x views in rake tasks or background jobs when a
# controller is unavailable.
#
# Examples
#
# template = OfflineTemplate.new(:users)
# template.render("users/index", :layout => false, :locals => { :users => users })
#
# template = OfflineTemplate.new(ProjectsHelper, PermissionsHelper)
# template.render("projects/recent", :projects => recent_projects)
#
class OfflineTemplate
include ActionController::UrlWriter
include ActionController::Helpers::ClassMethods
 
# Public: Returns the ActionView::Base internal view.
attr_reader :view
 
# Public: Convenience method to render
delegate :render, :to => :view
 
# Public: Initialize an offline template for the current Rails environment.
#
# helpers - The Rails helpers to include (listed as symbols or modules).
def initialize(*helpers)
helper(helpers + [ApplicationHelper])
@view = ActionView::Base.new(Rails.configuration.view_path, {}, self)
@view.class.send(:include, master_helper_module)
end
 
private
 
# Internal: Required to use ActionConroller::Helpers.
#
# Returns a Module to collect helper methods.
def master_helper_module
@master_helper_module ||= Module.new
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.