Skip to content

Instantly share code, notes, and snippets.

@ivarvong
Forked from juggy/call_template.rb
Created July 30, 2014 16:42
Show Gist options
  • Save ivarvong/75fffcee1113a2025b3f to your computer and use it in GitHub Desktop.
Save ivarvong/75fffcee1113a2025b3f to your computer and use it in GitHub Desktop.
# create the template
template = PageOfflineTemplate.new
template.quote = quote
template.pages = quote.build_pages
# Here I render a template with layout to a string then a PDF
pdf = PDFKit.new template.render_to_string(:template=>"quotes/review.html.haml")
class OfflineTemplate < 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 ActionController::UrlFor
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
def params
{}
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
# subclass to define the @ attributes your tempalte will use
class PageOfflineTemplate < OfflineTemplate
attr_accessor :pages, :quote, :invoice, :quotes, :mat_pages, :wo_pages, :exp_pages
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment