-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f3c3fa2
Showing
8 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pkg/* | ||
*.gem | ||
.bundle |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in render_anywhere.gemspec | ||
gemspec |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'bundler' | ||
Bundler::GemHelper.install_tasks |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module RenderAnywhere | ||
VERSION = "0.0.1" | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |