aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/base.rb9
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb32
-rw-r--r--actionpack/lib/action_view/helpers/capture_helper.rb22
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb5
-rw-r--r--actionpack/lib/action_view/render/partials.rb26
-rw-r--r--actionpack/lib/action_view/render/rendering.rb6
-rw-r--r--actionpack/lib/action_view/template/handlers/builder.rb3
-rw-r--r--actionpack/lib/action_view/template/handlers/erb.rb3
8 files changed, 64 insertions, 42 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index b994c7b141..45184f58fb 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -230,7 +230,7 @@ module ActionView #:nodoc:
def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil, formats = nil)#:nodoc:
@formats = formats || [:html]
- @assigns = assigns_for_first_render
+ @assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
@controller = controller
@helpers = ProxyModule.new(self)
@_content_for = Hash.new {|h,k| h[k] = "" }
@@ -245,6 +245,7 @@ module ActionView #:nodoc:
end
def with_template(current_template)
+ _evaluate_assigns_and_ivars
last_template, self.template = template, current_template
last_formats, self.formats = formats, [current_template.mime_type.to_sym] + Mime::SET.symbols
yield
@@ -260,10 +261,7 @@ module ActionView #:nodoc:
# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #:nodoc:
- return if @assigns_added
- @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
- _copy_ivars_from_controller
- @assigns_added = true
+ @assigns_added ||= _copy_ivars_from_controller
end
private
@@ -274,6 +272,7 @@ module ActionView #:nodoc:
variables -= @controller.protected_instance_variables if @controller.respond_to?(:protected_instance_variables)
variables.each { |name| instance_variable_set(name, @controller.instance_variable_get(name)) }
end
+ true
end
end
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index babb9db38a..14cdc7a025 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -1,6 +1,7 @@
require 'cgi'
require 'action_view/helpers/url_helper'
require 'action_view/helpers/tag_helper'
+require 'active_support/core_ext/file'
module ActionView
module Helpers #:nodoc:
@@ -285,7 +286,7 @@ module ActionView
end
javascript_src_tag(joined_javascript_name, options)
else
- expand_javascript_sources(sources, recursive).collect { |source| javascript_src_tag(source, options) }.join("\n")
+ ensure_javascript_sources!(expand_javascript_sources(sources, recursive)).collect { |source| javascript_src_tag(source, options) }.join("\n")
end
end
@@ -434,7 +435,7 @@ module ActionView
end
stylesheet_tag(joined_stylesheet_name, options)
else
- expand_stylesheet_sources(sources, recursive).collect { |source| stylesheet_tag(source, options) }.join("\n")
+ ensure_stylesheet_sources!(expand_stylesheet_sources(sources, recursive)).collect { |source| stylesheet_tag(source, options) }.join("\n")
end
end
@@ -664,13 +665,28 @@ module ActionView
end
end
+ def ensure_stylesheet_sources!(sources)
+ sources.each do |source|
+ asset_file_path!(path_to_stylesheet(source))
+ end
+ return sources
+ end
+
+ def ensure_javascript_sources!(sources)
+ sources.each do |source|
+ asset_file_path!(path_to_javascript(source))
+ end
+ return sources
+ end
+
def join_asset_file_contents(paths)
- paths.collect { |path| File.read(asset_file_path(path)) }.join("\n\n")
+ paths.collect { |path| File.read(asset_file_path!(path)) }.join("\n\n")
end
def write_asset_file_contents(joined_asset_path, asset_paths)
+
FileUtils.mkdir_p(File.dirname(joined_asset_path))
- File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }
+ File.atomic_write(joined_asset_path) { |cache| cache.write(join_asset_file_contents(asset_paths)) }
# Set mtime to the latest of the combined files to allow for
# consistent ETag without a shared filesystem.
@@ -682,6 +698,14 @@ module ActionView
File.join(ASSETS_DIR, path.split('?').first)
end
+ def asset_file_path!(path)
+ unless path =~ %r{^[-a-z]+://}
+ absolute_path = asset_file_path(path)
+ raise(Errno::ENOENT, "Asset file not found at '#{absolute_path}'" ) unless File.exist?(absolute_path)
+ return absolute_path
+ end
+ end
+
def collect_asset_files(*path)
dir = path.first
diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb
index a8b5a9dbb9..c90acc5ac2 100644
--- a/actionpack/lib/action_view/helpers/capture_helper.rb
+++ b/actionpack/lib/action_view/helpers/capture_helper.rb
@@ -117,6 +117,28 @@ module ActionView
@_content_for[name]
end
+ # content_for? simply checks whether any content has been captured yet using content_for
+ # Useful to render parts of your layout differently based on what is in your views.
+ #
+ # ==== Examples
+ #
+ # Perhaps you will use different css in you layout if no content_for :right_column
+ #
+ # <%# This is the layout %>
+ # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ # <head>
+ # <title>My Website</title>
+ # <%= yield :script %>
+ # </head>
+ # <body class="<%= content_for?(:right_col) ? 'one-column' : 'two-column' %>">
+ # <%= yield %>
+ # <%= yield :right_col %>
+ # </body>
+ # </html>
+ def content_for?(name)
+ @_content_for[name].present?
+ end
+
# Use an alternate output buffer for the duration of the block.
# Defaults to a new empty string.
def with_output_buffer(buf = nil) #:nodoc:
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 8ab78e7bc6..ca6ba501e2 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -231,6 +231,8 @@ module ActionView
# * <tt>:rows</tt> - Specify the number of rows in the textarea
# * <tt>:cols</tt> - Specify the number of columns in the textarea
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
+ # * <tt>:escape</tt> - By default, the contents of the text input are HTML escaped.
+ # If you need unescaped contents, set this to false.
# * Any other key creates standard HTML attributes for the tag.
#
# ==== Examples
@@ -258,6 +260,9 @@ module ActionView
options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
end
+ escape = options.key?("escape") ? options.delete("escape") : true
+ content = html_escape(content) if escape
+
content_tag :textarea, content, { "name" => name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
end
diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb
index 87314fff67..a80ffe3c20 100644
--- a/actionpack/lib/action_view/render/partials.rb
+++ b/actionpack/lib/action_view/render/partials.rb
@@ -232,18 +232,6 @@ module ActionView
ensure
@_proc_for_layout = nil
end
-
- def _deprecated_ivar_assign(template)
- if respond_to?(:controller)
- ivar = :"@#{template.variable_name}"
- object =
- if controller.instance_variable_defined?(ivar)
- ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
- controller.instance_variable_get(ivar),
- "#{ivar} will no longer be implicitly assigned to #{template.variable_name}")
- end
- end
- end
def _render_partial_with_layout(layout, options)
if layout
@@ -253,18 +241,6 @@ module ActionView
content = _render_partial(options)
return _render_content_with_layout(content, layout, options[:locals])
end
-
- def _deprecated_ivar_assign(template)
- if respond_to?(:controller)
- ivar = :"@#{template.variable_name}"
- object =
- if controller.instance_variable_defined?(ivar)
- ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
- controller.instance_variable_get(ivar),
- "#{ivar} will no longer be implicitly assigned to #{template.variable_name}")
- end
- end
- end
def _array_like_objects
array_like = [Array]
@@ -290,8 +266,6 @@ module ActionView
end
def _set_locals(object, locals, template, options)
- object ||= _deprecated_ivar_assign(template)
-
locals[:object] = locals[template.variable_name] = object
locals[options[:as]] = object if options[:as]
end
diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb
index 588a64a652..162e38c484 100644
--- a/actionpack/lib/action_view/render/rendering.rb
+++ b/actionpack/lib/action_view/render/rendering.rb
@@ -84,7 +84,7 @@ module ActionView
# ==== Example
#
# # The template
- # <% render :layout => "my_layout" do |name| %>Hello <%= customer.name %><% end %>
+ # <% render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>
#
# # The layout
# <html><% yield Struct.new(:name).new("David") %></html>
@@ -99,14 +99,12 @@ module ActionView
def _render_template(template, local_assigns = {})
with_template(template) do
- _evaluate_assigns_and_ivars
-
template.render(self, local_assigns) do |*names|
capture(*names, &layout_proc(names.first))
end
end
rescue Exception => e
- if TemplateError === e
+ if e.is_a?(TemplateError)
e.sub_template_of(template)
raise e
else
diff --git a/actionpack/lib/action_view/template/handlers/builder.rb b/actionpack/lib/action_view/template/handlers/builder.rb
index abe140af0b..5f381f7bf0 100644
--- a/actionpack/lib/action_view/template/handlers/builder.rb
+++ b/actionpack/lib/action_view/template/handlers/builder.rb
@@ -1,5 +1,3 @@
-require 'builder'
-
module ActionView
module TemplateHandlers
class Builder < TemplateHandler
@@ -8,6 +6,7 @@ module ActionView
self.default_format = Mime::XML
def compile(template)
+ require 'builder'
"xml = ::Builder::XmlMarkup.new(:indent => 2);" +
"self.output_buffer = xml.target!;" +
template.source +
diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb
index 21272ef089..e3a7d96941 100644
--- a/actionpack/lib/action_view/template/handlers/erb.rb
+++ b/actionpack/lib/action_view/template/handlers/erb.rb
@@ -1,4 +1,3 @@
-require 'erb'
require 'active_support/core_ext/class/attribute_accessors'
module ActionView
@@ -16,6 +15,8 @@ module ActionView
self.default_format = Mime::HTML
def compile(template)
+ require 'erb'
+
magic = $1 if template.source =~ /\A(<%#.*coding:\s*(\S+)\s*-?%>)/
erb = "#{magic}<% __in_erb_template=true %>#{template.source}"
::ERB.new(erb, nil, erb_trim_mode, '@output_buffer').src