From 614d69dc18796d4cc24d2e2d07b29331ff91c5a8 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 3 Jan 2014 14:56:16 -0800 Subject: [PATCH] move current_path in to the template context, one less piece of global state --- .../middleman-core/core_extensions/request.rb | 24 ++----------------- middleman-core/lib/middleman-core/sitemap.rb | 3 +++ .../lib/middleman-core/sitemap/resource.rb | 2 +- .../lib/middleman-core/template_context.rb | 2 +- 4 files changed, 7 insertions(+), 24 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index 6758a7bf..8a001b91 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -153,20 +153,6 @@ module Middleman # Methods to be mixed-in to Middleman::Application module InstanceMethods - # Accessor for current path - # @return [String] - def current_path - Thread.current[:current_path] - end - - # Set the current path - # - # @param [String] path The new current path - # @return [void] - def current_path=(path) - Thread.current[:current_path] = path - end - delegate :use, :to => :"self.class" delegate :map, :to => :"self.class" @@ -210,7 +196,6 @@ module Middleman # @param [Rack::Response] res def process_request(env, req, res) start_time = Time.now - current_path = nil request_path = URI.decode(env['PATH_INFO'].dup) if request_path.respond_to? :force_encoding @@ -230,17 +215,12 @@ module Middleman # If this path is a binary file, send it immediately return send_file(resource, env) if resource.binary? - current_path = resource.destination_path - res['Content-Type'] = resource.content_type || 'text/plain' begin # Write out the contents of the page - output = resource.render do - self.current_path = current_path - end + res.write resource.render - res.write output # Valid content is a 200 status res.status = 200 rescue Middleman::TemplateRenderer::TemplateNotFound => e @@ -249,7 +229,7 @@ module Middleman end # End the request - logger.debug "== Finishing Request: #{current_path} (#{(Time.now - start_time).round(2)}s)" + logger.debug "== Finishing Request: #{resource.destination_path} (#{(Time.now - start_time).round(2)}s)" halt res.finish end diff --git a/middleman-core/lib/middleman-core/sitemap.rb b/middleman-core/lib/middleman-core/sitemap.rb index 6d7b6518..fbda5b2d 100644 --- a/middleman-core/lib/middleman-core/sitemap.rb +++ b/middleman-core/lib/middleman-core/sitemap.rb @@ -38,6 +38,9 @@ module Middleman # Sitemap instance methods module InstanceMethods + def current_path + @current_locs[:current_path] + end # Get the resource object for the current path # @return [Middleman::Sitemap::Resource] diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 0a2cb502..a02485c6 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -135,7 +135,7 @@ module Middleman blocks = Array(md[:blocks]).dup blocks << block if block_given? - app.current_path ||= self.destination_path + locs[:current_path] ||= self.destination_path # Certain output file types don't use layouts if !opts.has_key?(:layout) diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index d26e4667..70c97195 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -4,7 +4,7 @@ require 'middleman-core/template_renderer' module Middleman class TemplateContext attr_reader :app - attr_accessor :current_engine, :current_path + attr_accessor :current_engine delegate :config, :logger, :sitemap, :build?, :development?, :data, :extensions, :source_dir, :root, :to => :app