Skip to content

Instantly share code, notes, and snippets.

@overture8
Forked from zorab47/offline_template.rb
Created December 1, 2011 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overture8/1419425 to your computer and use it in GitHub Desktop.
Save overture8/1419425 to your computer and use it in GitHub Desktop.
Offline Template for Rails 2.3.x
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment