Created
November 22, 2011 16:18
-
-
Save zorab47/1386052 to your computer and use it in GitHub Desktop.
Offline Template for Rails 2.3.x
This file contains hidden or 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
# 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