From 6f5327013d6353c50cadfe2160d1b526ad687633 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 25 Jun 2008 05:47:01 -0500 Subject: Consolidate CustomHandlerTest, TemplateFileTest, and TemplateObjectTest and test them at a higher level of abstraction in ViewRenderTest. --- actionpack/lib/action_view/view_load_paths.rb | 10 +- actionpack/test/controller/custom_handler_test.rb | 45 -------- actionpack/test/controller/view_paths_test.rb | 30 +++--- actionpack/test/template/render_test.rb | 124 ++++++++++++++++++++++ actionpack/test/template/template_file_test.rb | 95 ----------------- actionpack/test/template/template_object_test.rb | 92 ---------------- 6 files changed, 142 insertions(+), 254 deletions(-) delete mode 100644 actionpack/test/controller/custom_handler_test.rb create mode 100644 actionpack/test/template/render_test.rb delete mode 100644 actionpack/test/template/template_file_test.rb delete mode 100644 actionpack/test/template/template_object_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_view/view_load_paths.rb b/actionpack/lib/action_view/view_load_paths.rb index e873d96aa0..f23ac665f1 100644 --- a/actionpack/lib/action_view/view_load_paths.rb +++ b/actionpack/lib/action_view/view_load_paths.rb @@ -6,19 +6,15 @@ module ActionView #:nodoc: class LoadPath #:nodoc: attr_reader :path, :paths - delegate :to_s, :inspect, :to => :path + delegate :to_s, :to_str, :inspect, :to => :path def initialize(path) @path = path.freeze reload! end - def eql?(view_path) - view_path.is_a?(ViewPath) && @path == view_path.path - end - - def hash - @path.hash + def ==(path) + to_str == path.to_str end # Rebuild load path directory cache diff --git a/actionpack/test/controller/custom_handler_test.rb b/actionpack/test/controller/custom_handler_test.rb deleted file mode 100644 index ac484ae17e..0000000000 --- a/actionpack/test/controller/custom_handler_test.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'abstract_unit' - -class CustomHandler < ActionView::TemplateHandler - def initialize( view ) - @view = view - end - - def render( template ) - [ template.source, - template.locals, - @view ] - end -end - -class CustomHandlerTest < Test::Unit::TestCase - def setup - ActionView::Template.register_template_handler "foo", CustomHandler - ActionView::Template.register_template_handler :foo2, CustomHandler - @view = ActionView::Base.new - end - - def test_custom_render - template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo") - - result = @view.render_template(template) - assert_equal( - [ "hello <%= one %>", { :one => "two" }, @view ], - result ) - end - - def test_custom_render2 - template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo2") - result = @view.render_template(template) - assert_equal( - [ "hello <%= one %>", { :one => "two" }, @view ], - result ) - end - - def test_unhandled_extension - # uses the ERb handler by default if the extension isn't recognized - template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "bar") - result = @view.render_template(template) - assert_equal "hello two", result - end -end diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb index 1b4c1fae2f..8203061028 100644 --- a/actionpack/test/controller/view_paths_test.rb +++ b/actionpack/test/controller/view_paths_test.rb @@ -47,35 +47,35 @@ class ViewLoadPathsTest < Test::Unit::TestCase end def test_template_load_path_was_set_correctly - assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths.map(&:to_s) + assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths end def test_controller_appends_view_path_correctly @controller.append_view_path 'foo' - assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths @controller.append_view_path(%w(bar baz)) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths @controller.append_view_path(LOAD_PATH_ROOT) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths @controller.append_view_path([LOAD_PATH_ROOT]) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths end def test_controller_prepends_view_path_correctly @controller.prepend_view_path 'baz' - assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths @controller.prepend_view_path(LOAD_PATH_ROOT) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths @controller.prepend_view_path([LOAD_PATH_ROOT]) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths end def test_template_appends_view_path_correctly @@ -83,10 +83,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase class_view_paths = TestController.view_paths @controller.append_view_path 'foo' - assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths @controller.append_view_path(%w(bar baz)) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths assert_equal class_view_paths, TestController.view_paths end @@ -95,10 +95,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase class_view_paths = TestController.view_paths @controller.prepend_view_path 'baz' - assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths.map(&:to_s) + assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths assert_equal class_view_paths, TestController.view_paths end @@ -140,13 +140,13 @@ class ViewLoadPathsTest < Test::Unit::TestCase A.view_paths = ['a/path'] - assert_equal ['a/path'], A.view_paths.map(&:to_s) + assert_equal ['a/path'], A.view_paths assert_equal A.view_paths, B.view_paths assert_equal original_load_paths, C.view_paths C.view_paths = [] assert_nothing_raised { C.view_paths << 'c/path' } - assert_equal ['c/path'], C.view_paths.map(&:to_s) + assert_equal ['c/path'], C.view_paths end def test_find_template_file_for_path diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb new file mode 100644 index 0000000000..bdabe07121 --- /dev/null +++ b/actionpack/test/template/render_test.rb @@ -0,0 +1,124 @@ +require 'abstract_unit' +require 'controller/fake_models' + +class ViewRenderTest < Test::Unit::TestCase + FIXTURE_LOAD_PATHS = ActionView::ViewLoadPaths.new([File.join(File.dirname(__FILE__), '..', 'fixtures')]) + + def setup + @assigns = { :secret => 'in the sauce' } + @view = ActionView::Base.new(FIXTURE_LOAD_PATHS, @assigns) + end + + def test_render_file + assert_equal "Hello world!", @view.render("test/hello_world.erb") + end + + def test_render_file_not_using_full_path + assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb", :use_full_path => true) + end + + def test_render_file_without_specific_extension + assert_equal "Hello world!", @view.render("test/hello_world") + end + + def test_render_file_with_full_path + template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb') + assert_equal "Hello world!", @view.render(:file => template_path, :use_full_path => false) + end + + def test_render_file_with_instance_variables + assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_ivar.erb") + end + + def test_render_file_with_locals + locals = { :secret => 'in the sauce' } + assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_locals.erb", locals) + end + + def test_render_file_not_using_full_path_with_dot_in_path + assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar") + end + + def test_render_update + # TODO: You should not have to stub out template because template is self! + @view.instance_variable_set(:@template, @view) + assert_equal 'alert("Hello, World!");', @view.render(:update) { |page| page.alert('Hello, World!') } + end + + def test_render_partial + assert_equal "only partial", @view.render(:partial => "test/partial_only") + end + + def test_render_partial_with_errors + assert_raise(ActionView::TemplateError) { @view.render(:partial => "test/raise") } + end + + def test_render_partial_collection + assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ]) + end + + # TODO: The reason for this test is unclear, improve documentation + def test_render_partial_and_fallback_to_layout + assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) + end + + # TODO: The reason for this test is unclear, improve documentation + def test_render_js_partial_and_fallback_to_erb_layout + @view.template_format = :js + assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) + end + + # TODO: The reason for this test is unclear, improve documentation + def test_render_missing_xml_partial_and_raise_missing_template + @view.template_format = :xml + assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") } + end + + def test_render_inline + assert_equal "Hello, World!", @view.render(:inline => "Hello, World!") + end + + def test_render_inline_with_locals + assert_equal "Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }) + end + + def test_render_fallbacks_to_erb_for_unknown_types + assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :foo) + end + + class CustomHandler < ActionView::TemplateHandler + def render(template) + [template.source, template.locals].inspect + end + end + + def test_render_inline_with_custom_type + ActionView::Template.register_template_handler :foo, CustomHandler + assert_equal '["Hello, World!", {}]', @view.render(:inline => "Hello, World!", :type => :foo) + end + + def test_render_inline_with_locals_and_custom_type + ActionView::Template.register_template_handler :foo, CustomHandler + assert_equal '["Hello, <%= name %>!", {:name=>"Josh"}]', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) + end + + class CompilableCustomHandler < ActionView::TemplateHandler + include ActionView::TemplateHandlers::Compilable + + def compile(template) + "@output_buffer = ''\n" + + "@output_buffer << 'locals: #{template.locals.inspect}, '\n" + + "@output_buffer << 'source: #{template.source.inspect}'\n" + end + end + + def test_render_inline_with_compilable_custom_type + ActionView::Template.register_template_handler :foo, CompilableCustomHandler + assert_equal 'locals: {}, source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo) + end + + def test_render_inline_with_locals_and_compilable_custom_type + ActionView::Template.register_template_handler :foo, CompilableCustomHandler + assert_equal 'locals: {:name=>"Josh"}, source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) + end +end diff --git a/actionpack/test/template/template_file_test.rb b/actionpack/test/template/template_file_test.rb deleted file mode 100644 index d14a966c1c..0000000000 --- a/actionpack/test/template/template_file_test.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'abstract_unit' - -class TemplateFileTest < Test::Unit::TestCase - LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') - - def setup - @template = ActionView::TemplateFile.new("test/hello_world.html.erb") - @another_template = ActionView::TemplateFile.new("test/hello_world.erb") - @file_only = ActionView::TemplateFile.new("hello_world.erb") - @full_path = ActionView::TemplateFile.new("/u/app/scales/config/../app/views/test/hello_world.erb", true) - @layout = ActionView::TemplateFile.new("layouts/hello") - @multipart = ActionView::TemplateFile.new("test_mailer/implicitly_multipart_example.text.html.erb") - end - - def test_path - assert_equal "test/hello_world.html.erb", @template.path - assert_equal "test/hello_world.erb", @another_template.path - assert_equal "hello_world.erb", @file_only.path - assert_equal "/u/app/scales/config/../app/views/test/hello_world.erb", @full_path.path - assert_equal "layouts/hello", @layout.path - assert_equal "test_mailer/implicitly_multipart_example.text.html.erb", @multipart.path - end - - def test_path_without_extension - assert_equal "test/hello_world.html", @template.path_without_extension - assert_equal "test/hello_world", @another_template.path_without_extension - assert_equal "hello_world", @file_only.path_without_extension - assert_equal "layouts/hello", @layout.path_without_extension - assert_equal "test_mailer/implicitly_multipart_example.text.html", @multipart.path_without_extension - end - - def test_path_without_format_and_extension - assert_equal "test/hello_world", @template.path_without_format_and_extension - assert_equal "test/hello_world", @another_template.path_without_format_and_extension - assert_equal "hello_world", @file_only.path_without_format_and_extension - assert_equal "layouts/hello", @layout.path_without_format_and_extension - assert_equal "test_mailer/implicitly_multipart_example", @multipart.path_without_format_and_extension - end - - def test_name - assert_equal "hello_world", @template.name - assert_equal "hello_world", @another_template.name - assert_equal "hello_world", @file_only.name - assert_equal "hello_world", @full_path.name - assert_equal "hello", @layout.name - assert_equal "implicitly_multipart_example", @multipart.name - end - - def test_format - assert_equal "html", @template.format - assert_equal nil, @another_template.format - assert_equal nil, @layout.format - assert_equal "text.html", @multipart.format - end - - def test_extension - assert_equal "erb", @template.extension - assert_equal "erb", @another_template.extension - assert_equal nil, @layout.extension - assert_equal "erb", @multipart.extension - end - - def test_format_and_extension - assert_equal "html.erb", @template.format_and_extension - assert_equal "erb", @another_template.format_and_extension - assert_equal nil, @layout.format_and_extension - assert_equal "text.html.erb", @multipart.format_and_extension - end - - def test_new_file_with_extension - file = @template.dup_with_extension(:haml) - assert_equal "test/hello_world.html", file.path_without_extension - assert_equal "haml", file.extension - assert_equal "test/hello_world.html.haml", file.path - - file = @another_template.dup_with_extension(:haml) - assert_equal "test/hello_world", file.path_without_extension - assert_equal "haml", file.extension - assert_equal "test/hello_world.haml", file.path - - file = @another_template.dup_with_extension(nil) - assert_equal "test/hello_world", file.path_without_extension - assert_equal nil, file.extension - assert_equal "test/hello_world", file.path - end - - def test_freezes_entire_contents - @template.freeze - assert @template.frozen? - assert @template.base_path.frozen? - assert @template.name.frozen? - assert @template.format.frozen? - assert @template.extension.frozen? - end -end diff --git a/actionpack/test/template/template_object_test.rb b/actionpack/test/template/template_object_test.rb deleted file mode 100644 index 2cfc4523c6..0000000000 --- a/actionpack/test/template/template_object_test.rb +++ /dev/null @@ -1,92 +0,0 @@ -require 'abstract_unit' - -class TemplateObjectTest < Test::Unit::TestCase - LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') - - class TemplateTest < Test::Unit::TestCase - def setup - @view = ActionView::Base.new(LOAD_PATH_ROOT) - @path = "test/hello_world.erb" - end - - def test_should_create_valid_template - template = ActionView::Template.new(@view, @path, true) - - assert_kind_of ActionView::TemplateHandlers::ERB, template.handler - assert_equal "test/hello_world.erb", template.path.to_s - assert_nil template.instance_variable_get(:"@source") - assert_equal "erb", template.extension - end - - uses_mocha 'Template preparation tests' do - def test_should_prepare_template_properly - template = ActionView::Template.new(@view, @path, true) - view = template.instance_variable_get(:"@view") - - view.expects(:evaluate_assigns) - template.handler.expects(:compile_template).with(template) - view.expects(:method_names).returns({}) - - template.prepare! - end - end - end - - class PartialTemplateTest < Test::Unit::TestCase - def setup - @view = ActionView::Base.new(LOAD_PATH_ROOT) - @path = "test/partial_only" - end - - def test_should_create_valid_partial_template - template = ActionView::PartialTemplate.new(@view, @path, nil) - - assert_equal "test/_partial_only", template.path.path_without_format_and_extension - assert_equal :partial_only, template.variable_name - - assert template.locals.has_key?(:object) - assert template.locals.has_key?(:partial_only) - end - - def test_partial_with_errors - template = ActionView::PartialTemplate.new(@view, 'test/raise', nil) - assert_raise(ActionView::TemplateError) { template.render_template } - end - - uses_mocha 'Partial template preparation tests' do - def test_should_prepare_on_initialization - ActionView::PartialTemplate.any_instance.expects(:prepare!) - template = ActionView::PartialTemplate.new(@view, @path, 1) - end - end - end - - class PartialTemplateFallbackTest < Test::Unit::TestCase - def setup - @view = ActionView::Base.new(LOAD_PATH_ROOT) - @path = 'test/layout_for_partial' - end - - def test_default - template = ActionView::PartialTemplate.new(@view, @path, nil) - assert_equal 'test/_layout_for_partial', template.path.path_without_format_and_extension - assert_equal 'erb', template.extension - assert_equal :html, @view.template_format - end - - def test_js - @view.template_format = :js - template = ActionView::PartialTemplate.new(@view, @path, nil) - assert_equal 'test/_layout_for_partial', template.path.path_without_format_and_extension - assert_equal 'erb', template.extension - assert_equal :html, @view.template_format - end - - def test_xml - @view.template_format = :xml - assert_raise ActionView::MissingTemplate do - ActionView::PartialTemplate.new(@view, @path, nil) - end - end - end -end -- cgit v1.2.3 From ad772402c46a79c2d38979cef754b26dbd868196 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 25 Jun 2008 05:57:24 -0500 Subject: Made ActionView::Base#render_file private --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 2 +- actionpack/lib/action_controller/layout.rb | 2 +- actionpack/lib/action_controller/rescue.rb | 2 +- .../templates/rescues/diagnostics.erb | 4 +-- .../templates/rescues/template_error.erb | 4 +-- actionpack/lib/action_view/base.rb | 40 +++++++++++----------- actionpack/test/fixtures/test/hello.builder | 2 +- 8 files changed, 30 insertions(+), 28 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index a28bf66b88..d32f7a298f 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Made ActionView::Base#render_file private [Josh Peek] + * Fix polymorphic_url with singleton resources. #461 [Tammer Saleh] * Replaced TemplateFinder abstraction with ViewLoadPaths [Josh Peek] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index bf34edcd85..c50c55dd05 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -1095,7 +1095,7 @@ module ActionController #:nodoc: def render_for_file(template_path, status = nil, use_full_path = false, locals = {}) #:nodoc: add_variables_to_assigns logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger - render_for_text(@template.render_file(template_path, use_full_path, locals), status) + render_for_text(@template.render(:file => template_path, :use_full_path => use_full_path, :locals => locals), status) end def render_for_text(text = nil, status = nil, append_response = false) #:nodoc: diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index 0721f71498..d0c717ff67 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -254,7 +254,7 @@ module ActionController #:nodoc: @template.instance_variable_set("@content_for_layout", content_for_layout) response.layout = layout status = template_with_options ? options[:status] : nil - render_for_text(@template.render_file(layout, true), status) + render_for_text(@template.render(layout), status) else render_with_no_layout(options, extra_options, &block) end diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/rescue.rb index 40ef4ea044..163ed87fbb 100644 --- a/actionpack/lib/action_controller/rescue.rb +++ b/actionpack/lib/action_controller/rescue.rb @@ -178,7 +178,7 @@ module ActionController #:nodoc: @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub"))) @template.send!(:assign_variables_from_controller) - @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false)) + @template.instance_variable_set("@contents", @template.render(:file => template_path_for_local_rescue(exception), :use_full_path => false)) response.content_type = Mime::HTML render_for_file(rescues_path("layout"), response_code_for_rescue(exception)) diff --git a/actionpack/lib/action_controller/templates/rescues/diagnostics.erb b/actionpack/lib/action_controller/templates/rescues/diagnostics.erb index 032f945ed2..385c6c1b09 100644 --- a/actionpack/lib/action_controller/templates/rescues/diagnostics.erb +++ b/actionpack/lib/action_controller/templates/rescues/diagnostics.erb @@ -6,6 +6,6 @@
<%=h @exception.clean_message %>
-<%= render_file(@rescues_path + "/_trace.erb", false) %> +<%= render(:file => @rescues_path + "/_trace.erb", :use_full_path => false) %> -<%= render_file(@rescues_path + "/_request_and_response.erb", false) %> +<%= render(:file => @rescues_path + "/_request_and_response.erb", :use_full_path => false) %> diff --git a/actionpack/lib/action_controller/templates/rescues/template_error.erb b/actionpack/lib/action_controller/templates/rescues/template_error.erb index eda64db3e9..4aecc68d18 100644 --- a/actionpack/lib/action_controller/templates/rescues/template_error.erb +++ b/actionpack/lib/action_controller/templates/rescues/template_error.erb @@ -15,7 +15,7 @@ <% @real_exception = @exception @exception = @exception.original_exception || @exception %> -<%= render_file(@rescues_path + "/_trace.erb", false) %> +<%= render(:file => @rescues_path + "/_trace.erb", :use_full_path => false) %> <% @exception = @real_exception %> -<%= render_file(@rescues_path + "/_request_and_response.erb", false) %> +<%= render(:file => @rescues_path + "/_request_and_response.erb", :use_full_path => false) %> diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 197455a933..84f2c45f4e 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -228,26 +228,6 @@ module ActionView #:nodoc: @view_paths = ViewLoadPaths.new(Array(paths)) end - # Renders the template present at template_path. If use_full_path is set to true, - # it's relative to the view_paths array, otherwise it's absolute. The hash in local_assigns - # is made available as local variables. - def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc: - if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) && !template_path.include?("/") - raise ActionViewError, <<-END_ERROR -Due to changes in ActionMailer, you need to provide the mailer_name along with the template name. - - render "user_mailer/signup" - render :file => "user_mailer/signup" - -If you are rendering a subtemplate, you must now use controller-like partial syntax: - - render :partial => 'signup' # no mailer_name necessary - END_ERROR - end - - Template.new(self, template_path, use_full_path, local_assigns).render_template - end - # Renders the template present at template_path (relative to the view_paths array). # The hash in local_assigns is made available as local variables. def render(options = {}, local_assigns = {}, &block) #:nodoc: @@ -323,6 +303,26 @@ If you are rendering a subtemplate, you must now use controller-like partial syn end private + # Renders the template present at template_path. If use_full_path is set to true, + # it's relative to the view_paths array, otherwise it's absolute. The hash in local_assigns + # is made available as local variables. + def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc: + if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) && !template_path.include?("/") + raise ActionViewError, <<-END_ERROR + Due to changes in ActionMailer, you need to provide the mailer_name along with the template name. + + render "user_mailer/signup" + render :file => "user_mailer/signup" + + If you are rendering a subtemplate, you must now use controller-like partial syntax: + + render :partial => 'signup' # no mailer_name necessary + END_ERROR + end + + Template.new(self, template_path, use_full_path, local_assigns).render_template + end + def wrap_content_for_layout(content) original_content_for_layout, @content_for_layout = @content_for_layout, content yield diff --git a/actionpack/test/fixtures/test/hello.builder b/actionpack/test/fixtures/test/hello.builder index 82a4a310d3..86a8bb3d7b 100644 --- a/actionpack/test/fixtures/test/hello.builder +++ b/actionpack/test/fixtures/test/hello.builder @@ -1,4 +1,4 @@ xml.html do xml.p "Hello #{@name}" - xml << render_file("test/greeting") + xml << render("test/greeting") end \ No newline at end of file -- cgit v1.2.3 From a9259ccfe05690dc4cb7993d551603c39619c27f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 25 Jun 2008 06:04:06 -0500 Subject: Hide InlineTemplate class from ActionController and use ActionView's render API --- actionpack/lib/action_controller/base.rb | 3 +-- actionpack/lib/action_view/base.rb | 11 +++++------ 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c50c55dd05..7cc670289d 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -859,8 +859,7 @@ module ActionController #:nodoc: elsif inline = options[:inline] add_variables_to_assigns - tmpl = ActionView::InlineTemplate.new(@template, options[:inline], options[:locals], options[:type]) - render_for_text(@template.render_template(tmpl), options[:status]) + render_for_text(@template.render(options), options[:status]) elsif action_name = options[:action] template = default_template_name(action_name.to_s) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 84f2c45f4e..40b808a61e 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -256,16 +256,11 @@ module ActionView #:nodoc: elsif options[:partial] render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) elsif options[:inline] - template = InlineTemplate.new(self, options[:inline], options[:locals], options[:type]) - render_template(template) + render_inline(options[:inline], options[:locals], options[:type]) end end end - def render_template(template) #:nodoc: - template.render_template - end - # Returns true is the file may be rendered implicitly. def file_public?(template_path)#:nodoc: template_path.split('/').last[0,1] != '_' @@ -323,6 +318,10 @@ module ActionView #:nodoc: Template.new(self, template_path, use_full_path, local_assigns).render_template end + def render_inline(text, local_assigns = {}, type = nil) + InlineTemplate.new(self, text, local_assigns, type).render_template + end + def wrap_content_for_layout(content) original_content_for_layout, @content_for_layout = @content_for_layout, content yield -- cgit v1.2.3 From 339491a6b37722497ebafe9998e17507f47e8fd6 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 25 Jun 2008 15:24:12 -0500 Subject: Set precompiled fixture load path constant to speed up tests --- actionpack/test/abstract_unit.rb | 1 + actionpack/test/active_record_unit.rb | 58 ++++++----- ...nder_partial_with_record_identification_test.rb | 26 ++--- .../test/controller/action_pack_assertions_test.rb | 5 +- .../test/controller/addresses_render_test.rb | 3 +- actionpack/test/controller/caching_test.rb | 5 +- actionpack/test/controller/capture_test.rb | 2 +- actionpack/test/controller/content_type_test.rb | 24 ++--- .../deprecation/deprecated_base_methods_test.rb | 3 +- actionpack/test/controller/mime_responds_test.rb | 53 +++++----- actionpack/test/controller/new_render_test.rb | 109 ++++++++++----------- actionpack/test/controller/render_test.rb | 27 +++-- actionpack/test/controller/send_file_test.rb | 4 +- actionpack/test/controller/view_paths_test.rb | 44 ++++----- actionpack/test/template/render_test.rb | 4 +- actionpack/test/template/url_helper_test.rb | 8 +- 16 files changed, 183 insertions(+), 193 deletions(-) (limited to 'actionpack') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index fa1c3293b4..70f6a28a9c 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -22,6 +22,7 @@ ActiveSupport::Deprecation.debug = true ActionController::Base.logger = nil ActionController::Routing::Routes.reload rescue nil +FIXTURE_LOAD_PATH = ActionView::ViewLoadPaths::LoadPath.new(File.join(File.dirname(__FILE__), 'fixtures')) # Wrap tests that use Mocha and skip if unavailable. def uses_mocha(test_name) diff --git a/actionpack/test/active_record_unit.rb b/actionpack/test/active_record_unit.rb index a7d526850e..a377ccad24 100644 --- a/actionpack/test/active_record_unit.rb +++ b/actionpack/test/active_record_unit.rb @@ -30,7 +30,6 @@ end $stderr.flush - # Define the rest of the connector class ActiveRecordTestConnector class << self @@ -48,46 +47,45 @@ class ActiveRecordTestConnector end private - - def setup_connection - if Object.const_defined?(:ActiveRecord) - defaults = { :database => ':memory:' } - begin - options = defaults.merge :adapter => 'sqlite3', :timeout => 500 - ActiveRecord::Base.establish_connection(options) - ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options } - ActiveRecord::Base.connection - rescue Exception # errors from establishing a connection - $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.' - options = defaults.merge :adapter => 'sqlite' - ActiveRecord::Base.establish_connection(options) - ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options } - ActiveRecord::Base.connection + def setup_connection + if Object.const_defined?(:ActiveRecord) + defaults = { :database => ':memory:' } + begin + options = defaults.merge :adapter => 'sqlite3', :timeout => 500 + ActiveRecord::Base.establish_connection(options) + ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options } + ActiveRecord::Base.connection + rescue Exception # errors from establishing a connection + $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.' + options = defaults.merge :adapter => 'sqlite' + ActiveRecord::Base.establish_connection(options) + ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options } + ActiveRecord::Base.connection + end + + Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE) + else + raise "Can't setup connection since ActiveRecord isn't loaded." end - - Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE) - else - raise "Can't setup connection since ActiveRecord isn't loaded." end - end - # Load actionpack sqlite tables - def load_schema - File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql| - ActiveRecord::Base.connection.execute(sql) unless sql.blank? + # Load actionpack sqlite tables + def load_schema + File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql| + ActiveRecord::Base.connection.execute(sql) unless sql.blank? + end end - end - def require_fixture_models - Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f} - end + def require_fixture_models + Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f} + end end end class ActiveRecordTestCase < ActiveSupport::TestCase # Set our fixture path if ActiveRecordTestConnector.able_to_connect - self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/" + self.fixture_path = [FIXTURE_LOAD_PATH] self.use_transactional_fixtures = false end diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb index ed10e72953..af2725a99b 100644 --- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb +++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb @@ -40,11 +40,12 @@ class RenderPartialWithRecordIdentificationController < ActionController::Base render :partial => @developers end end -RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] + +RenderPartialWithRecordIdentificationController.view_paths = [FIXTURE_LOAD_PATH] class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots - + def setup @controller = RenderPartialWithRecordIdentificationController.new @request = ActionController::TestRequest.new @@ -56,22 +57,22 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase get :render_with_has_many_and_belongs_to_association assert_template 'projects/_project' end - + def test_rendering_partial_with_has_many_association get :render_with_has_many_association assert_template 'replies/_reply' end - + def test_rendering_partial_with_named_scope get :render_with_named_scope assert_template 'replies/_reply' end - + def test_render_with_record get :render_with_record assert_template 'developers/_developer' end - + def test_render_with_record_collection get :render_with_record_collection assert_template 'developers/_developer' @@ -116,7 +117,8 @@ class RenderPartialWithRecordIdentificationController < ActionController::Base render :partial => @developers end end -RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] + +RenderPartialWithRecordIdentificationController.view_paths = [FIXTURE_LOAD_PATH] class Game < Struct.new(:name, :id) def to_param @@ -134,7 +136,8 @@ module Fun render :partial => [ Game.new("Pong"), Game.new("Tank") ] end end - NestedController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] + + NestedController.view_paths = [FIXTURE_LOAD_PATH] module Serious class NestedDeeperController < ActionController::Base @@ -146,7 +149,8 @@ module Fun render :partial => [ Game.new("Chess"), Game.new("Sudoku"), Game.new("Solitaire") ] end end - NestedDeeperController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] + + NestedDeeperController.view_paths = [FIXTURE_LOAD_PATH] end end @@ -167,7 +171,6 @@ class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveReco get :render_with_record_collection_in_nested_controller assert_template 'fun/games/_game' end - end class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase @@ -187,5 +190,4 @@ class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < Acti get :render_with_record_collection_in_deeper_nested_controller assert_template 'fun/serious/games/_game' end - -end \ No newline at end of file +end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index c25e9e1df6..7a90a9408e 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -157,7 +157,7 @@ module Admin def redirect_to_fellow_controller redirect_to :controller => 'user' end - + def redirect_to_top_level_named_route redirect_to top_level_url(:id => "foo") end @@ -170,7 +170,7 @@ end # tell the controller where to find its templates but start from parent # directory of test_request_response to simulate the behaviour of a # production environment -ActionPackAssertionsController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +ActionPackAssertionsController.view_paths = [FIXTURE_LOAD_PATH] # a test case to exercise the new capabilities TestRequest & TestResponse class ActionPackAssertionsControllerTest < Test::Unit::TestCase @@ -533,7 +533,6 @@ class ActionPackHeaderTest < Test::Unit::TestCase assert_equal('application/pdf; charset=utf-8', @response.headers['type']) end - def test_render_text_with_custom_content_type get :render_text_with_custom_content_type assert_equal 'application/rss+xml; charset=utf-8', @response.headers['type'] diff --git a/actionpack/test/controller/addresses_render_test.rb b/actionpack/test/controller/addresses_render_test.rb index a31734203d..df87182082 100644 --- a/actionpack/test/controller/addresses_render_test.rb +++ b/actionpack/test/controller/addresses_render_test.rb @@ -1,7 +1,6 @@ require 'abstract_unit' class Address - def Address.count(conditions = nil, join = nil) nil end @@ -20,7 +19,7 @@ class AddressesTestController < ActionController::Base def self.controller_path; "addresses"; end end -AddressesTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +AddressesTestController.view_paths = [FIXTURE_LOAD_PATH] class AddressesTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 14cf0a86a1..0140654155 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -6,7 +6,7 @@ CACHE_DIR = 'test_cache' FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) ActionController::Base.page_cache_directory = FILE_STORE_PATH ActionController::Base.cache_store = :file_store, FILE_STORE_PATH -ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/' ] +ActionController::Base.view_paths = [FIXTURE_LOAD_PATH] class PageCachingTestController < ActionController::Base caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? } @@ -631,7 +631,7 @@ class FunctionalCachingController < ActionController::Base end end -FunctionalCachingController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +FunctionalCachingController.view_paths = [FIXTURE_LOAD_PATH] class FunctionalFragmentCachingTest < Test::Unit::TestCase def setup @@ -642,6 +642,7 @@ class FunctionalFragmentCachingTest < Test::Unit::TestCase @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end + def test_fragment_caching get :fragment_cached assert_response :success diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index 2604844b84..87f9ce8ab3 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -23,7 +23,7 @@ class CaptureController < ActionController::Base def rescue_action(e) raise end end -CaptureController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +CaptureController.view_paths = [FIXTURE_LOAD_PATH] class CaptureTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index d262ce8103..2019b4a2d0 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -13,12 +13,12 @@ class ContentTypeController < ActionController::Base def render_content_type_from_render render :text => "hello world!", :content_type => Mime::RSS end - + def render_charset_from_body response.charset = "utf-16" render :text => "hello world!" end - + def render_default_for_rhtml end @@ -45,7 +45,7 @@ class ContentTypeController < ActionController::Base def rescue_action(e) raise end end -ContentTypeController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +ContentTypeController.view_paths = [FIXTURE_LOAD_PATH] class ContentTypeTest < Test::Unit::TestCase def setup @@ -68,7 +68,7 @@ class ContentTypeTest < Test::Unit::TestCase def test_render_changed_charset_default ContentTypeController.default_charset = "utf-16" get :render_defaults - assert_equal "utf-16", @response.charset + assert_equal "utf-16", @response.charset assert_equal Mime::HTML, @response.content_type ContentTypeController.default_charset = "utf-8" end @@ -76,13 +76,13 @@ class ContentTypeTest < Test::Unit::TestCase def test_content_type_from_body get :render_content_type_from_body assert_equal "application/rss+xml", @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end def test_content_type_from_render get :render_content_type_from_render assert_equal "application/rss+xml", @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end def test_charset_from_body @@ -94,27 +94,27 @@ class ContentTypeTest < Test::Unit::TestCase def test_default_for_rhtml get :render_default_for_rhtml assert_equal Mime::HTML, @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end def test_default_for_rxml get :render_default_for_rxml assert_equal Mime::XML, @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end def test_default_for_rjs xhr :post, :render_default_for_rjs assert_equal Mime::JS, @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end def test_change_for_rxml get :render_change_for_rxml assert_equal Mime::HTML, @response.content_type - assert_equal "utf-8", @response.charset + assert_equal "utf-8", @response.charset end - + def test_render_default_content_types_for_respond_to @request.env["HTTP_ACCEPT"] = Mime::HTML.to_s get :render_default_content_types_for_respond_to @@ -130,7 +130,7 @@ class ContentTypeTest < Test::Unit::TestCase get :render_default_content_types_for_respond_to assert_equal Mime::XML, @response.content_type end - + def test_render_default_content_types_for_respond_to_with_overwrite @request.env["HTTP_ACCEPT"] = Mime::RSS.to_s get :render_default_content_types_for_respond_to diff --git a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb index 8c1a8954a5..f485500b7f 100644 --- a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb +++ b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb @@ -2,7 +2,6 @@ require 'abstract_unit' class DeprecatedBaseMethodsTest < Test::Unit::TestCase class Target < ActionController::Base - def home_url(greeting) "http://example.com/#{greeting}" end @@ -14,7 +13,7 @@ class DeprecatedBaseMethodsTest < Test::Unit::TestCase def rescue_action(e) raise e end end - Target.view_paths = [ File.dirname(__FILE__) + "/../../fixtures" ] + Target.view_paths = [FIXTURE_LOAD_PATH] def setup @request = ActionController::TestRequest.new diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index c617cb2e84..fb2519563d 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -107,7 +107,7 @@ class RespondToController < ActionController::Base type.any(:js, :xml) { render :text => "Either JS or XML" } end end - + def handle_any_any respond_to do |type| type.html { render :text => 'HTML' } @@ -120,12 +120,12 @@ class RespondToController < ActionController::Base type.html type.js end - end - - def iphone_with_html_response_type + end + + def iphone_with_html_response_type Mime::Type.register_alias("text/html", :iphone) request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone" - + respond_to do |type| type.html { @type = "Firefox" } type.iphone { @type = "iPhone" } @@ -138,7 +138,7 @@ class RespondToController < ActionController::Base def iphone_with_html_response_type_without_layout Mime::Type.register_alias("text/html", :iphone) request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" - + respond_to do |type| type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" } type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" } @@ -162,7 +162,7 @@ class RespondToController < ActionController::Base end end -RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +RespondToController.view_paths = [FIXTURE_LOAD_PATH] class MimeControllerTest < Test::Unit::TestCase def setup @@ -247,7 +247,7 @@ class MimeControllerTest < Test::Unit::TestCase get :just_xml assert_equal 'XML', @response.body end - + def test_using_defaults @request.env["HTTP_ACCEPT"] = "*/*" get :using_defaults @@ -347,12 +347,12 @@ class MimeControllerTest < Test::Unit::TestCase get :handle_any_any assert_equal 'HTML', @response.body end - + def test_handle_any_any_parameter_format get :handle_any_any, {:format=>'html'} assert_equal 'HTML', @response.body end - + def test_handle_any_any_explicit_html @request.env["HTTP_ACCEPT"] = "text/html" get :handle_any_any @@ -364,7 +364,7 @@ class MimeControllerTest < Test::Unit::TestCase get :handle_any_any assert_equal 'Whatever you ask for, I got it', @response.body end - + def test_handle_any_any_xml @request.env["HTTP_ACCEPT"] = "text/xml" get :handle_any_any @@ -445,31 +445,31 @@ class MimeControllerTest < Test::Unit::TestCase get :using_defaults, :format => "xml" assert_equal "using_defaults - xml", @response.body - end - + end + def test_format_with_custom_response_type get :iphone_with_html_response_type - assert_equal '
Hello future from Firefox!
', @response.body - + assert_equal '
Hello future from Firefox!
', @response.body + get :iphone_with_html_response_type, :format => "iphone" assert_equal "text/html", @response.content_type assert_equal '
Hello iPhone future from iPhone!
', @response.body - end - + end + def test_format_with_custom_response_type_and_request_headers @request.env["HTTP_ACCEPT"] = "text/iphone" get :iphone_with_html_response_type assert_equal '
Hello iPhone future from iPhone!
', @response.body assert_equal "text/html", @response.content_type - end + end def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present get :iphone_with_html_response_type_without_layout - assert_equal '
Hello future from Firefox!
', @response.body + assert_equal '
Hello future from Firefox!
', @response.body @request.env["HTTP_ACCEPT"] = "text/iphone" assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout } - end + end end class AbstractPostController < ActionController::Base @@ -497,7 +497,7 @@ class PostController < AbstractPostController end end -class SuperPostController < PostController +class SuperPostController < PostController def index respond_to do |type| type.html @@ -514,25 +514,24 @@ class MimeControllerLayoutsTest < Test::Unit::TestCase @controller = PostController.new @request.host = "www.example.com" end - + def test_missing_layout_renders_properly get :index - assert_equal '
Hello Firefox
', @response.body + assert_equal '
Hello Firefox
', @response.body @request.env["HTTP_ACCEPT"] = "text/iphone" get :index assert_equal 'Hello iPhone', @response.body end - + def test_format_with_inherited_layouts @controller = SuperPostController.new - + get :index assert_equal 'Super Firefox', @response.body - + @request.env["HTTP_ACCEPT"] = "text/iphone" get :index assert_equal '
Super iPhone
', @response.body end end - diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index b77b3ceffa..deda47f352 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -45,11 +45,11 @@ class NewRenderTestController < ActionController::Base def render_action_hello_world_as_symbol render :action => :hello_world end - + def render_text_hello_world render :text => "hello world" end - + def render_text_hello_world_with_layout @variable_for_layout = ", I'm here!" render :text => "hello world", :layout => true @@ -68,7 +68,7 @@ class NewRenderTestController < ActionController::Base path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb') render :file => path end - + def render_file_from_template @secret = 'in the sauce' @path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb')) @@ -76,14 +76,14 @@ class NewRenderTestController < ActionController::Base def render_file_with_locals path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb') - render :file => path, :locals => {:secret => 'in the sauce'} + 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_file_not_using_full_path_with_dot_in_path @secret = 'in the sauce' render :file => 'test/dot.directory/render_file_with_ivar', :use_full_path => true @@ -105,7 +105,7 @@ class NewRenderTestController < ActionController::Base def layout_test_with_different_layout render :action => "hello_world", :layout => "standard" end - + def rendering_without_layout render :action => "hello_world", :layout => false end @@ -113,11 +113,11 @@ class NewRenderTestController < ActionController::Base def layout_overriding_layout render :action => "hello_world", :layout => "standard" end - + def rendering_nothing_on_layout render :nothing => true end - + def builder_layout_test render :action => "hello" end @@ -135,9 +135,9 @@ class NewRenderTestController < ActionController::Base def partial_only_with_layout render :partial => "partial_only", :layout => true end - + def partial_with_locals - render :partial => "customer", :locals => { :customer => Customer.new("david") } + render :partial => "customer", :locals => { :customer => Customer.new("david") } end def partial_with_form_builder @@ -151,11 +151,11 @@ class NewRenderTestController < ActionController::Base def partial_collection render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ] end - + def partial_collection_with_spacer render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] end - + def partial_collection_with_counter render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] end @@ -186,33 +186,33 @@ class NewRenderTestController < ActionController::Base def empty_partial_collection render :partial => "customer", :collection => [] end - + def partial_with_hash_object render :partial => "hash_object", :object => {:first_name => "Sam"} end - + def partial_hash_collection render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ] end - + def partial_hash_collection_with_locals render :partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" } end - + def partial_with_implicit_local_assignment @customer = Customer.new("Marcel") render :partial => "customer" end - + def missing_partial render :partial => 'thisFileIsntHere' end - + def hello_in_a_string @customers = [ Customer.new("david"), Customer.new("mary") ] render :text => "How's there? " << render_to_string(:template => "test/list") end - + def render_to_string_with_assigns @before = "i'm before the render" render_to_string :text => "foo" @@ -222,14 +222,14 @@ class NewRenderTestController < ActionController::Base def render_to_string_with_partial @partial_only = render_to_string :partial => "partial_only" - @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } - render :action => "test/hello_world" - end - + @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } + render :action => "test/hello_world" + end + def render_to_string_with_exception render_to_string :file => "exception that will not be caught - this will certainly not work", :use_full_path => true end - + def render_to_string_with_caught_exception @before = "i'm before the render" begin @@ -282,7 +282,7 @@ class NewRenderTestController < ActionController::Base render :text => "hello" redirect_to :action => "double_render" end - + def render_to_string_and_render @stuff = render_to_string :text => "here is some cached stuff" render :text => "Hi web users! #{@stuff}" @@ -329,7 +329,7 @@ class NewRenderTestController < ActionController::Base def render_with_location render :xml => "", :location => "http://example.com", :status => 201 end - + def render_with_object_location customer = Customer.new("Some guy", 1) render :xml => "", :location => customer_url(customer), :status => :created @@ -341,12 +341,12 @@ class NewRenderTestController < ActionController::Base "" end end.new - + render :xml => to_xmlable end helper NewRenderTestHelper - helper do + helper do def rjs_helper_method(value) page.visual_effect :highlight, value end @@ -383,7 +383,7 @@ class NewRenderTestController < ActionController::Base page.visual_effect :highlight, 'balance' end end - + def update_page_with_instance_variables @money = '$37,000,000.00' @div_id = 'balance' @@ -426,12 +426,12 @@ class NewRenderTestController < ActionController::Base def render_using_layout_around_block_in_main_layout_and_within_content_for_layout render :action => "using_layout_around_block" end - + def rescue_action(e) raise end - + private def determine_layout - case action_name + case action_name when "hello_world", "layout_test", "rendering_without_layout", "rendering_nothing_on_layout", "render_text_hello_world", "render_text_hello_world_with_layout", @@ -443,7 +443,7 @@ class NewRenderTestController < ActionController::Base "render_js_with_explicit_template", "render_js_with_explicit_action_template", "delete_with_js", "update_page", "update_page_with_instance_variables" - + "layouts/standard" when "builder_layout_test" "layouts/builder" @@ -457,8 +457,8 @@ class NewRenderTestController < ActionController::Base end end -NewRenderTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] -Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +NewRenderTestController.view_paths = [FIXTURE_LOAD_PATH] +Fun::GamesController.view_paths = [FIXTURE_LOAD_PATH] class NewRenderTest < Test::Unit::TestCase def setup @@ -527,7 +527,7 @@ class NewRenderTest < Test::Unit::TestCase end def test_render_file_not_using_full_path - get :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 @@ -540,7 +540,7 @@ class NewRenderTest < Test::Unit::TestCase get :render_file_with_locals assert_equal "The secret is in the sauce\n", @response.body end - + def test_render_file_from_template get :render_file_from_template assert_equal "The secret is in the sauce\n", @response.body @@ -661,7 +661,7 @@ EOS assert_not_deprecated { get :hello_in_a_string } assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body end - + def test_render_to_string_doesnt_break_assigns get :render_to_string_with_assigns assert_equal "i'm before the render", assigns(:before) @@ -672,12 +672,12 @@ EOS get :render_to_string_with_partial assert_equal "only partial", assigns(:partial_only) assert_equal "Hello: david", assigns(:partial_with_locals) - end + end def test_bad_render_to_string_still_throws_exception assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception } end - + def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns assert_nothing_raised { get :render_to_string_with_caught_exception } assert_equal "i'm before the render", assigns(:before) @@ -715,7 +715,7 @@ EOS def test_render_and_redirect assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect } end - + # specify the one exception to double render rule - render_to_string followed by render def test_render_to_string_and_render get :render_to_string_and_render @@ -736,7 +736,7 @@ EOS get :partials_list assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body end - + def test_partial_with_locals get :partial_with_locals assert_equal "Hello: david", @response.body @@ -758,17 +758,17 @@ EOS get :partial_collection assert_equal "Hello: davidHello: mary", @response.body end - + def test_partial_collection_with_counter get :partial_collection_with_counter assert_equal "david0mary1", @response.body end - + def test_partial_collection_with_locals get :partial_collection_with_locals assert_equal "Bonjour: davidBonjour: mary", @response.body end - + def test_partial_collection_with_spacer get :partial_collection_with_spacer assert_equal "Hello: davidonly partialHello: mary", @response.body @@ -793,12 +793,12 @@ EOS get :partial_with_hash_object assert_equal "Sam\nmaS\n", @response.body end - + def test_hash_partial_collection get :partial_hash_collection assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body end - + def test_partial_hash_collection_with_locals get :partial_hash_collection_with_locals assert_equal "Hola: PratikHola: Amy", @response.body @@ -808,25 +808,25 @@ EOS get :partial_with_implicit_local_assignment assert_equal "Hello: Marcel", @response.body end - + def test_render_missing_partial_template assert_raises(ActionView::MissingTemplate) do get :missing_partial end end - + def test_render_text_with_assigns get :render_text_with_assigns assert_equal "world", assigns["hello"] end - + def test_update_page get :update_page assert_template nil assert_equal 'text/javascript; charset=utf-8', @response.headers['type'] assert_equal 2, @response.body.split($/).length end - + def test_update_page_with_instance_variables get :update_page_with_instance_variables assert_template nil @@ -834,7 +834,7 @@ EOS assert_match /balance/, @response.body assert_match /\$37/, @response.body end - + def test_yield_content_for assert_not_deprecated { get :yield_content_for } assert_equal "Putting stuff in the title!\n\nGreat stuff!\n", @response.body @@ -906,12 +906,12 @@ EOS get :render_with_location assert_equal "http://example.com", @response.headers["Location"] end - + def test_rendering_xml_should_call_to_xml_if_possible get :render_with_to_xml assert_equal "", @response.body end - + def test_rendering_with_object_location_should_set_header_with_url_for ActionController::Routing::Routes.draw do |map| map.resources :customers @@ -941,5 +941,4 @@ EOS get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body end - end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 066fa6acd4..65862c6b14 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -23,11 +23,11 @@ class TestController < ActionController::Base def render_hello_world_with_forward_slash render :template => "/test/hello_world" end - + def render_template_in_top_directory render :template => 'shared' end - + def render_template_in_top_directory_with_slash render :template => '/shared' end @@ -86,7 +86,7 @@ class TestController < ActionController::Base def render_nothing_with_appendix render :text => "appended" end - + def render_invalid_args render("test/hello") end @@ -171,7 +171,7 @@ class TestController < ActionController::Base def partial_dot_html render :partial => 'partial.html.erb' end - + def partial_as_rjs render :update do |page| page.replace :foo, :partial => 'partial' @@ -217,8 +217,8 @@ class TestController < ActionController::Base end end -TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] -Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +TestController.view_paths = [FIXTURE_LOAD_PATH] +Fun::GamesController.view_paths = [FIXTURE_LOAD_PATH] class RenderTest < Test::Unit::TestCase def setup @@ -251,13 +251,13 @@ class RenderTest < Test::Unit::TestCase get :render_hello_world_with_forward_slash assert_template "test/hello_world" end - + def test_render_in_top_directory get :render_template_in_top_directory assert_template "shared" assert_equal "Elastica", @response.body end - + def test_render_in_top_directory_with_slash get :render_template_in_top_directory_with_slash assert_template "shared" @@ -336,11 +336,11 @@ class RenderTest < Test::Unit::TestCase assert_response 200 assert_equal 'appended', @response.body end - + def test_attempt_to_render_with_invalid_arguments assert_raises(ActionController::RenderError) { get :render_invalid_args } end - + def test_attempt_to_access_object_method assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone } end @@ -467,17 +467,17 @@ class RenderTest < Test::Unit::TestCase get :formatted_html_erb assert_equal 'formatted html erb', @response.body end - + def test_should_render_formatted_xml_erb_template get :formatted_xml_erb, :format => :xml assert_equal 'passed formatted xml erb', @response.body end - + def test_should_render_formatted_html_erb_template get :formatted_xml_erb assert_equal 'passed formatted html erb', @response.body end - + def test_should_render_formatted_html_erb_template_with_faulty_accepts_header @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" get :formatted_xml_erb @@ -520,7 +520,6 @@ class RenderTest < Test::Unit::TestCase end protected - def etag_for(text) %("#{Digest::MD5.hexdigest(text)}") end diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 406825fe59..ddec51d173 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -1,13 +1,11 @@ require 'abstract_unit' - module TestFileUtils def file_name() File.basename(__FILE__) end def file_path() File.expand_path(__FILE__) end def file_data() File.open(file_path, 'rb') { |f| f.read } end end - class SendFileController < ActionController::Base include TestFileUtils layout "layouts/standard" # to make sure layouts don't interfere @@ -21,7 +19,7 @@ class SendFileController < ActionController::Base def rescue_action(e) raise end end -SendFileController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +SendFileController.view_paths = [FIXTURE_LOAD_PATH] class SendFileTest < Test::Unit::TestCase include TestFileUtils diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb index 8203061028..9401c87d10 100644 --- a/actionpack/test/controller/view_paths_test.rb +++ b/actionpack/test/controller/view_paths_test.rb @@ -1,9 +1,7 @@ require 'abstract_unit' class ViewLoadPathsTest < Test::Unit::TestCase - LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') - - ActionController::Base.view_paths = [LOAD_PATH_ROOT] + ActionController::Base.view_paths = [FIXTURE_LOAD_PATH] class TestController < ActionController::Base def self.controller_path() "test" end @@ -16,7 +14,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase private def add_view_path - prepend_view_path "#{LOAD_PATH_ROOT}/override" + prepend_view_path "#{FIXTURE_LOAD_PATH}/override" end end @@ -47,35 +45,35 @@ class ViewLoadPathsTest < Test::Unit::TestCase end def test_template_load_path_was_set_correctly - assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths + assert_equal [FIXTURE_LOAD_PATH], @controller.view_paths end def test_controller_appends_view_path_correctly @controller.append_view_path 'foo' - assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths + assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths @controller.append_view_path(%w(bar baz)) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths + assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths - @controller.append_view_path(LOAD_PATH_ROOT) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths + @controller.append_view_path(FIXTURE_LOAD_PATH) + assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths - @controller.append_view_path([LOAD_PATH_ROOT]) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths + @controller.append_view_path([FIXTURE_LOAD_PATH]) + assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths end def test_controller_prepends_view_path_correctly @controller.prepend_view_path 'baz' - assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths + assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths + assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths - @controller.prepend_view_path(LOAD_PATH_ROOT) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths + @controller.prepend_view_path(FIXTURE_LOAD_PATH) + assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths - @controller.prepend_view_path([LOAD_PATH_ROOT]) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths + @controller.prepend_view_path([FIXTURE_LOAD_PATH]) + assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths end def test_template_appends_view_path_correctly @@ -83,10 +81,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase class_view_paths = TestController.view_paths @controller.append_view_path 'foo' - assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths + assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths @controller.append_view_path(%w(bar baz)) - assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths + assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths assert_equal class_view_paths, TestController.view_paths end @@ -95,10 +93,10 @@ class ViewLoadPathsTest < Test::Unit::TestCase class_view_paths = TestController.view_paths @controller.prepend_view_path 'baz' - assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths + assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths + assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths assert_equal class_view_paths, TestController.view_paths end @@ -109,7 +107,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase end def test_view_paths_override - TestController.prepend_view_path "#{LOAD_PATH_ROOT}/override" + TestController.prepend_view_path "#{FIXTURE_LOAD_PATH}/override" get :hello_world assert_response :success assert_equal "Hello overridden world!", @response.body @@ -117,7 +115,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase def test_view_paths_override_for_layouts_in_controllers_with_a_module @controller = Test::SubController.new - Test::SubController.view_paths = [ "#{LOAD_PATH_ROOT}/override", LOAD_PATH_ROOT, "#{LOAD_PATH_ROOT}/override2" ] + Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ] get :hello_world assert_response :success assert_equal "layout: Hello overridden world!", @response.body diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index bdabe07121..54e4bbda1b 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -2,11 +2,9 @@ require 'abstract_unit' require 'controller/fake_models' class ViewRenderTest < Test::Unit::TestCase - FIXTURE_LOAD_PATHS = ActionView::ViewLoadPaths.new([File.join(File.dirname(__FILE__), '..', 'fixtures')]) - def setup @assigns = { :secret => 'in the sauce' } - @view = ActionView::Base.new(FIXTURE_LOAD_PATHS, @assigns) + @view = ActionView::Base.new([FIXTURE_LOAD_PATH], @assigns) end def test_render_file diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 44ceed6661..3d5f7eae11 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -301,7 +301,7 @@ end class UrlHelperWithControllerTest < ActionView::TestCase class UrlHelperController < ActionController::Base - self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ] + self.view_paths = [FIXTURE_LOAD_PATH] def self.controller_path; 'url_helper_with_controller' end @@ -356,7 +356,7 @@ end class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase class TasksController < ActionController::Base - self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] + self.view_paths = [FIXTURE_LOAD_PATH] def self.controller_path; 'tasks' end @@ -448,7 +448,7 @@ end class PolymorphicControllerTest < ActionView::TestCase class WorkshopsController < ActionController::Base - self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] + self.view_paths = [FIXTURE_LOAD_PATH] def self.controller_path; 'workshops' end @@ -466,7 +466,7 @@ class PolymorphicControllerTest < ActionView::TestCase end class SessionsController < ActionController::Base - self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] + self.view_paths = [FIXTURE_LOAD_PATH] def self.controller_path; 'sessions' end -- cgit v1.2.3 From 3c1e8ab0fecd5ee7f9328578016ef1e61214eeee Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Mon, 23 Jun 2008 21:14:49 +0800 Subject: Allow single quote (the ' character) in the middle of URL when auto_link-ing. [#471 state:resolved] Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/text_helper.rb | 2 +- actionpack/test/template/text_helper_test.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index d98c5bd0d5..a6c48737e9 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -468,7 +468,7 @@ module ActionView [-\w]+ # subdomain or domain (?:\.[-\w]+)* # remaining subdomains or domain (?::\d+)? # port - (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:][^\s$]))+)?)* # path + (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))+)?)* # path (?:\?[\w\+@%&=.;-]+)? # query string (?:\#[\w\-]*)? # trailing anchor ) diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index df6be3bb13..4999525939 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -193,6 +193,7 @@ class TextHelperTest < ActionView::TestCase http://www.mail-archive.com/rails@lists.rubyonrails.org/ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1 http://en.wikipedia.org/wiki/Sprite_(computer_graphics) + http://en.wikipedia.org/wiki/Texas_hold'em ) urls.each do |url| -- cgit v1.2.3 From 029a7455846cde2654958358a6fe354f236ade35 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 29 Jun 2008 00:35:30 +0100 Subject: Ensure FormBuilder date helpers respects html_options. [#506 state:resolved] [Pascal Ehlert] Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/date_helper.rb | 6 +-- actionpack/test/template/date_helper_test.rb | 63 ++++++++++++++++++++--- 2 files changed, 58 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 7ed6272898..1aee9ef0a2 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -696,15 +696,15 @@ module ActionView class FormBuilder def date_select(method, options = {}, html_options = {}) - @template.date_select(@object_name, method, options.merge(:object => @object)) + @template.date_select(@object_name, method, options.merge(:object => @object), html_options) end def time_select(method, options = {}, html_options = {}) - @template.time_select(@object_name, method, options.merge(:object => @object)) + @template.time_select(@object_name, method, options.merge(:object => @object), html_options) end def datetime_select(method, options = {}, html_options = {}) - @template.datetime_select(@object_name, method, options.merge(:object => @object)) + @template.datetime_select(@object_name, method, options.merge(:object => @object), html_options) end end end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 11b3bdb3fa..3faa363459 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1155,6 +1155,30 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", {}, :class => 'selector') end + def test_date_select_with_html_options_within_fields_for + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + + fields_for :post, @post do |f| + concat f.date_select(:written_on, {}, :class => 'selector') + end + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, output_buffer + end + def test_time_select @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) @@ -1216,6 +1240,29 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, time_select("post", "written_on", {}, :class => 'selector') end + def test_time_select_with_html_options_within_fields_for + @post = Post.new + @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) + + fields_for :post, @post do |f| + concat f.time_select(:written_on, {}, :class => 'selector') + end + + expected = %{\n} + expected << %{\n} + expected << %{\n} + + expected << %(\n" + expected << " : " + expected << %(\n" + + assert_dom_equal expected, output_buffer + end + def test_datetime_select @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) @@ -1281,21 +1328,21 @@ class DateHelperTest < ActionView::TestCase end end - def test_datetime_select_within_fields_for + def test_datetime_select_with_html_options_within_fields_for @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) fields_for :post, @post do |f| - concat f.datetime_select(:updated_at) + concat f.datetime_select(:updated_at, {}, :class => 'selector') end - expected = "\n" - expected << "\n" - expected << "\n" - expected << " — \n" - expected << " : \n" + expected = "\n" + expected << "\n" + expected << "\n" + expected << " — \n" + expected << " : \n" - assert_dom_equal(expected, output_buffer) + assert_dom_equal expected, output_buffer end def test_date_select_with_zero_value_and_no_start_year -- cgit v1.2.3 From d0092dc44d580f4179308c7394d9023098406f79 Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 28 Jun 2008 22:27:32 -0400 Subject: Added support for regexp matching of priority zones in time_zone_select [#195 state:resolved] --- .../lib/action_view/helpers/form_options_helper.rb | 11 +++++++++-- actionpack/test/template/form_options_helper_test.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index b3f8e63c1b..75220a2f66 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -150,7 +150,8 @@ module ActionView # You can also supply an array of TimeZone objects # as +priority_zones+, so that they will be listed above the rest of the # (long) list. (You can use TimeZone.us_zones as a convenience for - # obtaining a list of the US time zones.) + # obtaining a list of the US time zones, or a Regexp to select the zones + # of your choice) # # Finally, this method supports a :default option, which selects # a default TimeZone if the object's time zone is +nil+. @@ -164,6 +165,8 @@ module ActionView # # time_zone_select( "user", 'time_zone', [ TimeZone['Alaska'], TimeZone['Hawaii'] ]) # + # time_zone_select( "user", 'time_zone', /Australia/) + # # time_zone_select( "user", "time_zone", TZInfo::Timezone.all.sort, :model => TZInfo::Timezone) def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {}) InstanceTag.new(object, method, self, nil, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options) @@ -292,7 +295,8 @@ module ActionView # selected option tag. You can also supply an array of TimeZone objects # as +priority_zones+, so that they will be listed above the rest of the # (long) list. (You can use TimeZone.us_zones as a convenience for - # obtaining a list of the US time zones.) + # obtaining a list of the US time zones, or a Regexp to select the zones + # of your choice) # # The +selected+ parameter must be either +nil+, or a string that names # a TimeZone. @@ -311,6 +315,9 @@ module ActionView convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } } if priority_zones + if priority_zones.is_a?(Regexp) + priority_zones = model.all.find_all {|z| z =~ priority_zones} + end zone_options += options_for_select(convert_zones[priority_zones], selected) zone_options += "\n" diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 3f89a5e426..1db3a4d7d8 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1300,6 +1300,24 @@ COUNTRIES html end + uses_mocha "time_zone_select_with_priority_zones_as_regexp" do + def test_time_zone_select_with_priority_zones_as_regexp + @firm = Firm.new("D") + MockTimeZone.any_instance.stubs(:=~).returns(true,false,false,true,false) + + html = time_zone_select("firm", "time_zone", /A|D/) + assert_dom_equal "", + html + end + end + def test_time_zone_select_with_default_time_zone_and_nil_value @firm = Firm.new() @firm.time_zone = nil -- cgit v1.2.3 From 4cf93935b2478201863c01569e894c9dcf7e9074 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sun, 29 Jun 2008 13:46:07 -0500 Subject: Fix indentation and update changelogs for previous commit --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/form_options_helper.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index d32f7a298f..165db916ee 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* time_zone_select: support for regexp matching of priority zones. Resolves #195 [Ernie Miller] + * Made ActionView::Base#render_file private [Josh Peek] * Fix polymorphic_url with singleton resources. #461 [Tammer Saleh] diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 75220a2f66..0ca5cebcba 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -315,9 +315,9 @@ module ActionView convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } } if priority_zones - if priority_zones.is_a?(Regexp) + if priority_zones.is_a?(Regexp) priority_zones = model.all.find_all {|z| z =~ priority_zones} - end + end zone_options += options_for_select(convert_zones[priority_zones], selected) zone_options += "\n" -- cgit v1.2.3 From 7378e237342443addb1691795ac9457250b6db1e Mon Sep 17 00:00:00 2001 From: Scott Stewart Date: Wed, 2 Jul 2008 01:36:58 +0100 Subject: Ensure proper output when submit_tag is used with :disabled_with. [#388 state:resolved] Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/form_tag_helper.rb | 6 ++++-- actionpack/test/template/form_tag_helper_test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index ccebec3692..bdfb2eebd7 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -348,11 +348,13 @@ module ActionView options.stringify_keys! if disable_with = options.delete("disable_with") + disable_with = "this.value='#{disable_with}'" + disable_with << ";#{options.delete('onclick')}" if options['onclick'] + options["onclick"] = [ "this.setAttribute('originalValue', this.value)", "this.disabled=true", - "this.value='#{disable_with}'", - "#{options["onclick"]}", + disable_with, "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())", "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }", "return result;", diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index eabbe9c8a0..4e4102aec7 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -222,6 +222,13 @@ class FormTagHelperTest < ActionView::TestCase ) end + def test_submit_tag_with_no_onclick_options + assert_dom_equal( + %(), + submit_tag("Save", :disable_with => "Saving...") + ) + end + def test_submit_tag_with_confirmation assert_dom_equal( %(), -- cgit v1.2.3 From aff2d331720a3143914a0fffd1eba613dc333bfc Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 1 Jul 2008 21:52:28 -0500 Subject: Improved test coverage and added RackRequest support for CGI environment variables. --- actionpack/lib/action_controller/rack_process.rb | 35 ++++++++-- actionpack/test/controller/cgi_test.rb | 87 +++++++++++++++++++++--- actionpack/test/controller/rack_test.rb | 54 ++++++++++++--- 3 files changed, 154 insertions(+), 22 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index 9b4aa9b7cf..01bc1ebb26 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -24,6 +24,19 @@ module ActionController #:nodoc: super() end + %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO + PATH_TRANSLATED QUERY_STRING REMOTE_HOST + REMOTE_IDENT REMOTE_USER SCRIPT_NAME + SERVER_NAME SERVER_PROTOCOL + + HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING + HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST + HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env| + define_method(env.sub(/^HTTP_/n, '').downcase) do + @env[env] + end + end + # The request body is an IO input stream. If the RAW_POST_DATA environment # variable is already set, wrap it in a StringIO. def body @@ -35,7 +48,7 @@ module ActionController #:nodoc: end def key?(key) - @env.key? key + @env.key?(key) end def query_parameters @@ -85,6 +98,18 @@ module ActionController #:nodoc: @env['REMOTE_ADDR'] end + def request_method + @env['REQUEST_METHOD'].downcase.to_sym + end + + def server_port + @env['SERVER_PORT'].to_i + end + + def server_software + @env['SERVER_SOFTWARE'].split("/").first + end + def session unless defined?(@session) if @session_options == false @@ -178,9 +203,9 @@ end_msg normalize_headers(@headers) if [204, 304].include?(@status.to_i) @headers.delete "Content-Type" - [status.to_i, @headers.to_hash, []] + [status, @headers.to_hash, []] else - [status.to_i, @headers.to_hash, self] + [status, @headers.to_hash, self] end end alias to_a out @@ -225,8 +250,8 @@ end_msg headers['Content-Language'] = options.delete('language') if options['language'] headers['Expires'] = options.delete('expires') if options['expires'] - @status = options.delete('Status') if options['Status'] - @status ||= 200 + @status = options['Status'] || "200 OK" + # Convert 'cookie' header to 'Set-Cookie' headers. # Because Set-Cookie header can appear more the once in the response body, # we store it in a line break seperated string that will be translated to diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index 1b1ded4615..bf3b8b788e 100755 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -3,18 +3,58 @@ require 'action_controller/cgi_process' class BaseCgiTest < Test::Unit::TestCase def setup - @request_hash = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"} + @request_hash = { + "HTTP_MAX_FORWARDS" => "10", + "SERVER_NAME" => "glu.ttono.us:8007", + "FCGI_ROLE" => "RESPONDER", + "AUTH_TYPE" => "Basic", + "HTTP_X_FORWARDED_HOST" => "glu.ttono.us", + "HTTP_ACCEPT_CHARSET" => "UTF-8", + "HTTP_ACCEPT_ENCODING" => "gzip, deflate", + "HTTP_CACHE_CONTROL" => "no-cache, max-age=0", + "HTTP_PRAGMA" => "no-cache", + "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", + "PATH_INFO" => "/homepage/", + "HTTP_ACCEPT_LANGUAGE" => "en", + "HTTP_NEGOTIATE" => "trans", + "HTTP_HOST" => "glu.ttono.us:8007", + "HTTP_REFERER" => "http://www.google.com/search?q=glu.ttono.us", + "HTTP_FROM" => "googlebot", + "SERVER_PROTOCOL" => "HTTP/1.1", + "REDIRECT_URI" => "/dispatch.fcgi", + "SCRIPT_NAME" => "/dispatch.fcgi", + "SERVER_ADDR" => "207.7.108.53", + "REMOTE_ADDR" => "207.7.108.53", + "REMOTE_HOST" => "google.com", + "REMOTE_IDENT" => "kevin", + "REMOTE_USER" => "kevin", + "SERVER_SOFTWARE" => "lighttpd/1.4.5", + "HTTP_COOKIE" => "_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", + "HTTP_X_FORWARDED_SERVER" => "glu.ttono.us", + "REQUEST_URI" => "/admin", + "DOCUMENT_ROOT" => "/home/kevinc/sites/typo/public", + "PATH_TRANSLATED" => "/home/kevinc/sites/typo/public/homepage/", + "SERVER_PORT" => "8007", + "QUERY_STRING" => "", + "REMOTE_PORT" => "63137", + "GATEWAY_INTERFACE" => "CGI/1.1", + "HTTP_X_FORWARDED_FOR" => "65.88.180.234", + "HTTP_ACCEPT" => "*/*", + "SCRIPT_FILENAME" => "/home/kevinc/sites/typo/public/dispatch.fcgi", + "REDIRECT_STATUS" => "200", + "REQUEST_METHOD" => "GET" + } # some Nokia phone browsers omit the space after the semicolon separator. # some developers have grown accustomed to using comma in cookie values. @alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"} - @fake_cgi = Struct.new(:env_table).new(@request_hash) - @request = ActionController::CgiRequest.new(@fake_cgi) + @cgi = CGI.new + @cgi.stubs(:env_table).returns(@request_hash) + @request = ActionController::CgiRequest.new(@cgi) end def default_test; end end - class CgiRequestTest < BaseCgiTest def test_proxy_request assert_equal 'glu.ttono.us', @request.host_with_port @@ -71,6 +111,37 @@ class CgiRequestTest < BaseCgiTest assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host end + def test_cgi_environment_variables + assert_equal "Basic", @request.auth_type + assert_equal 0, @request.content_length + assert_equal nil, @request.content_type + assert_equal "CGI/1.1", @request.gateway_interface + assert_equal "*/*", @request.accept + assert_equal "UTF-8", @request.accept_charset + assert_equal "gzip, deflate", @request.accept_encoding + assert_equal "en", @request.accept_language + assert_equal "no-cache, max-age=0", @request.cache_control + assert_equal "googlebot", @request.from + assert_equal "glu.ttono.us", @request.host + assert_equal "trans", @request.negotiate + assert_equal "no-cache", @request.pragma + assert_equal "http://www.google.com/search?q=glu.ttono.us", @request.referer + assert_equal "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", @request.user_agent + assert_equal "/homepage/", @request.path_info + assert_equal "/home/kevinc/sites/typo/public/homepage/", @request.path_translated + assert_equal "", @request.query_string + assert_equal "207.7.108.53", @request.remote_addr + assert_equal "google.com", @request.remote_host + assert_equal "kevin", @request.remote_ident + assert_equal "kevin", @request.remote_user + assert_equal :get, @request.request_method + assert_equal "/dispatch.fcgi", @request.script_name + assert_equal "glu.ttono.us:8007", @request.server_name + assert_equal 8007, @request.server_port + assert_equal "HTTP/1.1", @request.server_protocol + assert_equal "lighttpd", @request.server_software + end + def test_cookie_syntax_resilience cookies = CGI::Cookie::parse(@request_hash["HTTP_COOKIE"]); assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect @@ -82,7 +153,6 @@ class CgiRequestTest < BaseCgiTest end end - class CgiRequestParamsParsingTest < BaseCgiTest def test_doesnt_break_when_content_type_has_charset data = 'flamenco=love' @@ -98,7 +168,6 @@ class CgiRequestParamsParsingTest < BaseCgiTest end end - class CgiRequestNeedsRewoundTest < BaseCgiTest def test_body_should_be_rewound data = 'foo' @@ -119,8 +188,8 @@ uses_mocha 'CGI Response' do class CgiResponseTest < BaseCgiTest def setup super - @fake_cgi.expects(:header).returns("HTTP/1.0 200 OK\nContent-Type: text/html\n") - @response = ActionController::CgiResponse.new(@fake_cgi) + @cgi.expects(:header).returns("HTTP/1.0 200 OK\nContent-Type: text/html\n") + @response = ActionController::CgiResponse.new(@cgi) @output = StringIO.new('') end @@ -132,7 +201,7 @@ uses_mocha 'CGI Response' do end def test_head_request - @fake_cgi.env_table['REQUEST_METHOD'] = 'HEAD' + @cgi.env_table['REQUEST_METHOD'] = 'HEAD' @response.body = "Hello, World!" @response.out(@output) diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb index 856f24bbdb..486fe49737 100644 --- a/actionpack/test/controller/rack_test.rb +++ b/actionpack/test/controller/rack_test.rb @@ -7,22 +7,33 @@ class BaseRackTest < Test::Unit::TestCase "HTTP_MAX_FORWARDS" => "10", "SERVER_NAME" => "glu.ttono.us:8007", "FCGI_ROLE" => "RESPONDER", + "AUTH_TYPE" => "Basic", "HTTP_X_FORWARDED_HOST" => "glu.ttono.us", + "HTTP_ACCEPT_CHARSET" => "UTF-8", "HTTP_ACCEPT_ENCODING" => "gzip, deflate", + "HTTP_CACHE_CONTROL" => "no-cache, max-age=0", + "HTTP_PRAGMA" => "no-cache", "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", - "PATH_INFO" => "", + "PATH_INFO" => "/homepage/", "HTTP_ACCEPT_LANGUAGE" => "en", + "HTTP_NEGOTIATE" => "trans", "HTTP_HOST" => "glu.ttono.us:8007", + "HTTP_REFERER" => "http://www.google.com/search?q=glu.ttono.us", + "HTTP_FROM" => "googlebot", "SERVER_PROTOCOL" => "HTTP/1.1", "REDIRECT_URI" => "/dispatch.fcgi", "SCRIPT_NAME" => "/dispatch.fcgi", "SERVER_ADDR" => "207.7.108.53", "REMOTE_ADDR" => "207.7.108.53", + "REMOTE_HOST" => "google.com", + "REMOTE_IDENT" => "kevin", + "REMOTE_USER" => "kevin", "SERVER_SOFTWARE" => "lighttpd/1.4.5", "HTTP_COOKIE" => "_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER" => "glu.ttono.us", "REQUEST_URI" => "/admin", "DOCUMENT_ROOT" => "/home/kevinc/sites/typo/public", + "PATH_TRANSLATED" => "/home/kevinc/sites/typo/public/homepage/", "SERVER_PORT" => "8007", "QUERY_STRING" => "", "REMOTE_PORT" => "63137", @@ -42,7 +53,6 @@ class BaseRackTest < Test::Unit::TestCase def default_test; end end - class RackRequestTest < BaseRackTest def test_proxy_request assert_equal 'glu.ttono.us', @request.host_with_port @@ -99,6 +109,37 @@ class RackRequestTest < BaseRackTest assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host end + def test_cgi_environment_variables + assert_equal "Basic", @request.auth_type + assert_equal 0, @request.content_length + assert_equal nil, @request.content_type + assert_equal "CGI/1.1", @request.gateway_interface + assert_equal "*/*", @request.accept + assert_equal "UTF-8", @request.accept_charset + assert_equal "gzip, deflate", @request.accept_encoding + assert_equal "en", @request.accept_language + assert_equal "no-cache, max-age=0", @request.cache_control + assert_equal "googlebot", @request.from + assert_equal "glu.ttono.us", @request.host + assert_equal "trans", @request.negotiate + assert_equal "no-cache", @request.pragma + assert_equal "http://www.google.com/search?q=glu.ttono.us", @request.referer + assert_equal "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", @request.user_agent + assert_equal "/homepage/", @request.path_info + assert_equal "/home/kevinc/sites/typo/public/homepage/", @request.path_translated + assert_equal "", @request.query_string + assert_equal "207.7.108.53", @request.remote_addr + assert_equal "google.com", @request.remote_host + assert_equal "kevin", @request.remote_ident + assert_equal "kevin", @request.remote_user + assert_equal :get, @request.request_method + assert_equal "/dispatch.fcgi", @request.script_name + assert_equal "glu.ttono.us:8007", @request.server_name + assert_equal 8007, @request.server_port + assert_equal "HTTP/1.1", @request.server_protocol + assert_equal "lighttpd", @request.server_software + end + def test_cookie_syntax_resilience cookies = @request.cookies assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect @@ -110,7 +151,6 @@ class RackRequestTest < BaseRackTest end end - class RackRequestParamsParsingTest < BaseRackTest def test_doesnt_break_when_content_type_has_charset data = 'flamenco=love' @@ -126,7 +166,6 @@ class RackRequestParamsParsingTest < BaseRackTest end end - class RackRequestNeedsRewoundTest < BaseRackTest def test_body_should_be_rewound data = 'foo' @@ -143,7 +182,6 @@ class RackRequestNeedsRewoundTest < BaseRackTest end end - class RackResponseTest < BaseRackTest def setup super @@ -155,7 +193,7 @@ class RackResponseTest < BaseRackTest @response.body = "Hello, World!" status, headers, body = @response.out(@output) - assert_equal 200, status + assert_equal "200 OK", status assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers) parts = [] @@ -169,7 +207,7 @@ class RackResponseTest < BaseRackTest end status, headers, body = @response.out(@output) - assert_equal 200, status + assert_equal "200 OK", status assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers) parts = [] @@ -184,7 +222,7 @@ class RackResponseTest < BaseRackTest @response.body = "Hello, World!" status, headers, body = @response.out(@output) - assert_equal 200, status + assert_equal "200 OK", status assert_equal({ "Content-Type" => "text/html", "Cache-Control" => "no-cache", -- cgit v1.2.3 From f5052dd8a39099e4930faafe9b01e63ced2f6391 Mon Sep 17 00:00:00 2001 From: Tim Haines Date: Wed, 2 Jul 2008 04:26:34 +0100 Subject: Make sure render :template works with :locals. [#524 state:resolved] Signed-off-by: Pratik Naik --- actionpack/lib/action_controller/base.rb | 5 ++++- actionpack/test/controller/new_render_test.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 7cc670289d..a7b2f147e8 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -733,6 +733,9 @@ module ActionController #:nodoc: # # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb) # render :template => "weblog/show" # + # # Renders the template with a local variable + # render :template => "weblog/show", :locals => {:customer => Customer.new} + # # === Rendering a file # # File rendering works just like action rendering except that it takes a filesystem path. By default, the path @@ -855,7 +858,7 @@ module ActionController #:nodoc: render_for_file(file, options[:status], options[:use_full_path], options[:locals] || {}) elsif template = options[:template] - render_for_file(template, options[:status], true) + render_for_file(template, options[:status], true, options[:locals] || {}) elsif inline = options[:inline] add_variables_to_assigns diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index deda47f352..eac36a04cb 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -268,6 +268,10 @@ class NewRenderTestController < ActionController::Base render :template => "test/hello_world" end + def render_with_explicit_template_with_locals + render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' } + end + def double_render render :text => "hello" render :text => "world" @@ -820,6 +824,11 @@ EOS assert_equal "world", assigns["hello"] end + def test_template_with_locals + get :render_with_explicit_template_with_locals + assert_equal "The secret is area51\n", @response.body + end + def test_update_page get :update_page assert_template nil -- cgit v1.2.3 From 2b43620e3c1352028f19550fcde4632d65cbd191 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 2 Jul 2008 16:38:50 +0100 Subject: Add :as option to render a collection of partials with a custom local variable name. [#509 state:resolved] [Simon Jefford, Pratik Naik] --- actionpack/CHANGELOG | 6 ++++++ actionpack/lib/action_controller/base.rb | 5 ++++- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/partial_template.rb | 9 ++++++--- actionpack/lib/action_view/partials.rb | 14 +++++++------- actionpack/test/controller/new_render_test.rb | 9 +++++++++ actionpack/test/fixtures/test/_customer_with_var.erb | 1 + actionpack/test/template/render_test.rb | 5 +++++ 8 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 actionpack/test/fixtures/test/_customer_with_var.erb (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 165db916ee..5a5895d9b4 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,11 @@ *Edge* +* Add :as option to render a collection of partials with a custom local variable name. #509 [Simon Jefford, Pratik Naik] + + render :partial => 'other_people', :collection => @people, :as => :person + + This will let you access objects of @people as 'person' local variable inside 'other_people' partial template. + * time_zone_select: support for regexp matching of priority zones. Resolves #195 [Ernie Miller] * Made ActionView::Base#render_file private [Josh Peek] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index a7b2f147e8..209cdfa686 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -702,6 +702,9 @@ module ActionController #:nodoc: # # builds the complete response. # render :partial => "person", :collection => @winners # + # # Renders a collection of partials but with a custom local variable name + # render :partial => "admin_person", :collection => @winners, :as => :person + # # # Renders the same collection of partials, but also renders the # # person_divider partial between each person partial. # render :partial => "person", :collection => @winners, :spacer_template => "person_divider" @@ -889,7 +892,7 @@ module ActionController #:nodoc: if collection = options[:collection] render_for_text( @template.send!(:render_partial_collection, partial, collection, - options[:spacer_template], options[:locals]), options[:status] + options[:spacer_template], options[:locals], options[:as]), options[:status] ) else render_for_text( diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 40b808a61e..40a3b16e9f 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -252,7 +252,7 @@ module ActionView #:nodoc: elsif options[:file] render_file(options[:file], use_full_path || false, options[:locals]) elsif options[:partial] && options[:collection] - render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals]) + render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as]) elsif options[:partial] render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) elsif options[:inline] diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 0cf996ca04..3513244ecb 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -1,9 +1,10 @@ module ActionView #:nodoc: class PartialTemplate < Template #:nodoc: - attr_reader :variable_name, :object + attr_reader :variable_name, :object, :as - def initialize(view, partial_path, object = nil, locals = {}) + def initialize(view, partial_path, object = nil, locals = {}, as = nil) @view_controller = view.controller if view.respond_to?(:controller) + @as = as set_path_and_variable_name!(partial_path) super(view, @path, true, locals) add_object_to_local_assigns!(object) @@ -22,10 +23,11 @@ module ActionView #:nodoc: end def render_member(object) - @locals[:object] = @locals[@variable_name] = object + @locals[:object] = @locals[@variable_name] = @locals[as] = object template = render_template @locals[@counter_name] += 1 + @locals.delete(as) @locals.delete(@variable_name) @locals.delete(:object) @@ -45,6 +47,7 @@ module ActionView #:nodoc: else object end || @view_controller.instance_variable_get("@#{variable_name}") + @locals[as] ||= @locals[:object] if as end def set_path_and_variable_name!(partial_path) diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 6b294be6bd..7c6c98d611 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -123,32 +123,32 @@ module ActionView end end - def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}) #:nodoc: + def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}, as = nil) #:nodoc: return " " if collection.empty? local_assigns = local_assigns ? local_assigns.clone : {} spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : '' if partial_path.nil? - render_partial_collection_with_unknown_partial_path(collection, local_assigns) + render_partial_collection_with_unknown_partial_path(collection, local_assigns, as) else - render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns) + render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, as) end.join(spacer) end - def render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns) - template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) + def render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, as) + template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns, as) collection.map do |element| template.render_member(element) end end - def render_partial_collection_with_unknown_partial_path(collection, local_assigns) + def render_partial_collection_with_unknown_partial_path(collection, local_assigns, as) templates = Hash.new i = 0 collection.map do |element| partial_path = ActionController::RecordIdentifier.partial_path(element, controller.class.controller_path) - template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) + template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns, as) template.counter = i i += 1 template.render_member(element) diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index eac36a04cb..5a7da57559 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -152,6 +152,10 @@ class NewRenderTestController < ActionController::Base render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ] end + def partial_collection_with_as + render :partial => "customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer + end + def partial_collection_with_spacer render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] end @@ -763,6 +767,11 @@ EOS assert_equal "Hello: davidHello: mary", @response.body end + def test_partial_collection_with_as + get :partial_collection_with_as + assert_equal "david david davidmary mary mary", @response.body + end + def test_partial_collection_with_counter get :partial_collection_with_counter assert_equal "david0mary1", @response.body diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb new file mode 100644 index 0000000000..3379246b7e --- /dev/null +++ b/actionpack/test/fixtures/test/_customer_with_var.erb @@ -0,0 +1 @@ +<%= customer.name %> <%= object.name %> <%= customer_with_var.name %> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 54e4bbda1b..64244e50f7 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -54,6 +54,11 @@ class ViewRenderTest < Test::Unit::TestCase def test_render_partial_collection assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ]) end + + def test_render_partial_collection_as + assert_equal "david david davidmary mary mary", + @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer) + end # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout -- cgit v1.2.3 From 267d3964ebd7bbc8879d69ff5a323527179c497d Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 2 Jul 2008 17:19:41 +0100 Subject: Make sure render :collection doesnt set nil local when :as is absent --- actionpack/lib/action_view/partial_template.rb | 3 ++- actionpack/test/fixtures/test/_local_inspector.html.erb | 1 + actionpack/test/template/render_test.rb | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/fixtures/test/_local_inspector.html.erb (limited to 'actionpack') diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 3513244ecb..a2129952c0 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -23,7 +23,8 @@ module ActionView #:nodoc: end def render_member(object) - @locals[:object] = @locals[@variable_name] = @locals[as] = object + @locals[:object] = @locals[@variable_name] = object + @locals[as] = object if as template = render_template @locals[@counter_name] += 1 diff --git a/actionpack/test/fixtures/test/_local_inspector.html.erb b/actionpack/test/fixtures/test/_local_inspector.html.erb new file mode 100644 index 0000000000..c5a6e3e5bc --- /dev/null +++ b/actionpack/test/fixtures/test/_local_inspector.html.erb @@ -0,0 +1 @@ +<%= local_assigns.keys.map(&:to_s).sort.join(",") -%> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 64244e50f7..5163c35189 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -59,6 +59,11 @@ class ViewRenderTest < Test::Unit::TestCase assert_equal "david david davidmary mary mary", @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer) end + + def test_render_partial_collection_without_as + assert_equal "local_inspector,local_inspector_counter,object", + @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ]) + end # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout -- cgit v1.2.3 From a4138d46321d63012adbf1942f5ea7c5e4951fc9 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 2 Jul 2008 11:09:10 -0700 Subject: use mocha for TimeZone mocking in Form Options helper tests --- .../test/template/form_options_helper_test.rb | 999 ++++++++++----------- 1 file changed, 495 insertions(+), 504 deletions(-) (limited to 'actionpack') diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 1db3a4d7d8..2496931f4b 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1,66 +1,43 @@ require 'abstract_unit' -class MockTimeZone - attr_reader :name - - def initialize( name ) - @name = name - end - - def self.all - [ "A", "B", "C", "D", "E" ].map { |s| new s } - end - - def ==( z ) - z && @name == z.name - end - - def to_s - @name - end -end - -class FormOptionsHelperTest < ActionView::TestCase - tests ActionView::Helpers::FormOptionsHelper - - silence_warnings do - Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) - Continent = Struct.new('Continent', :continent_name, :countries) - Country = Struct.new('Country', :country_id, :country_name) - Firm = Struct.new('Firm', :time_zone) - Album = Struct.new('Album', :id, :title, :genre) - - ActiveSupport::TimeZone = MockTimeZone - end +TZInfo::Timezone.cattr_reader :loaded_zones + +uses_mocha "FormOptionsHelperTest" do + class FormOptionsHelperTest < ActionView::TestCase + tests ActionView::Helpers::FormOptionsHelper + + silence_warnings do + Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) + Continent = Struct.new('Continent', :continent_name, :countries) + Country = Struct.new('Country', :country_id, :country_name) + Firm = Struct.new('Firm', :time_zone) + Album = Struct.new('Album', :id, :title, :genre) + end - def test_collection_options - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] - - assert_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title") - ) - end + def setup + @fake_timezones = %w(A B C D E).inject([]) do |zones, id| + tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id) + ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz) + zones << tz + end + ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones) + end + def test_collection_options + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - def test_collection_options_with_preselected_value - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + assert_dom_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title") + ) + end - assert_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title", "Babe") - ) - end - def test_collection_options_with_preselected_value_array + def test_collection_options_with_preselected_value @posts = [ Post.new(" went home", "", "To a little house", "shh!"), Post.new("Babe went home", "Babe", "To a little house", "shh!"), @@ -68,354 +45,367 @@ class FormOptionsHelperTest < ActionView::TestCase ] assert_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", "Babe") ) - end + end - def test_array_options_for_select - assert_dom_equal( - "\n\n", - options_for_select([ "", "USA", "Sweden" ]) - ) - end + def test_collection_options_with_preselected_value_array + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_dom_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) + ) + end - def test_array_options_for_select_with_selection - assert_dom_equal( - "\n\n", - options_for_select([ "Denmark", "", "Sweden" ], "") - ) - end + def test_array_options_for_select + assert_dom_equal( + "\n\n", + options_for_select([ "", "USA", "Sweden" ]) + ) + end - def test_array_options_for_select_with_selection_array + def test_array_options_for_select_with_selection assert_dom_equal( - "\n\n", - options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], "") ) - end + end - def test_array_options_for_string_include_in_other_string_bug_fix + def test_array_options_for_select_with_selection_array + assert_dom_equal( + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) + ) + end + + def test_array_options_for_string_include_in_other_string_bug_fix + assert_dom_equal( + "\n", + options_for_select([ "ruby", "rubyonrails" ], "rubyonrails") + ) + assert_dom_equal( + "\n", + options_for_select([ "ruby", "rubyonrails" ], "ruby") + ) + assert_dom_equal( + %(\n\n), + options_for_select([ "ruby", "rubyonrails", nil ], "ruby") + ) + end + + def test_hash_options_for_select assert_dom_equal( - "\n", - options_for_select([ "ruby", "rubyonrails" ], "rubyonrails") + "\n", + options_for_select("$" => "Dollar", "" => "").split("\n").sort.join("\n") ) assert_dom_equal( - "\n", - options_for_select([ "ruby", "rubyonrails" ], "ruby") + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, "Dollar").split("\n").sort.join("\n") ) assert_dom_equal( - %(\n\n), - options_for_select([ "ruby", "rubyonrails", nil ], "ruby") + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]).split("\n").sort.join("\n") ) - end - - def test_hash_options_for_select - assert_dom_equal( - "\n", - options_for_select("$" => "Dollar", "" => "").split("\n").sort.join("\n") - ) - assert_dom_equal( - "\n", - options_for_select({ "$" => "Dollar", "" => "" }, "Dollar").split("\n").sort.join("\n") - ) - assert_dom_equal( - "\n", - options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]).split("\n").sort.join("\n") - ) - end + end - def test_ducktyped_options_for_select - quack = Struct.new(:first, :last) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")]) - ) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")], "Dollar") - ) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")], ["Dollar", ""]) - ) - end + def test_ducktyped_options_for_select + quack = Struct.new(:first, :last) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")]) + ) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")], "Dollar") + ) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")], ["Dollar", ""]) + ) + end - def test_option_groups_from_collection_for_select - @continents = [ - Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")] ), - Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) - ] + def test_option_groups_from_collection_for_select + @continents = [ + Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")] ), + Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) + ] - assert_dom_equal( - "\n\n", - option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") - ) - end + assert_dom_equal( + "\n\n", + option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") + ) + end - def test_time_zone_options_no_parms - opts = time_zone_options_for_select - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_no_parms + opts = time_zone_options_for_select + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_selected - opts = time_zone_options_for_select( "D" ) - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_selected + opts = time_zone_options_for_select( "D" ) + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_unknown_selected - opts = time_zone_options_for_select( "K" ) - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_unknown_selected + opts = time_zone_options_for_select( "K" ) + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( nil, zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( nil, zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_selected_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( "E", zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_selected_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( "E", zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_unselected_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( "C", zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_unselected_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( "C", zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_select - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest)) - ) - end + def test_select + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest)) + ) + end - def test_select_under_fields_for - @post = Post.new - @post.category = "" + def test_select_under_fields_for + @post = Post.new + @post.category = "" - fields_for :post, @post do |f| - concat f.select(:category, %w( abe hest)) - end + fields_for :post, @post do |f| + concat f.select(:category, %w( abe hest)) + end - assert_dom_equal( - "", - output_buffer - ) - end + assert_dom_equal( + "", + output_buffer + ) + end - def test_select_with_blank - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :include_blank => true) - ) - end + def test_select_with_blank + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :include_blank => true) + ) + end - def test_select_with_blank_as_string - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :include_blank => 'None') - ) - end + def test_select_with_blank_as_string + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :include_blank => 'None') + ) + end - def test_select_with_default_prompt - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true) - ) - end + def test_select_with_default_prompt + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true) + ) + end - def test_select_no_prompt_when_select_has_value - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true) - ) - end + def test_select_no_prompt_when_select_has_value + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true) + ) + end - def test_select_with_given_prompt - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => 'The prompt') - ) - end + def test_select_with_given_prompt + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => 'The prompt') + ) + end - def test_select_with_prompt_and_blank - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true, :include_blank => true) - ) - end + def test_select_with_prompt_and_blank + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true, :include_blank => true) + ) + end - def test_select_with_selected_value - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest ), :selected => 'abe') - ) - end + def test_select_with_selected_value + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest ), :selected => 'abe') + ) + end - def test_select_with_index_option - @album = Album.new - @album.id = 1 + def test_select_with_index_option + @album = Album.new + @album.id = 1 - expected = "" + expected = "" - assert_dom_equal( - expected, - select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) - ) - end + assert_dom_equal( + expected, + select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) + ) + end - def test_select_with_selected_nil - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest ), :selected => nil) - ) - end + def test_select_with_selected_nil + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest ), :selected => nil) + ) + end - def test_collection_select - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + def test_collection_select + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - @post = Post.new - @post.author_name = "Babe" + @post = Post.new + @post.author_name = "Babe" - assert_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name") - ) - end + assert_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name") + ) + end - def test_collection_select_under_fields_for - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + def test_collection_select_under_fields_for + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - @post = Post.new - @post.author_name = "Babe" + @post = Post.new + @post.author_name = "Babe" - fields_for :post, @post do |f| - concat f.collection_select(:author_name, @posts, :author_name, :author_name) - end + fields_for :post, @post do |f| + concat f.collection_select(:author_name, @posts, :author_name, :author_name) + end - assert_dom_equal( - "", - output_buffer - ) - end + assert_dom_equal( + "", + output_buffer + ) + end - def test_collection_select_with_blank_and_style - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + def test_collection_select_with_blank_and_style + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - @post = Post.new - @post.author_name = "Babe" + @post = Post.new + @post.author_name = "Babe" - assert_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") - ) - end + assert_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") + ) + end - def test_collection_select_with_blank_as_string_and_style - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + def test_collection_select_with_blank_as_string_and_style + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - @post = Post.new - @post.author_name = "Babe" + @post = Post.new + @post.author_name = "Babe" - assert_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px") - ) - end + assert_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px") + ) + end - def test_collection_select_with_multiple_option_appends_array_brackets - @posts = [ - Post.new(" went home", "", "To a little house", "shh!"), - Post.new("Babe went home", "Babe", "To a little house", "shh!"), - Post.new("Cabe went home", "Cabe", "To a little house", "shh!") - ] + def test_collection_select_with_multiple_option_appends_array_brackets + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] - @post = Post.new - @post.author_name = "Babe" + @post = Post.new + @post.author_name = "Babe" - expected = "" + expected = "" - # Should suffix default name with []. - assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, :multiple => true) + # Should suffix default name with []. + assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, :multiple => true) - # Shouldn't suffix custom name with []. - assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true) - end + # Shouldn't suffix custom name with []. + assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true) + end - def test_country_select - @post = Post.new - @post.origin = "Denmark" - expected_select = <<-COUNTRIES + def test_country_select + @post = Post.new + @post.origin = "Denmark" + expected_select = <<-COUNTRIES -COUNTRIES - assert_dom_equal(expected_select[0..-2], country_select("post", "origin")) - end + COUNTRIES + assert_dom_equal(expected_select[0..-2], country_select("post", "origin")) + end - def test_country_select_with_priority_countries - @post = Post.new - @post.origin = "Denmark" - expected_select = <<-COUNTRIES + def test_country_select_with_priority_countries + @post = Post.new + @post.origin = "Denmark" + expected_select = <<-COUNTRIES -COUNTRIES - assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"])) - end + COUNTRIES + assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"])) + end - def test_country_select_with_selected_priority_country - @post = Post.new - @post.origin = "New Zealand" - expected_select = <<-COUNTRIES + def test_country_select_with_selected_priority_country + @post = Post.new + @post.origin = "New Zealand" + expected_select = <<-COUNTRIES -COUNTRIES - assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"])) - end - - def test_time_zone_select - @firm = Firm.new("D") - html = time_zone_select( "firm", "time_zone" ) - assert_dom_equal "", - html - end - - def test_time_zone_select_under_fields_for - @firm = Firm.new("D") - - fields_for :firm, @firm do |f| - concat f.time_zone_select(:time_zone) + COUNTRIES + assert_dom_equal(expected_select[0..-2], country_select("post", "origin", ["New Zealand", "Nicaragua"])) end - - assert_dom_equal( - "", - output_buffer - ) - end - def test_time_zone_select_with_blank - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, :include_blank => true) - assert_dom_equal "", - html - end + def test_time_zone_select + @firm = Firm.new("D") + html = time_zone_select( "firm", "time_zone" ) + assert_dom_equal "", + html + end - def test_time_zone_select_with_blank_as_string - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone') - assert_dom_equal "", - html - end + def test_time_zone_select_under_fields_for + @firm = Firm.new("D") - def test_time_zone_select_with_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, {}, - "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {}, - :style => "color: red") - end + fields_for :firm, @firm do |f| + concat f.time_zone_select(:time_zone) + end + + assert_dom_equal( + "", + output_buffer + ) + end - def test_time_zone_select_with_blank_and_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, - { :include_blank => true }, "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, - { :include_blank => true }, :style => "color: red") - end + def test_time_zone_select_with_blank + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, :include_blank => true) + assert_dom_equal "", + html + end - def test_time_zone_select_with_blank_as_string_and_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, - { :include_blank => 'No Zone' }, "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, - { :include_blank => 'No Zone' }, :style => "color: red") - end + def test_time_zone_select_with_blank_as_string + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone') + assert_dom_equal "", + html + end - def test_time_zone_select_with_priority_zones - @firm = Firm.new("D") - zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] - html = time_zone_select("firm", "time_zone", zones ) - assert_dom_equal "", - html - end + def test_time_zone_select_with_style + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, {}, + "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {}, + :style => "color: red") + end - uses_mocha "time_zone_select_with_priority_zones_as_regexp" do - def test_time_zone_select_with_priority_zones_as_regexp + def test_time_zone_select_with_blank_and_style @firm = Firm.new("D") - MockTimeZone.any_instance.stubs(:=~).returns(true,false,false,true,false) + html = time_zone_select("firm", "time_zone", nil, + { :include_blank => true }, "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, + { :include_blank => true }, :style => "color: red") + end - html = time_zone_select("firm", "time_zone", /A|D/) - assert_dom_equal "" + + "\n" + "\n" + - "" + - "\n" + "\n" + "\n" + + "\n" + "" + "", html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, + { :include_blank => 'No Zone' }, :style => "color: red") end - end - def test_time_zone_select_with_default_time_zone_and_nil_value - @firm = Firm.new() - @firm.time_zone = nil - html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + def test_time_zone_select_with_priority_zones + @firm = Firm.new("D") + zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] + html = time_zone_select("firm", "time_zone", zones ) assert_dom_equal "", html - end + end - def test_time_zone_select_with_default_time_zone_and_value - @firm = Firm.new('D') - html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + def test_time_zone_select_with_priority_zones_as_regexp + @firm = Firm.new("D") + @fake_timezones.each_with_index do |tz, i| + tz.stubs(:=~).returns(i.zero? || i == 3) + end + + html = time_zone_select("firm", "time_zone", /A|D/) assert_dom_equal "", html - end + end + + def test_time_zone_select_with_default_time_zone_and_nil_value + @firm = Firm.new() + @firm.time_zone = nil + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "", + html + end + + def test_time_zone_select_with_default_time_zone_and_value + @firm = Firm.new('D') + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "", + html + end -end + end +end \ No newline at end of file -- cgit v1.2.3 From 8f640c381d9d1b74f6a0fc3648c21da373661914 Mon Sep 17 00:00:00 2001 From: Mike Subelsky Date: Wed, 2 Jul 2008 14:15:43 -0400 Subject: Added application/jsonrequest as a synonym for application/json [#536 state:resolved] --- actionpack/lib/action_controller/mime_types.rb | 3 ++- actionpack/test/controller/request_test.rb | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb index 01a266d3fe..2d7fba1173 100644 --- a/actionpack/lib/action_controller/mime_types.rb +++ b/actionpack/lib/action_controller/mime_types.rb @@ -17,4 +17,5 @@ Mime::Type.register "multipart/form-data", :multipart_form Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form # http://www.ietf.org/rfc/rfc4627.txt -Mime::Type.register "application/json", :json, %w( text/x-json ) \ No newline at end of file +# http://www.json.org/JSONRequest.html +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest ) \ No newline at end of file diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 2bd489b2c7..20f3fd4d7b 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -909,15 +909,21 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest end class JsonParamsParsingTest < Test::Unit::TestCase - def test_hash_params - person = parse_body({:person => {:name => "David"}}.to_json)[:person] + def test_hash_params_for_application_json + person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person] + assert_kind_of Hash, person + assert_equal 'David', person['name'] + end + + def test_hash_params_for_application_jsonrequest + person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person] assert_kind_of Hash, person assert_equal 'David', person['name'] end private - def parse_body(body) - env = { 'CONTENT_TYPE' => 'application/json', + def parse_body(body,content_type) + env = { 'CONTENT_TYPE' => content_type, 'CONTENT_LENGTH' => body.size.to_s } cgi = ActionController::Integration::Session::StubCGI.new(env, body) ActionController::CgiRequest.new(cgi).request_parameters -- cgit v1.2.3 From 3b3790a4351ba7c9d2711089c21f24fcedc11fc0 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Jul 2008 21:38:58 -0500 Subject: Deprecate :use_full_path render option. The supplying the option no longer has an effect. --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 10 +++---- actionpack/lib/action_view/base.rb | 12 ++++---- actionpack/lib/action_view/partial_template.rb | 2 +- actionpack/lib/action_view/template.rb | 39 ++++++++++++++------------ actionpack/lib/action_view/template_file.rb | 10 +++---- actionpack/test/controller/new_render_test.rb | 8 +++--- actionpack/test/template/render_test.rb | 4 +-- 8 files changed, 45 insertions(+), 42 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 5a5895d9b4..9d64b6f231 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Deprecate :use_full_path render option. The supplying the option no longer has an effect [Josh Peek] + * Add :as option to render a collection of partials with a custom local variable name. #509 [Simon Jefford, Pratik Naik] render :partial => 'other_people', :collection => @people, :as => :person diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 209cdfa686..02941bade9 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -858,7 +858,7 @@ module ActionController #:nodoc: else if file = options[:file] - render_for_file(file, options[:status], options[:use_full_path], options[:locals] || {}) + render_for_file(file, options[:status], nil, options[:locals] || {}) elsif template = options[:template] render_for_file(template, options[:status], true, options[:locals] || {}) @@ -870,9 +870,9 @@ module ActionController #:nodoc: elsif action_name = options[:action] template = default_template_name(action_name.to_s) if options[:layout] && !template_exempt_from_layout?(template) - render_with_a_layout(:file => template, :status => options[:status], :use_full_path => true, :layout => true) + render_with_a_layout(:file => template, :status => options[:status], :layout => true) else - render_with_no_layout(:file => template, :status => options[:status], :use_full_path => true) + render_with_no_layout(:file => template, :status => options[:status]) end elsif xml = options[:xml] @@ -1097,10 +1097,10 @@ module ActionController #:nodoc: private - def render_for_file(template_path, status = nil, use_full_path = false, locals = {}) #:nodoc: + def render_for_file(template_path, status = nil, use_full_path = nil, locals = {}) #:nodoc: add_variables_to_assigns logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger - render_for_text(@template.render(:file => template_path, :use_full_path => use_full_path, :locals => locals), status) + render_for_text(@template.render(:file => template_path, :locals => locals), status) end def render_for_text(text = nil, status = nil, append_response = false) #:nodoc: diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 40a3b16e9f..a8c6e15ca3 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -232,12 +232,11 @@ module ActionView #:nodoc: # The hash in local_assigns is made available as local variables. def render(options = {}, local_assigns = {}, &block) #:nodoc: if options.is_a?(String) - render_file(options, true, local_assigns) + render_file(options, nil, local_assigns) elsif options == :update update_page(&block) elsif options.is_a?(Hash) - use_full_path = options[:use_full_path] - options = options.reverse_merge(:locals => {}, :use_full_path => true) + options = options.reverse_merge(:locals => {}) if partial_layout = options.delete(:layout) if block_given? @@ -250,7 +249,7 @@ module ActionView #:nodoc: end end elsif options[:file] - render_file(options[:file], use_full_path || false, options[:locals]) + render_file(options[:file], nil, options[:locals]) elsif options[:partial] && options[:collection] render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as]) elsif options[:partial] @@ -298,10 +297,9 @@ module ActionView #:nodoc: end private - # Renders the template present at template_path. If use_full_path is set to true, - # it's relative to the view_paths array, otherwise it's absolute. The hash in local_assigns + # Renders the template present at template_path. The hash in local_assigns # is made available as local variables. - def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc: + def render_file(template_path, use_full_path = nil, local_assigns = {}) #:nodoc: if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) && !template_path.include?("/") raise ActionViewError, <<-END_ERROR Due to changes in ActionMailer, you need to provide the mailer_name along with the template name. diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index a2129952c0..719199f19d 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -6,7 +6,7 @@ module ActionView #:nodoc: @view_controller = view.controller if view.respond_to?(:controller) @as = as set_path_and_variable_name!(partial_path) - super(view, @path, true, locals) + super(view, @path, nil, locals) add_object_to_local_assigns!(object) # This is needed here in order to compile template with knowledge of 'counter' diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 4c3f252c10..0cba1942f7 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -5,15 +5,19 @@ module ActionView #:nodoc: attr_accessor :locals attr_reader :handler, :path, :extension, :filename, :method - def initialize(view, path, use_full_path, locals = {}) + def initialize(view, path, use_full_path = nil, locals = {}) + unless use_full_path == nil + ActiveSupport::Deprecation.warn("use_full_path option has been deprecated and has no affect.", caller) + end + @view = view @paths = view.view_paths @original_path = path - @path = TemplateFile.from_path(path, !use_full_path) + @path = TemplateFile.from_path(path) @view.first_render ||= @path.to_s @source = nil # Don't read the source until we know that it is required - set_extension_and_file_name(use_full_path) + set_extension_and_file_name @locals = locals || {} @handler = self.class.handler_class_for_extension(@extension).new(@view) @@ -63,25 +67,24 @@ module ActionView #:nodoc: end private - def set_extension_and_file_name(use_full_path) + def set_extension_and_file_name @extension = @path.extension - if use_full_path - unless @extension - @path = @view.send(:template_file_from_name, @path) - raise_missing_template_exception unless @path - @extension = @path.extension - end - - if @path = @paths.find_template_file_for_path(path) - @filename = @path.full_path - @extension = @path.extension - end - else - @filename = @path.full_path + unless @extension + @path = @view.send(:template_file_from_name, @path) + raise_missing_template_exception unless @path + @extension = @path.extension end - raise_missing_template_exception if @filename.blank? + if p = @paths.find_template_file_for_path(path) + @path = p + @filename = @path.full_path + @extension = @path.extension + raise_missing_template_exception if @filename.blank? + else + @filename = @original_path + raise_missing_template_exception unless File.exist?(@filename) + end end def raise_missing_template_exception diff --git a/actionpack/lib/action_view/template_file.rb b/actionpack/lib/action_view/template_file.rb index dd66482b3c..c38e8ed122 100644 --- a/actionpack/lib/action_view/template_file.rb +++ b/actionpack/lib/action_view/template_file.rb @@ -4,8 +4,8 @@ module ActionView #:nodoc: # from the load path root e.g. "hello/index.html.erb" not # "app/views/hello/index.html.erb" class TemplateFile - def self.from_path(path, use_full_path = false) - path.is_a?(self) ? path : new(path, use_full_path) + def self.from_path(path) + path.is_a?(self) ? path : new(path) end def self.from_full_path(load_path, full_path) @@ -17,11 +17,11 @@ module ActionView #:nodoc: attr_accessor :load_path, :base_path, :name, :format, :extension delegate :to_s, :inspect, :to => :path - def initialize(path, use_full_path = false) + def initialize(path) path = path.dup - # Clear the forward slash in the beginning unless using full path - trim_forward_slash!(path) unless use_full_path + # Clear the forward slash in the beginning + trim_forward_slash!(path) @base_path, @name, @format, @extension = split(path) end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 5a7da57559..d60bacd52a 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -81,12 +81,12 @@ class NewRenderTestController < ActionController::Base def render_file_not_using_full_path @secret = 'in the sauce' - render :file => 'test/render_file_with_ivar', :use_full_path => true + render :file => 'test/render_file_with_ivar' end def render_file_not_using_full_path_with_dot_in_path @secret = 'in the sauce' - render :file => 'test/dot.directory/render_file_with_ivar', :use_full_path => true + render :file => 'test/dot.directory/render_file_with_ivar' end def render_xml_hello @@ -231,13 +231,13 @@ class NewRenderTestController < ActionController::Base end def render_to_string_with_exception - render_to_string :file => "exception that will not be caught - this will certainly not work", :use_full_path => true + render_to_string :file => "exception that will not be caught - this will certainly not work" end def render_to_string_with_caught_exception @before = "i'm before the render" begin - render_to_string :file => "exception that will be caught- hope my future instance vars still work!", :use_full_path => true + render_to_string :file => "exception that will be caught- hope my future instance vars still work!" rescue end @after = "i'm after the render" diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 5163c35189..726cf37026 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -12,7 +12,7 @@ class ViewRenderTest < Test::Unit::TestCase end def test_render_file_not_using_full_path - assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb", :use_full_path => true) + assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb") end def test_render_file_without_specific_extension @@ -21,7 +21,7 @@ class ViewRenderTest < Test::Unit::TestCase def test_render_file_with_full_path template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb') - assert_equal "Hello world!", @view.render(:file => template_path, :use_full_path => false) + assert_equal "Hello world!", @view.render(:file => template_path) end def test_render_file_with_instance_variables -- cgit v1.2.3 From a37d065f85941e1fe746dff4d42f015e1618834f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Thu, 3 Jul 2008 17:49:12 +0300 Subject: Use :namespace instead of :path_prefix for finding controller. [#544 state:resolved] :namespace is supposed to be the module where controller exists. :path_prefix can contain anything, including variables, which makes it unsuitable for determining the module for a controller. Signed-off-by: Pratik Naik --- actionpack/lib/action_controller/routing/builder.rb | 3 +-- actionpack/test/controller/routing_test.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/routing/builder.rb b/actionpack/lib/action_controller/routing/builder.rb index 4740113ed0..b8323847fd 100644 --- a/actionpack/lib/action_controller/routing/builder.rb +++ b/actionpack/lib/action_controller/routing/builder.rb @@ -67,10 +67,9 @@ module ActionController options = options.dup if options[:namespace] - options[:controller] = "#{options[:path_prefix]}/#{options[:controller]}" + options[:controller] = "#{options.delete(:namespace).sub(/\/$/, '')}/#{options[:controller]}" options.delete(:path_prefix) options.delete(:name_prefix) - options.delete(:namespace) end requirements = (options.delete(:requirements) || {}).dup diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 07c13ebbf7..c5ccb71582 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -2039,6 +2039,26 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do Object.send(:remove_const, :Api) end + def test_namespace_with_path_prefix + Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) + + set.draw do |map| + + map.namespace 'api', :path_prefix => 'prefix' do |api| + api.route 'inventory', :controller => "products", :action => 'inventory' + end + + end + + request.path = "/prefix/inventory" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("api/products", request.path_parameters[:controller]) + assert_equal("inventory", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :Api) + end + def test_generate_finds_best_fit set.draw do |map| map.connect "/people", :controller => "people", :action => "index" -- cgit v1.2.3 From 5501166dec84e1dd63800ea25eaf23290216cf80 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 30 Jun 2008 18:44:21 +0300 Subject: Remove strange alias for JavaScriptHelper --- actionpack/lib/action_view/helpers/javascript_helper.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index f89b6c2f70..8caca5b23f 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -216,7 +216,5 @@ module ActionView end end end - - JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper end end -- cgit v1.2.3 From e358b1fce8fdcbac896dde08286be020420e843e Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 30 Jun 2008 18:46:53 +0300 Subject: Remove old method of including javascripts define_javascript_functions. javascript_include_tag and friends do a much better job. --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/javascript_helper.rb | 26 ---------------------- actionpack/test/template/javascript_helper_test.rb | 8 ------- 3 files changed, 2 insertions(+), 34 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 9d64b6f231..0a5afddf04 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Remove define_javascript_functions, javascript_include_tag and friends are far superior. [Michael Koziarski] + * Deprecate :use_full_path render option. The supplying the option no longer has an effect [Josh Peek] * Add :as option to render a collection of partials with a custom local variable name. #509 [Simon Jefford, Pratik Naik] diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 8caca5b23f..22bd5cb440 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -114,32 +114,6 @@ module ActionView tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) end - # Includes the Action Pack JavaScript libraries inside a single ' - end - - JS_ESCAPE_MAP = { '\\' => '\\\\', ' '<\/', diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index d6d398d224..b2c4a130c8 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -3,14 +3,6 @@ require 'abstract_unit' class JavaScriptHelperTest < ActionView::TestCase tests ActionView::Helpers::JavaScriptHelper - def test_define_javascript_functions - # check if prototype.js is included first - assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype JavaScript framework/) - - # check that scriptaculous.js is not in here, only needed if loaded remotely - assert_nil define_javascript_functions.split("\n")[1].match(/var Scriptaculous = \{/) - end - def test_escape_javascript assert_equal '', escape_javascript(nil) assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos')) -- cgit v1.2.3 From efd18066a25610e5c00c7e144a9a08023016e576 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 30 Jun 2008 19:10:48 +0300 Subject: Tighten the rescue clause here to prevent hiding strange mock related errors behind the line offset test --- actionpack/test/controller/render_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 65862c6b14..10264dadaa 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -103,7 +103,7 @@ class TestController < ActionController::Base def render_line_offset begin render :inline => '<% raise %>', :locals => {:foo => 'bar'} - rescue => exc + rescue RuntimeError => exc end line = exc.backtrace.first render :text => line -- cgit v1.2.3 From df36a6f7598a7e963fb3d79fb48fd1c073045a43 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 30 Jun 2008 21:39:22 +0300 Subject: Remove unneeded ObjectWrapper class. Was previously needed to work around the semantics of a deprecated (now removed) API to render :partial --- actionpack/lib/action_controller/base.rb | 2 +- actionpack/lib/action_view/base.rb | 5 +---- actionpack/lib/action_view/partial_template.rb | 7 +------ 3 files changed, 3 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 02941bade9..009c9eec99 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -897,7 +897,7 @@ module ActionController #:nodoc: else render_for_text( @template.send!(:render_partial, partial, - ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]), options[:status] + options[:object], options[:locals]), options[:status] ) end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index a8c6e15ca3..9e255bd324 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -199,9 +199,6 @@ module ActionView #:nodoc: cattr_reader :computed_public_paths @@computed_public_paths = {} - class ObjectWrapper < Struct.new(:value) #:nodoc: - end - def self.helper_modules #:nodoc: helpers = [] Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file| @@ -253,7 +250,7 @@ module ActionView #:nodoc: elsif options[:partial] && options[:collection] render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as]) elsif options[:partial] - render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) + render_partial(options[:partial], options[:object], options[:locals]) elsif options[:inline] render_inline(options[:inline], options[:locals], options[:type]) end diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 719199f19d..6ebe165a15 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -42,12 +42,7 @@ module ActionView #:nodoc: private def add_object_to_local_assigns!(object) @locals[:object] ||= - @locals[@variable_name] ||= - if object.is_a?(ActionView::Base::ObjectWrapper) - object.value - else - object - end || @view_controller.instance_variable_get("@#{variable_name}") + @locals[@variable_name] ||= object || @view_controller.instance_variable_get("@#{variable_name}") @locals[as] ||= @locals[:object] if as end -- cgit v1.2.3 From 5dd10d60bbde906449882e39278be76f1f445a41 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 30 Jun 2008 22:15:12 +0300 Subject: Remove nested ternary operators from select_year in favour of conditionals. --- actionpack/lib/action_view/helpers/date_helper.rb | 31 +++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 1aee9ef0a2..fa3e612fc1 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -547,23 +547,32 @@ module ActionView # select_year(2006, :start_year => 2000, :end_year => 2010) # def select_year(date, options = {}, html_options = {}) - val = date ? (date.kind_of?(Fixnum) ? date : date.year) : '' + if !date || date == 0 + value = '' + middle_year = Date.today.year + elsif date.kind_of?(Fixnum) + value = middle_year = date + else + value = middle_year = date.year + end + if options[:use_hidden] - hidden_html(options[:field_name] || 'year', val, options) + hidden_html(options[:field_name] || 'year', value, options) else - year_options = [] - y = date ? (date.kind_of?(Fixnum) ? (y = (date == 0) ? Date.today.year : date) : date.year) : Date.today.year + year_options = '' + start_year = options[:start_year] || middle_year - 5 + end_year = options[:end_year] || middle_year + 5 + step_val = start_year < end_year ? 1 : -1 - start_year, end_year = (options[:start_year] || y-5), (options[:end_year] || y+5) - step_val = start_year < end_year ? 1 : -1 start_year.step(end_year, step_val) do |year| - year_options << ((val == year) ? - content_tag(:option, year, :value => year, :selected => "selected") : - content_tag(:option, year, :value => year) - ) + if value == year + year_options << content_tag(:option, year, :value => year, :selected => "selected") + else + year_options << content_tag(:option, year, :value => year) + end year_options << "\n" end - select_html(options[:field_name] || 'year', year_options.join, options, html_options) + select_html(options[:field_name] || 'year', year_options, options, html_options) end end -- cgit v1.2.3 From 7098143f07bd03223bbbeea8a9c23506a7b5dffc Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 2 Jul 2008 19:53:04 +0300 Subject: Remove unused local_binding option to InstanceTag's Constructor --- actionpack/lib/action_view/helpers/date_helper.rb | 6 +++--- actionpack/lib/action_view/helpers/form_helper.rb | 20 ++++++++++---------- .../lib/action_view/helpers/form_options_helper.rb | 8 ++++---- 3 files changed, 17 insertions(+), 17 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index fa3e612fc1..50ae27da61 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -153,7 +153,7 @@ module ActionView # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month # choices are valid. def date_select(object_name, method, options = {}, html_options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_date_select_tag(options, html_options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options) end # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified @@ -188,7 +188,7 @@ module ActionView # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month # choices are valid. def time_select(object_name, method, options = {}, html_options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_time_select_tag(options, html_options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_time_select_tag(options, html_options) end # Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based @@ -214,7 +214,7 @@ module ActionView # # The selects are prepared for multi-parameter assignment to an Active Record object. def datetime_select(object_name, method, options = {}, html_options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_datetime_select_tag(options, html_options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_datetime_select_tag(options, html_options) end # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+. diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 63a932320e..fa5a9bfac6 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -333,7 +333,7 @@ module ActionView # # => # def label(object_name, method, text = nil, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(text, options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options) end # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object @@ -355,7 +355,7 @@ module ActionView # # => # def text_field(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("text", options) end # Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object @@ -377,7 +377,7 @@ module ActionView # # => # def password_field(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("password", options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("password", options) end # Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object @@ -395,7 +395,7 @@ module ActionView # hidden_field(:user, :token) # # => def hidden_field(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("hidden", options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("hidden", options) end # Returns an file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object @@ -414,7 +414,7 @@ module ActionView # # => # def file_field(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("file", options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("file", options) end # Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+) @@ -442,7 +442,7 @@ module ActionView # # #{@entry.body} # # def text_area(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_text_area_tag(options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_text_area_tag(options) end # Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object @@ -468,7 +468,7 @@ module ActionView # # # def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value) end # Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object @@ -488,7 +488,7 @@ module ActionView # # => # # def radio_button(object_name, method, tag_value, options = {}) - InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_radio_button_tag(tag_value, options) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_radio_button_tag(tag_value, options) end end @@ -501,9 +501,9 @@ module ActionView DEFAULT_RADIO_OPTIONS = { }.freeze unless const_defined?(:DEFAULT_RADIO_OPTIONS) DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 }.freeze unless const_defined?(:DEFAULT_TEXT_AREA_OPTIONS) - def initialize(object_name, method_name, template_object, local_binding = nil, object = nil) + def initialize(object_name, method_name, template_object, object = nil) @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup - @template_object, @local_binding = template_object, local_binding + @template_object= template_object @object = object if @object_name.sub!(/\[\]$/,"") if object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 0ca5cebcba..ab9e174621 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -96,7 +96,7 @@ module ActionView # By default, post.person_id is the selected option. Specify :selected => value to use a different selection # or :selected => nil to leave all options unselected. def select(object, method, choices, options = {}, html_options = {}) - InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options) + InstanceTag.new(object, method, self, options.delete(:object)).to_select_tag(choices, options, html_options) end # Returns def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) - InstanceTag.new(object, method, self, nil, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options) + InstanceTag.new(object, method, self, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options) end # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags. def country_select(object, method, priority_countries = nil, options = {}, html_options = {}) - InstanceTag.new(object, method, self, nil, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options) + InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options) end # Return select and option tags for the given object and method, using @@ -169,7 +169,7 @@ module ActionView # # time_zone_select( "user", "time_zone", TZInfo::Timezone.all.sort, :model => TZInfo::Timezone) def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {}) - InstanceTag.new(object, method, self, nil, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options) + InstanceTag.new(object, method, self, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options) end # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container -- cgit v1.2.3 From 12cf8f348b591b7bb0dc899345293a8e37ddad7c Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 27 Jun 2008 21:24:21 +0300 Subject: Move template_format logic out to the request so it's alongside the 'regular' request format. Use xhr? instead of the expensive trip through Request#accepts. --- actionpack/lib/action_controller/request.rb | 13 +++++++++++++ actionpack/lib/action_view/base.rb | 12 +----------- actionpack/test/controller/new_render_test.rb | 3 +-- 3 files changed, 15 insertions(+), 13 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 9b02f2c8a1..c91a3387a0 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -116,6 +116,19 @@ module ActionController @format = Mime::Type.lookup_by_extension(parameters[:format]) end + def template_format + parameter_format = parameters[:format] + + case + when parameter_format.blank? && !xhr? + :html + when parameter_format.blank? && xhr? + :js + else + parameter_format.to_sym + end + end + # Returns true if the request's "X-Requested-With" header contains # "XMLHttpRequest". (The Prototype Javascript library sends this header with # every Ajax request.) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 9e255bd324..e8e690abec 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -273,17 +273,7 @@ module ActionView #:nodoc: return @template_format if @template_format if controller && controller.respond_to?(:request) - parameter_format = controller.request.parameters[:format] - accept_format = controller.request.accepts.first - - case - when parameter_format.blank? && accept_format != :js - @template_format = :html - when parameter_format.blank? && accept_format == :js - @template_format = :js - else - @template_format = parameter_format.to_sym - end + @template_format = controller.request.template_format else @template_format = :html end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index d60bacd52a..b2691d981b 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -605,8 +605,7 @@ EOS end def test_render_with_default_from_accept_header - @request.env["HTTP_ACCEPT"] = "text/javascript" - get :greeting + xhr :get, :greeting assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body end -- cgit v1.2.3 From d79cde37ea9a14fc6625297f40050296af7f7630 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Tue, 1 Jul 2008 20:37:21 +0300 Subject: Move the file exists checks outside write_asset_file_contents. This lets us avoid the relatively costly trip through compute_*_paths if the file already exists. --- actionpack/lib/action_view/helpers/asset_tag_helper.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index e5a95a961c..0122de47af 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -243,7 +243,7 @@ module ActionView joined_javascript_name = (cache == true ? "all" : cache) + ".js" joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name) - write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources)) + write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources)) unless File.exists?(joined_javascript_path) javascript_src_tag(joined_javascript_name, options) else expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n") @@ -370,7 +370,7 @@ module ActionView joined_stylesheet_name = (cache == true ? "all" : cache) + ".css" joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name) - write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources)) + write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources)) unless File.exists?(joined_stylesheet_path) stylesheet_tag(joined_stylesheet_name, options) else expand_stylesheet_sources(sources).collect { |source| stylesheet_tag(source, options) }.join("\n") @@ -601,10 +601,8 @@ module ActionView end def write_asset_file_contents(joined_asset_path, asset_paths) - unless file_exist?(joined_asset_path) - FileUtils.mkdir_p(File.dirname(joined_asset_path)) - File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) } - end + FileUtils.mkdir_p(File.dirname(joined_asset_path)) + File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) } end end end -- cgit v1.2.3 From 75e04b52956512d554c83e0134a81c980c15b4fa Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Tue, 1 Jul 2008 20:43:57 +0300 Subject: Tighten the rescue clause when dealing with invalid instance variable names in form_helper. --- actionpack/lib/action_view/helpers/form_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index fa5a9bfac6..bafc635ad2 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -601,7 +601,11 @@ module ActionView end def object - @object || (@template_object.instance_variable_get("@#{@object_name}") rescue nil) + @object || @template_object.instance_variable_get("@#{@object_name}") + rescue NameError + # As @object_name may contain the nested syntax (item[subobject]) we + # need to fallback to nil. + nil end def value(object) -- cgit v1.2.3 From 42d215a925a228778e43f7040f03ad8f3eb5341c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 12:48:00 -0500 Subject: Moved TemplateHandlers to Base --- actionpack/lib/action_view/base.rb | 1 + actionpack/lib/action_view/helpers/cache_helper.rb | 2 +- actionpack/lib/action_view/inline_template.rb | 2 +- actionpack/lib/action_view/template.rb | 7 +++++-- actionpack/test/controller/layout_test.rb | 2 +- actionpack/test/template/render_test.rb | 8 ++++---- 6 files changed, 13 insertions(+), 9 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index e8e690abec..169bee1bfc 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -151,6 +151,7 @@ module ActionView #:nodoc: # # See the ActionView::Helpers::PrototypeHelper::GeneratorMethods documentation for more details. class Base + extend TemplateHandlers include ERB::Util attr_accessor :base_path, :assigns, :template_extension, :first_render diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb index 930c397785..c2aab5aa72 100644 --- a/actionpack/lib/action_view/helpers/cache_helper.rb +++ b/actionpack/lib/action_view/helpers/cache_helper.rb @@ -32,7 +32,7 @@ module ActionView # Topics listed alphabetically # <% end %> def cache(name = {}, options = nil, &block) - handler = Template.handler_class_for_extension(current_render_extension.to_sym) + handler = Base.handler_class_for_extension(current_render_extension.to_sym) handler.new(@controller).cache_fragment(block, name, options) end end diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb index fd0ad48302..3a97f47e32 100644 --- a/actionpack/lib/action_view/inline_template.rb +++ b/actionpack/lib/action_view/inline_template.rb @@ -7,7 +7,7 @@ module ActionView #:nodoc: @extension = type @locals = locals || {} - @handler = self.class.handler_class_for_extension(@extension).new(@view) + @handler = Base.handler_class_for_extension(@extension).new(@view) end def method_key diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 0cba1942f7..d4118f0f86 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -1,6 +1,9 @@ module ActionView #:nodoc: class Template #:nodoc: - extend TemplateHandlers + class << self + # TODO: Deprecate + delegate :register_template_handler, :to => 'ActionView::Base' + end attr_accessor :locals attr_reader :handler, :path, :extension, :filename, :method @@ -20,7 +23,7 @@ module ActionView #:nodoc: set_extension_and_file_name @locals = locals || {} - @handler = self.class.handler_class_for_extension(@extension).new(@view) + @handler = Base.handler_class_for_extension(@extension).new(@view) end def render_template diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 3dc311b78a..52ab72bb99 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -40,7 +40,7 @@ class MabView < ActionView::TemplateHandler end end -ActionView::Template::register_template_handler :mab, MabView +ActionView::Base.register_template_handler :mab, MabView class LayoutAutoDiscoveryTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 726cf37026..0dcf88da83 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -101,12 +101,12 @@ class ViewRenderTest < Test::Unit::TestCase end def test_render_inline_with_custom_type - ActionView::Template.register_template_handler :foo, CustomHandler + ActionView::Base.register_template_handler :foo, CustomHandler assert_equal '["Hello, World!", {}]', @view.render(:inline => "Hello, World!", :type => :foo) end def test_render_inline_with_locals_and_custom_type - ActionView::Template.register_template_handler :foo, CustomHandler + ActionView::Base.register_template_handler :foo, CustomHandler assert_equal '["Hello, <%= name %>!", {:name=>"Josh"}]', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) end @@ -121,12 +121,12 @@ class ViewRenderTest < Test::Unit::TestCase end def test_render_inline_with_compilable_custom_type - ActionView::Template.register_template_handler :foo, CompilableCustomHandler + ActionView::Base.register_template_handler :foo, CompilableCustomHandler assert_equal 'locals: {}, source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo) end def test_render_inline_with_locals_and_compilable_custom_type - ActionView::Template.register_template_handler :foo, CompilableCustomHandler + ActionView::Base.register_template_handler :foo, CompilableCustomHandler assert_equal 'locals: {:name=>"Josh"}, source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) end end -- cgit v1.2.3 From b6f89a8bf59a0e6227c054d0be49baedf7b9f530 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 12:49:54 -0500 Subject: Don't rely on view instance logger --- actionpack/lib/action_view/template_handlers/compilable.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb index 783456ab53..f436ebbe45 100644 --- a/actionpack/lib/action_view/template_handlers/compilable.rb +++ b/actionpack/lib/action_view/template_handlers/compilable.rb @@ -41,10 +41,10 @@ module ActionView file_name = template.filename || 'compiled-template' ActionView::Base::CompiledTemplates.module_eval(render_source, file_name, -line_offset) rescue Exception => e # errors from template code - if @view.logger - @view.logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}" - @view.logger.debug "Function body: #{render_source}" - @view.logger.debug "Backtrace: #{e.backtrace.join("\n")}" + if Base.logger + Base.logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}" + Base.logger.debug "Function body: #{render_source}" + Base.logger.debug "Backtrace: #{e.backtrace.join("\n")}" end raise ActionView::TemplateError.new(template, @view.assigns, e) -- cgit v1.2.3 From 7d5c8505f528f195433693d5074a00f7635955b2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 12:50:43 -0500 Subject: Use render on InlineTemplate --- actionpack/lib/action_view/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 169bee1bfc..d4802d7965 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -305,7 +305,7 @@ module ActionView #:nodoc: end def render_inline(text, local_assigns = {}, type = nil) - InlineTemplate.new(self, text, local_assigns, type).render_template + InlineTemplate.new(self, text, local_assigns, type).render end def wrap_content_for_layout(content) -- cgit v1.2.3 From 8a442e0d57fabedcaf3ded836cfc12f7067ead68 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 13:06:00 -0500 Subject: Extracted Template rendering logic into Renderer module --- actionpack/lib/action_view.rb | 2 ++ actionpack/lib/action_view/inline_template.rb | 9 ++++--- actionpack/lib/action_view/partial_template.rb | 2 +- actionpack/lib/action_view/renderer.rb | 29 ++++++++++++++++++++++ actionpack/lib/action_view/template.rb | 33 +++++--------------------- 5 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 actionpack/lib/action_view/renderer.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index 973020a768..49768fe264 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -24,6 +24,8 @@ require 'action_view/template_handlers' require 'action_view/template_file' require 'action_view/view_load_paths' + +require 'action_view/renderer' require 'action_view/template' require 'action_view/partial_template' require 'action_view/inline_template' diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb index 3a97f47e32..19ab92ce1a 100644 --- a/actionpack/lib/action_view/inline_template.rb +++ b/actionpack/lib/action_view/inline_template.rb @@ -1,5 +1,7 @@ module ActionView #:nodoc: - class InlineTemplate < Template #:nodoc: + class InlineTemplate #:nodoc: + include Renderer + def initialize(view, source, locals = {}, type = nil) @view = view @@ -7,11 +9,8 @@ module ActionView #:nodoc: @extension = type @locals = locals || {} + @method_key = @source @handler = Base.handler_class_for_extension(@extension).new(@view) end - - def method_key - @source - end end end diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 6ebe165a15..72f831e937 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -18,7 +18,7 @@ module ActionView #:nodoc: def render ActionController::Base.benchmark("Rendered #{@path.path_without_format_and_extension}", Logger::DEBUG, false) do - @handler.render(self) + super end end diff --git a/actionpack/lib/action_view/renderer.rb b/actionpack/lib/action_view/renderer.rb new file mode 100644 index 0000000000..e6c64d2749 --- /dev/null +++ b/actionpack/lib/action_view/renderer.rb @@ -0,0 +1,29 @@ +module ActionView + module Renderer + # TODO: Local assigns should not be tied to template instance + attr_accessor :locals + + # TODO: These readers should be private + attr_reader :filename, :source, :handler, :method_key, :method + + def render + prepare! + @handler.render(self) + end + + private + def prepare! + unless @prepared + @view.send(:evaluate_assigns) + @view.current_render_extension = @extension + + if @handler.compilable? + @handler.compile_template(self) # compile the given template, if necessary + @method = @view.method_names[method_key] # Set the method name for this template and run it + end + + @prepared = true + end + end + end +end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index d4118f0f86..8142232c8f 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -1,12 +1,13 @@ module ActionView #:nodoc: class Template #:nodoc: + include Renderer + class << self # TODO: Deprecate delegate :register_template_handler, :to => 'ActionView::Base' end - attr_accessor :locals - attr_reader :handler, :path, :extension, :filename, :method + attr_reader :path, :extension def initialize(view, path, use_full_path = nil, locals = {}) unless use_full_path == nil @@ -19,9 +20,10 @@ module ActionView #:nodoc: @original_path = path @path = TemplateFile.from_path(path) @view.first_render ||= @path.to_s - @source = nil # Don't read the source until we know that it is required + set_extension_and_file_name + @method_key = @filename @locals = locals || {} @handler = Base.handler_class_for_extension(@extension).new(@view) end @@ -38,37 +40,14 @@ module ActionView #:nodoc: end end - def render - prepare! - @handler.render(self) - end - - def path_without_extension - @path.path_without_extension - end - def source @source ||= File.read(self.filename) end - def method_key - @filename - end - def base_path_for_exception (@paths.find_load_path_for_path(@path) || @paths.first).to_s end - def prepare! - @view.send :evaluate_assigns - @view.current_render_extension = @extension - - if @handler.compilable? - @handler.compile_template(self) # compile the given template, if necessary - @method = @view.method_names[method_key] # Set the method name for this template and run it - end - end - private def set_extension_and_file_name @extension = @path.extension @@ -94,7 +73,7 @@ module ActionView #:nodoc: full_template_path = @original_path.include?('.') ? @original_path : "#{@original_path}.#{@view.template_format}.erb" display_paths = @paths.join(':') template_type = (@original_path =~ /layouts/i) ? 'layout' : 'template' - raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}") + raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}" end end end -- cgit v1.2.3 From 1a478923dc909bf7b6aea4f2ad49cbeee6dea259 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 14:01:45 -0500 Subject: Reduce the number of callsites for new TemplateFiles --- actionpack/lib/action_controller/base.rb | 5 +++-- actionpack/lib/action_view/base.rb | 28 ++++++++++++++++----------- actionpack/lib/action_view/view_load_paths.rb | 22 ++++++++++++--------- 3 files changed, 33 insertions(+), 22 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 009c9eec99..a4bade1bd5 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -1235,8 +1235,9 @@ module ActionController #:nodoc: end def template_exempt_from_layout?(template_name = default_template_name) - template_name = @template.send(:template_file_from_name, template_name) if @template - @@exempt_from_layout.any? { |ext| template_name.to_s =~ ext } + extension = @template && @template.pick_template_extension(template_name) + name_with_extension = !template_name.include?('.') && extension ? "#{template_name}.#{extension}" : template_name + @@exempt_from_layout.any? { |ext| name_with_extension =~ ext } end def default_template_name(action_name = self.action_name) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index d4802d7965..5a3fc4182f 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -284,6 +284,21 @@ module ActionView #:nodoc: view_paths.template_exists?(template_file_from_name(template_path)) end + # Gets the extension for an existing template with the given template_path. + # Returns the format with the extension if that template exists. + # + # pick_template_extension('users/show') + # # => 'html.erb' + # + # pick_template_extension('users/legacy') + # # => "rhtml" + # + def pick_template_extension(template_path) + if template = template_file_from_name(template_path) + template.extension + end + end + private # Renders the template present at template_path. The hash in local_assigns # is made available as local variables. @@ -336,19 +351,10 @@ module ActionView #:nodoc: def template_file_from_name(template_name) template_name = TemplateFile.from_path(template_name) - pick_template_extension(template_name) unless template_name.extension + pick_template(template_name) unless template_name.extension end - # Gets the extension for an existing template with the given template_path. - # Returns the format with the extension if that template exists. - # - # pick_template_extension('users/show') - # # => 'html.erb' - # - # pick_template_extension('users/legacy') - # # => "rhtml" - # - def pick_template_extension(file) + def pick_template(file) if f = self.view_paths.find_template_file_for_path(file.dup_with_extension(template_format)) || file_from_first_render(file) f elsif template_format == :js && f = self.view_paths.find_template_file_for_path(file.dup_with_extension(:html)) diff --git a/actionpack/lib/action_view/view_load_paths.rb b/actionpack/lib/action_view/view_load_paths.rb index f23ac665f1..6e439a009c 100644 --- a/actionpack/lib/action_view/view_load_paths.rb +++ b/actionpack/lib/action_view/view_load_paths.rb @@ -29,12 +29,10 @@ module ActionView #:nodoc: @paths.freeze end - # Tries to find the extension for the template name. - # If it does not it exist, tries again without the format extension - # find_template_file_for_partial_path('users/show') => 'html.erb' - # find_template_file_for_partial_path('users/legacy') => 'rhtml' - def find_template_file_for_partial_path(file) - @paths[file.path] || @paths[file.path_without_extension] || @paths[file.path_without_format_and_extension] + def find_template_file_for_partial_path(template_path, template_format) + @paths["#{template_path}.#{template_format}"] || + @paths[template_path] || + @paths[template_path.gsub(/\..*$/, '')] end private @@ -81,10 +79,10 @@ module ActionView #:nodoc: find { |path| path.paths[file.to_s] } end - def find_template_file_for_path(file) - file = TemplateFile.from_path(file) + def find_template_file_for_path(template_path) + template_path_without_extension, template_extension = path_and_extension(template_path.to_s) each do |path| - if f = path.find_template_file_for_partial_path(file) + if f = path.find_template_file_for_partial_path(template_path_without_extension, template_extension) return f end end @@ -95,5 +93,11 @@ module ActionView #:nodoc: def delete_paths!(paths) paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } } end + + # Splits the path and extension from the given template_path and returns as an array. + def path_and_extension(template_path) + template_path_without_extension = template_path.sub(/\.(\w+)$/, '') + [template_path_without_extension, $1] + end end end -- cgit v1.2.3