Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Recently I wanted to take a rake task defined like Horace Loeb mentioned and translate it into a self contained background job, but it didn't easily translate.</p> <p>Here is my implementation for Rails 2.3.x because the <a href="http://jguimont.com/post/5582583230/how-to-render-a-full-page-template-in-a-rake-task-with" rel="nofollow">Rails 3 implementation</a> I found wouldn't work.</p> <pre><code># Public: Template to render views outside the context of a controller. # # Useful for rendering views in rake tasks or background jobs when a # controller is unavailable. # # Examples # # template = OfflineTemplate.new(:users) # template.render("users/index", :layout =&gt; false, :locals =&gt; { :users =&gt; users }) # # template = OfflineTemplate.new(ProjectsHelper, PermissionsHelper) # template.render("projects/recent", :projects =&gt; 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 delegate :render, :to =&gt; :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 </code></pre> <p>This is available as a gist: <a href="https://gist.github.com/1386052" rel="nofollow">https://gist.github.com/1386052</a>.</p> <p>Then you can use the class above to create an OfflineTemplate to render your views in a rake task:</p> <pre><code>task :recent_projects =&gt; :environment do template = OfflineTemplate.new(ProjectsHelper, PermissionsHelper) puts template.render("projects/recent", :projects =&gt; recent_projects) end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload