Class: ActionView::Template

Inherits:
Object
  • Object
show all
Extended by:
Handlers, ActiveSupport::Autoload
Defined in:
lib/action_view/template.rb,
lib/action_view/template/text.rb,
lib/action_view/template/error.rb,
lib/action_view/template/handler.rb,
lib/action_view/template/handlers.rb,
lib/action_view/template/handlers/erb.rb

Overview

Action View Template Handlers

Defined Under Namespace

Modules: Handlers Classes: Error, Handler, Text

Constant Summary collapse

Finalizer =
proc do |method_name, mod|
  proc do
    mod.module_eval do
      remove_possible_method method_name
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Handlers

extended, extensions, handler_class_for_extension, register_default_template_handler, register_template_handler, registered_template_handler, template_handler_extensions

Constructor Details

#initialize(source, identifier, handler, details) ⇒ Template

Returns a new instance of Template.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/action_view/template.rb', line 112

def initialize(source, identifier, handler, details)
  @source             = source
  @identifier         = identifier
  @handler            = handler
  @original_encoding  = nil
  @method_names       = {}

  format   = details[:format] || :html
  @formats = Array.wrap(format).map { |f| f.is_a?(Mime::Type) ? f.ref : f }
  @virtual_path = details[:virtual_path].try(:sub, ".#{format}", "")
end

Instance Attribute Details

#formatsObject (readonly)

Returns the value of attribute formats.



101
102
103
# File 'lib/action_view/template.rb', line 101

def formats
  @formats
end

#handlerObject (readonly)

Returns the value of attribute handler.



101
102
103
# File 'lib/action_view/template.rb', line 101

def handler
  @handler
end

#identifierObject (readonly)

Returns the value of attribute identifier.



101
102
103
# File 'lib/action_view/template.rb', line 101

def identifier
  @identifier
end

#original_encodingObject (readonly)

Returns the value of attribute original_encoding.



101
102
103
# File 'lib/action_view/template.rb', line 101

def original_encoding
  @original_encoding
end

#sourceObject (readonly)

Returns the value of attribute source.



101
102
103
# File 'lib/action_view/template.rb', line 101

def source
  @source
end

#virtual_pathObject (readonly)

Returns the value of attribute virtual_path.



101
102
103
# File 'lib/action_view/template.rb', line 101

def virtual_path
  @virtual_path
end

Instance Method Details

#counter_nameObject



154
155
156
# File 'lib/action_view/template.rb', line 154

def counter_name
  @counter_name ||= "#{variable_name}_counter".to_sym
end

#inspectObject



158
159
160
161
162
163
164
165
# File 'lib/action_view/template.rb', line 158

def inspect
  @inspect ||=
    if defined?(Rails.root)
      identifier.sub("#{Rails.root}/", '')
    else
      identifier
    end
end

#mime_typeObject



146
147
148
# File 'lib/action_view/template.rb', line 146

def mime_type
  @mime_type ||= Mime::Type.lookup_by_extension(@formats.first.to_s) if @formats.first
end

#render(view, locals, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/action_view/template.rb', line 124

def render(view, locals, &block)
  # Notice that we use a bang in this instrumentation because you don't want to
  # consume this in production. This is only slow if it's being listened to.
  ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do
    if view.is_a?(ActionView::CompiledTemplates)
      mod = ActionView::CompiledTemplates
    else
      mod = view.singleton_class
    end

    method_name = compile(locals, view, mod)
    view.send(method_name, locals, &block)
  end
rescue Exception => e
  if e.is_a?(Template::Error)
    e.sub_template_of(self)
    raise e
  else
    raise Template::Error.new(self, view.respond_to?(:assigns) ? view.assigns : {}, e)
  end
end

#variable_nameObject



150
151
152
# File 'lib/action_view/template.rb', line 150

def variable_name
  @variable_name ||= @virtual_path[%r'_?(\w+)(\.\w+)*$', 1].to_sym
end