From 39ba2da82bcc2f9fad494e6ac0a66a3387ab8ee2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 5 Jul 2008 16:27:43 -0500 Subject: Moved complied method name logic into Renderable --- actionpack/lib/action_view/inline_template.rb | 10 +++++++++ actionpack/lib/action_view/renderable.rb | 4 ++++ actionpack/lib/action_view/template.rb | 9 +++++++- .../action_view/template_handlers/compilable.rb | 24 ++-------------------- 4 files changed, 24 insertions(+), 23 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb index 49147901a1..fb5e4408db 100644 --- a/actionpack/lib/action_view/inline_template.rb +++ b/actionpack/lib/action_view/inline_template.rb @@ -2,6 +2,10 @@ module ActionView #:nodoc: class InlineTemplate #:nodoc: include Renderable + # Count the number of inline templates + cattr_accessor :inline_template_count + @@inline_template_count = 0 + def initialize(view, source, locals = {}, type = nil) @view = view @@ -12,5 +16,11 @@ module ActionView #:nodoc: @method_key = @source @handler = Template.handler_class_for_extension(@extension).new(@view) end + + private + # FIXME: Modifying this shared variable may not thread safe + def method_name_path_segment + "inline_#{@@inline_template_count += 1}" + end end end diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/renderable.rb index 61bd8140fb..d73dc3e2dc 100644 --- a/actionpack/lib/action_view/renderable.rb +++ b/actionpack/lib/action_view/renderable.rb @@ -11,6 +11,10 @@ module ActionView @handler.render(self) end + def method_name + ['_run', @extension, method_name_path_segment].compact.join('_').to_sym + end + private def prepare! unless @prepared diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index f696ea366d..56d740f478 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -37,7 +37,7 @@ module ActionView #:nodoc: end def source - @source ||= File.read(self.filename) + @source ||= File.read(@filename) end def base_path_for_exception @@ -71,5 +71,12 @@ module ActionView #:nodoc: template_type = (@original_path =~ /layouts/i) ? 'layout' : 'template' raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}" end + + def method_name_path_segment + s = File.expand_path(@filename) + s.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT) + s.gsub!(/([^a-zA-Z0-9_])/) { $1.ord } + s + end end end diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb index f436ebbe45..d95a5c8419 100644 --- a/actionpack/lib/action_view/template_handlers/compilable.rb +++ b/actionpack/lib/action_view/template_handlers/compilable.rb @@ -1,7 +1,6 @@ module ActionView module TemplateHandlers module Compilable - def self.included(base) base.extend ClassMethod @@ -12,10 +11,6 @@ module ActionView # Map method names to the names passed in local assigns so far base.cattr_accessor :template_args base.template_args = {} - - # Count the number of inline templates - base.cattr_accessor :inline_template_count - base.inline_template_count = 0 end module ClassMethod @@ -24,7 +19,7 @@ module ActionView true end end - + def render(template) @view.send :execute, template end @@ -75,22 +70,7 @@ module ActionView end def assign_method_name(template) - @view.method_names[template.method_key] ||= compiled_method_name(template) - end - - def compiled_method_name(template) - ['_run', self.class.to_s.demodulize.underscore, compiled_method_name_file_path_segment(template.filename)].compact.join('_').to_sym - end - - def compiled_method_name_file_path_segment(file_name) - if file_name - s = File.expand_path(file_name) - s.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT) - s.gsub!(/([^a-zA-Z0-9_])/) { $1.ord } - s - else - (self.inline_template_count += 1).to_s - end + @view.method_names[template.method_key] ||= template.method_name end # Method to create the source code for a given template. -- cgit v1.2.3