diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f30a35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +pkg/* +*.gem +.bundle diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fb1be68 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in render_anywhere.gemspec +gemspec diff --git a/README.md b/README.md new file mode 100644 index 0000000..93e601c --- /dev/null +++ b/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. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..14cfe0b --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require 'bundler' +Bundler::GemHelper.install_tasks diff --git a/lib/render_anywhere.rb b/lib/render_anywhere.rb new file mode 100644 index 0000000..38f4077 --- /dev/null +++ b/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 diff --git a/lib/render_anywhere/rendering_controller.rb b/lib/render_anywhere/rendering_controller.rb new file mode 100644 index 0000000..67da4f6 --- /dev/null +++ b/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 diff --git a/lib/render_anywhere/version.rb b/lib/render_anywhere/version.rb new file mode 100644 index 0000000..322d40f --- /dev/null +++ b/lib/render_anywhere/version.rb @@ -0,0 +1,3 @@ +module RenderAnywhere + VERSION = "0.0.1" +end diff --git a/render_anywhere.gemspec b/render_anywhere.gemspec new file mode 100644 index 0000000..ae340ae --- /dev/null +++ b/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