From bea737eb5a5f51ee7fbc2e60d870cee5936bdfb0 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 22 Nov 2005 08:37:04 +0000 Subject: Make ActionController's render honor the :locals option when rendering a :file. Closes #1665. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3157 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 10 ++++--- actionpack/test/controller/new_render_test.rb | 31 ++++++++++++++++++++++ .../test/fixtures/test/render_file_with_ivar.rhtml | 1 + .../fixtures/test/render_file_with_locals.rhtml | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/render_file_with_ivar.rhtml create mode 100644 actionpack/test/fixtures/test/render_file_with_locals.rhtml diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index db41b582cc..a4a7a605c8 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Make ActionController's render honor the :locals option when rendering a :file. #1665. [Emanuel Borsboom, Marcel Molina Jr.] + * Allow assert_tag(:conditions) to match the empty string when a tag has no children. Closes #2959. [Jamis Buck] * Update html-scanner to handle CDATA sections better. Closes #2970. [Jamis Buck] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 225a61f43e..9aab2c5028 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -459,6 +459,10 @@ module ActionController #:nodoc: self.class.controller_name end + def session_enabled? + request.session_options[:disabled] != false + end + protected # Renders the content that will be returned to the browser as the response body. # @@ -596,7 +600,7 @@ module ActionController #:nodoc: else if file = options[:file] - render_file(file, options[:status], options[:use_full_path]) + render_file(file, options[:status], options[:use_full_path], options[:locals] || {}) elsif template = options[:template] render_file(template, options[:status], true) @@ -645,11 +649,11 @@ module ActionController #:nodoc: end end - def render_file(template_path, status = nil, use_full_path = false) + def render_file(template_path, status = nil, use_full_path = false, locals = {}) add_variables_to_assigns assert_existance_of_template_file(template_path) if use_full_path logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger - render_text(@template.render_file(template_path, use_full_path), status) + render_text(@template.render_file(template_path, use_full_path, locals), status) end def render_template(template, status = nil, type = :rhtml, local_assigns = {}) diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 00458b698a..a1ed9f71d0 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -43,6 +43,22 @@ class NewRenderTestController < ActionController::Base def render_custom_code render :text => "hello world", :status => "404 Moved" end + + def render_file_with_instance_variables + @secret = 'in the sauce' + path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.rhtml') + render :file => path + end + + def render_file_with_locals + path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.rhtml') + render :file => path, :locals => {:secret => 'in the sauce'} + end + + def render_file_not_using_full_path + @secret = 'in the sauce' + render :file => 'test/render_file_with_ivar', :use_full_path => true + end def render_xml_hello @name = "David" @@ -257,6 +273,21 @@ class NewRenderTest < Test::Unit::TestCase assert_response :missing end + def test_render_file_with_instance_variables + get :render_file_with_instance_variables + assert_equal "The secret is in the sauce\n", @response.body + end + + def test_render_file_not_using_full_path + get :render_file_not_using_full_path + assert_equal "The secret is in the sauce\n", @response.body + end + + def test_render_file_with_locals + get :render_file_with_locals + assert_equal "The secret is in the sauce\n", @response.body + end + def test_attempt_to_access_object_method assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone } end diff --git a/actionpack/test/fixtures/test/render_file_with_ivar.rhtml b/actionpack/test/fixtures/test/render_file_with_ivar.rhtml new file mode 100644 index 0000000000..8b8a449236 --- /dev/null +++ b/actionpack/test/fixtures/test/render_file_with_ivar.rhtml @@ -0,0 +1 @@ +The secret is <%= @secret %> diff --git a/actionpack/test/fixtures/test/render_file_with_locals.rhtml b/actionpack/test/fixtures/test/render_file_with_locals.rhtml new file mode 100644 index 0000000000..ebe09faee6 --- /dev/null +++ b/actionpack/test/fixtures/test/render_file_with_locals.rhtml @@ -0,0 +1 @@ +The secret is <%= secret %> -- cgit v1.2.3