render_anywhere 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/render_anywhere.rb +15 -0
- data/lib/render_anywhere/rendering_controller.rb +46 -0
- data/lib/render_anywhere/version.rb +3 -0
- data/render_anywhere.gemspec +23 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
render_anywhere
|
2
|
+
====================
|
3
|
+
|
4
|
+
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.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------------
|
8
|
+
|
9
|
+
gem install render_anywhere
|
10
|
+
|
11
|
+
Usage
|
12
|
+
--------------------
|
13
|
+
|
14
|
+
Put render_anywhere in your Gemfile:
|
15
|
+
|
16
|
+
gem 'render_anywhere', :require => false
|
17
|
+
|
18
|
+
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.
|
19
|
+
|
20
|
+
require 'render_anywhere'
|
21
|
+
|
22
|
+
class AnyClass
|
23
|
+
include RenderAnwhere
|
24
|
+
|
25
|
+
def build_html
|
26
|
+
html = render :template => 'normal/template/reference',
|
27
|
+
:layout => 'application'
|
28
|
+
html
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Thanks
|
33
|
+
--------------------
|
34
|
+
|
35
|
+
[Yapp](http://yapp.us), whose CTO (me) kindly agreed to open source this library. App yourself!
|
36
|
+
|
37
|
+
The basic approach used here came from [this gist](https://gist.github.com/977181) by [Julien Guimont aka juggy](https://github.com/juggy). Thanks!
|
38
|
+
|
39
|
+
Author
|
40
|
+
--------------------
|
41
|
+
|
42
|
+
Luke Melia, [@lukemelia](https://twitter.com/lukemelia), [lukemelia.com](http://lukemelia.com)
|
43
|
+
|
44
|
+
License
|
45
|
+
--------------------
|
46
|
+
|
47
|
+
The MIT License. Copyright (c) 2011, Yapp, Inc.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'abstract_controller'
|
2
|
+
require 'action_view'
|
3
|
+
|
4
|
+
require 'render_anywhere/version'
|
5
|
+
require 'render_anywhere/rendering_controller'
|
6
|
+
|
7
|
+
module RenderAnywhere
|
8
|
+
def render(*args)
|
9
|
+
rendering_controller.render_to_string(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def rendering_controller
|
13
|
+
@rendering_controller ||= RenderingController.new
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class RenderingController < AbstractController::Base
|
2
|
+
# Include all the concerns we need to make this work
|
3
|
+
include AbstractController::Logger
|
4
|
+
include AbstractController::Rendering
|
5
|
+
include AbstractController::Layouts
|
6
|
+
include AbstractController::Helpers
|
7
|
+
include AbstractController::Translation
|
8
|
+
include AbstractController::AssetPaths
|
9
|
+
include Rails.application.routes.url_helpers
|
10
|
+
|
11
|
+
# this is you normal rails application helper
|
12
|
+
helper ApplicationHelper
|
13
|
+
|
14
|
+
# Define additional helpers, this one is for csrf_meta_tag
|
15
|
+
helper_method :protect_against_forgery?
|
16
|
+
|
17
|
+
# override the layout in your subclass if needed.
|
18
|
+
layout 'application'
|
19
|
+
|
20
|
+
# configure the different paths correctly
|
21
|
+
def initialize(*args)
|
22
|
+
super()
|
23
|
+
lookup_context.view_paths = Rails.root.join('app', 'views')
|
24
|
+
config.javascripts_dir = Rails.root.join('public', 'javascripts')
|
25
|
+
config.stylesheets_dir = Rails.root.join('public', 'stylesheets')
|
26
|
+
config.assets_dir = Rails.root.join('public')
|
27
|
+
end
|
28
|
+
|
29
|
+
# we are not in a browser, no need for this
|
30
|
+
def protect_against_forgery?
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
# so that your flash calls still work
|
35
|
+
def flash
|
36
|
+
{}
|
37
|
+
end
|
38
|
+
|
39
|
+
# same asset host as the controllers
|
40
|
+
self.asset_host = ActionController::Base.asset_host
|
41
|
+
|
42
|
+
# and nil request to differentiate between live and offline
|
43
|
+
def request
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "render_anywhere/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "render_anywhere"
|
7
|
+
s.version = RenderAnywhere::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Luke Melia"]
|
10
|
+
s.email = ["luke@lukemelia.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Render Rails templates to a string from anywhere.}
|
13
|
+
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.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "render_anywhere"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency('rails', '>= 3.0.7')
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: render_anywhere
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Luke Melia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-30 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rails
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 3.0.7
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: "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."
|
28
|
+
email:
|
29
|
+
- luke@lukemelia.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- lib/render_anywhere.rb
|
42
|
+
- lib/render_anywhere/rendering_controller.rb
|
43
|
+
- lib/render_anywhere/version.rb
|
44
|
+
- render_anywhere.gemspec
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: ""
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: render_anywhere
|
69
|
+
rubygems_version: 1.6.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Render Rails templates to a string from anywhere.
|
73
|
+
test_files: []
|
74
|
+
|