Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemelia committed Jun 30, 2011
0 parents commit f3c3fa2
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
pkg/*
*.gem
.bundle
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in render_anywhere.gemspec
gemspec
47 changes: 47 additions & 0 deletions README.md
@@ -0,0 +1,47 @@
render_anywhere
====================

Out of the box, Rails can render templates in a controller context only. This little gem allows for calling "render" from anywhere: models, background jobs, rake tasks, you name it.

Installation
------------------

gem install render_anywhere

Usage
--------------------

Put render_anywhere in your Gemfile:

gem 'render_anywhere', :require => false

In your Rails app, in a rake task, model, background job, or where ever you like, require render_anywhere, include the module and call render with the same arguments as ActionController::Base#render takes. It will return a string.

require 'render_anywhere'

class AnyClass
include RenderAnwhere

def build_html
html = render :template => 'normal/template/reference',
:layout => 'application'
html
end
end

Thanks
--------------------

[Yapp](http://yapp.us), whose CTO (me) kindly agreed to open source this library. App yourself!

The basic approach used here came from [this gist](https://gist.github.com/977181) by [Julien Guimont aka juggy](https://github.com/juggy). Thanks!

Author
--------------------

Luke Melia, [@lukemelia](https://twitter.com/lukemelia), [lukemelia.com](http://lukemelia.com)

License
--------------------

The MIT License. Copyright (c) 2011, Yapp, Inc.
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require 'bundler'
Bundler::GemHelper.install_tasks
15 changes: 15 additions & 0 deletions lib/render_anywhere.rb
@@ -0,0 +1,15 @@
require 'abstract_controller'
require 'action_view'

require 'render_anywhere/version'
require 'render_anywhere/rendering_controller'

module RenderAnywhere
def render(*args)
rendering_controller.render_to_string(*args)
end

def rendering_controller
@rendering_controller ||= RenderingController.new
end
end
46 changes: 46 additions & 0 deletions lib/render_anywhere/rendering_controller.rb
@@ -0,0 +1,46 @@
class RenderingController < AbstractController::Base
# Include all the concerns we need to make this work
include AbstractController::Logger
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
include Rails.application.routes.url_helpers

# this is you normal rails application helper
helper ApplicationHelper

# Define additional helpers, this one is for csrf_meta_tag
helper_method :protect_against_forgery?

# override the layout in your subclass if needed.
layout 'application'

# configure the different paths correctly
def initialize(*args)
super()
lookup_context.view_paths = Rails.root.join('app', 'views')
config.javascripts_dir = Rails.root.join('public', 'javascripts')
config.stylesheets_dir = Rails.root.join('public', 'stylesheets')
config.assets_dir = Rails.root.join('public')
end

# we are not in a browser, no need for this
def protect_against_forgery?
false
end

# so that your flash calls still work
def flash
{}
end

# same asset host as the controllers
self.asset_host = ActionController::Base.asset_host

# and nil request to differentiate between live and offline
def request
nil
end
end
3 changes: 3 additions & 0 deletions lib/render_anywhere/version.rb
@@ -0,0 +1,3 @@
module RenderAnywhere
VERSION = "0.0.1"
end
23 changes: 23 additions & 0 deletions render_anywhere.gemspec
@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "render_anywhere/version"

Gem::Specification.new do |s|
s.name = "render_anywhere"
s.version = RenderAnywhere::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Luke Melia"]
s.email = ["luke@lukemelia.com"]
s.homepage = ""
s.summary = %q{Render Rails templates to a string from anywhere.}
s.description = %q{Out of the box, Rails will render templates in a controller context only. This gem allows for calling "render" from anywhere: models, background jobs, rake tasks, you name it.}

s.rubyforge_project = "render_anywhere"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency('rails', '>= 3.0.7')
end

0 comments on commit f3c3fa2

Please sign in to comment.