module LiquorTemplateLoader def self.namespace_to_params(pack, namespace) if namespace == :html scope = pack.html_templates externals = [ :site, :request ] elsif namespace == :email scope = pack.email_templates externals = [ :site, :subscription ] end [ scope, externals ] end def self.load(pack, namespace) manager = Liquor::Manager.new(import: [ # Plugins Liquor::Pagination, FeedbackTag, EmailTag, # Core and plugin functions CoreFunctions, # Functions by model classes VideoFunctions, PostFunctions, MobileFunctions, EventFunctions, MarkupFunctions ]) scope, scope_externals = namespace_to_params(pack, namespace) template_externals_map = scope.system_templates ActiveSupport::Notifications.instrument("load.liquor") do scope.includes(:site).turned_on.each do |template| name, body = template.name, template.body if manager.partial? name manager.register_partial name, body else ActiveSupport::Notifications.instrument("load_template.liquor", name: name) do if template_externals_map.include? name externals = scope_externals + template_externals_map[name] else externals = scope_externals end manager.register_template name, body, externals end end end end ActiveSupport::Notifications.instrument("compile.liquor") do manager.compile end manager end def self.try_load(pack, namespace) manager = load(pack, namespace) manager.errors end @cache = {} @cache_timestamps = {} def self.load_with_caching(pack, namespace) cache_key = "#{pack.id}_#{namespace}" scope, = namespace_to_params(pack, namespace) last_updated_at = scope.maximum('updated_at') || Time.now if !@cache_timestamps.has_key?(cache_key) || @cache_timestamps[cache_key] < last_updated_at @cache[cache_key] = load(pack, namespace) @cache_timestamps[cache_key] = last_updated_at end @cache[cache_key] end end