Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-build fragment cache (internal approach)

I want to pre-build a partial that takes a few seconds to render. I have seen functions which use proxies to preload the cache via http, but I would like to have an "internal" solution.

This is my function, which is called whenever myobject is updated:

def pre_build_partial myobject
  the_controller = ActionController::Base.new
  the_controller.instance_variable_set '@myobject', myobject

  view_renderer = ActionView::Renderer.new the_controller.lookup_context
  view_renderer.render the_controller.view_context, { partial: 'mypartial', layout: false }
end

It works fine for partials that use basic helpers, but my custom helpers throw errors:

undefined method `my_custom_helper_function' for #<#<Class:...>

I guess the helper has to be included in the_controller, but I can't find a way to do so. Thanks for any help in advance!

like image 617
Railsana Avatar asked Oct 19 '22 19:10

Railsana


1 Answers

You could pick some ideas from the source :) https://github.com/rails/rails/issues/18409

like image 172
Cristian Bica Avatar answered Oct 27 '22 01:10

Cristian Bica