-
-
Save exocode/db83b705098a70cac159 to your computer and use it in GitHub Desktop.
Render with ``render_to_string`` a complete page within a sidekiq worker including current_user
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
module ApplicationHelper | |
def current_user | |
@current_user ||= warden.authenticate(:scope => :user) | |
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
# create the template | |
template = PageOfflineTemplate.new | |
data = template.render_to_string( | |
template: "blah/my_page_to_render.html.erb", | |
layout: false, | |
locals: | |
:@current_user => user | |
) | |
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
class OfflineTemplateController < AbstractController::Base | |
include AbstractController::Logger | |
include AbstractController::Rendering | |
include AbstractController::Layouts | |
include AbstractController::Helpers | |
include AbstractController::Translation | |
include AbstractController::AssetPaths | |
include ActionDispatch::Routing::UrlFor | |
include Rails.application.routes.url_helpers | |
Rails.application.routes.default_url_options = { :host => 'www.yoursite.com' } | |
# include Devise::Controllers::Helpers | |
helper ApplicationHelper | |
helper_method :protect_against_forgery? | |
# 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 | |
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
# subclass to define the @ attributes your tempalte will use | |
class PageOfflineTemplate < OfflineTemplate | |
attr_accessor :current_user | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment