From 78fdd4b67dd7c1f59ee3e18eb155c260a6b9e8fd Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Mon, 15 Mar 2010 17:40:39 -0700 Subject: [PATCH 1/5] [Haml] Deprecate '- form_for' in Rails 3. --- doc-src/HAML_CHANGELOG.md | 4 ++++ lib/haml/template/plugin.rb | 31 +++++++++++++++++++++++++++++++ test/haml/template_test.rb | 22 ++++++++++++++++++++++ test/test_helper.rb | 7 ++++++- 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md index bbf5d609..036070a9 100644 --- a/doc-src/HAML_CHANGELOG.md +++ b/doc-src/HAML_CHANGELOG.md @@ -8,6 +8,10 @@ * Add a railtie so Haml and Sass will be automatically loaded in Rails 3. Thanks to [Daniel Neighman](http://pancakestacks.wordpress.com/). +* Add a deprecation message for using `-` with methods like `form_for` + that return strings in Rails 3. + This is [the same deprecation that exists in Rails 3](http://github.com/rails/rails/commit/9de83050d3a4b260d4aeb5d09ec4eb64f913ba64). + ## 2.2.21 [Tagged on GitHub](http://github.com/nex3/haml/commit/2.2.21). diff --git a/lib/haml/template/plugin.rb b/lib/haml/template/plugin.rb index 0a8ce842..7a0c5d75 100644 --- a/lib/haml/template/plugin.rb +++ b/lib/haml/template/plugin.rb @@ -33,6 +33,37 @@ module Haml end end end + + # Rails 3.0 prints a deprecation warning when block helpers + # return strings that go unused. + # We want to print the same deprecation warning, + # so we have to compile in a method call to check for it. + # + # I don't like having this in the precompiler pipeline, + # and I'd like to get rid of it once Rails 3.1 is well-established. + if defined?(ActionView::OutputBuffer) && + Haml::Util.has?(:instance_method, ActionView::OutputBuffer, :append_if_string=) + module Precompiler + def push_silent_with_haml_block_deprecation(text, *args) + unless block_opened? && text =~ ActionView::Template::Handlers::Erubis::BLOCK_EXPR + return push_silent_without_haml_block_deprecation(text, *args) + end + + push_silent_without_haml_block_deprecation("_hamlout.append_if_string= #{text}", *args) + end + alias_method :push_silent_without_haml_block_deprecation, :push_silent + alias_method :push_silent, :push_silent_with_haml_block_deprecation + end + + class Buffer + def append_if_string=(value) + if value.is_a?(String) && !value.is_a?(ActionView::NonConcattingString) + ActiveSupport::Deprecation.warn("- style block helpers are deprecated. Please use =", caller) + buffer << value + end + end + end + end end if defined? ActionView::Template and ActionView::Template.respond_to? :register_template_handler diff --git a/test/haml/template_test.rb b/test/haml/template_test.rb index d753327a..3fdf701f 100755 --- a/test/haml/template_test.rb +++ b/test/haml/template_test.rb @@ -250,6 +250,28 @@ END end end + if defined?(ActionView::OutputBuffer) && + Haml::Util.has?(:instance_method, ActionView::OutputBuffer, :append_if_string=) + def test_av_block_deprecation_warning + assert_warning(/^DEPRECATION WARNING: - style block helpers are deprecated\. Please use =\./) do + assert_equal < + Title: + + Body: + + +HTML +- form_for :article, @article, :url => '' do |f| + Title: + = f.text_field :title + Body: + = f.text_field :body +HAML + end + end + end + ## XSS Protection Tests # In order to enable these, either test against Rails 3.0 diff --git a/test/test_helper.rb b/test/test_helper.rb index b2147310..bf835f8e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -24,7 +24,12 @@ class Test::Unit::TestCase def assert_warning(message) the_real_stderr, $stderr = $stderr, StringIO.new yield - assert_equal message.strip, $stderr.string.strip + + if message.is_a?(Regexp) + assert_match message, $stderr.string.strip + else + assert_equal message.strip, $stderr.string.strip + end ensure $stderr = the_real_stderr end From 7a5b66d41670f11313e5f90434089bd06420464b Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 16 Mar 2010 16:57:50 -0700 Subject: [PATCH 2/5] [Haml] Don't be overzealous about using _hamlout.append_if_string=. --- lib/haml/template/plugin.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/haml/template/plugin.rb b/lib/haml/template/plugin.rb index 7a0c5d75..36d735ec 100644 --- a/lib/haml/template/plugin.rb +++ b/lib/haml/template/plugin.rb @@ -44,12 +44,13 @@ module Haml if defined?(ActionView::OutputBuffer) && Haml::Util.has?(:instance_method, ActionView::OutputBuffer, :append_if_string=) module Precompiler - def push_silent_with_haml_block_deprecation(text, *args) - unless block_opened? && text =~ ActionView::Template::Handlers::Erubis::BLOCK_EXPR - return push_silent_without_haml_block_deprecation(text, *args) + def push_silent_with_haml_block_deprecation(text, can_suppress = false) + unless can_suppress && block_opened? && + text =~ ActionView::Template::Handlers::Erubis::BLOCK_EXPR + return push_silent_without_haml_block_deprecation(text, can_suppress) end - push_silent_without_haml_block_deprecation("_hamlout.append_if_string= #{text}", *args) + push_silent_without_haml_block_deprecation("_hamlout.append_if_string= #{text}", can_suppress) end alias_method :push_silent_without_haml_block_deprecation, :push_silent alias_method :push_silent, :push_silent_with_haml_block_deprecation From 25bb3802d09115053245b35a30a69ef2e7466478 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 16 Mar 2010 16:58:26 -0700 Subject: [PATCH 3/5] [Haml] Use = with render :partial :layout in Rails 3. --- test/haml/templates/partial_layout.haml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/haml/templates/partial_layout.haml b/test/haml/templates/partial_layout.haml index b6373b3f..ab4999f0 100644 --- a/test/haml/templates/partial_layout.haml +++ b/test/haml/templates/partial_layout.haml @@ -1,3 +1,7 @@ %h1 Partial layout used with for block: -- render :layout => 'layout_for_partial.haml' do - %p Some content within a layout \ No newline at end of file +- if Haml::Util.ap_geq_3? + = render :layout => 'layout_for_partial.haml' do + %p Some content within a layout +- else + - render :layout => 'layout_for_partial.haml' do + %p Some content within a layout From 342e9a7b2cb8d3d3ed657a695b7aaa546672f132 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 16 Mar 2010 17:06:48 -0700 Subject: [PATCH 4/5] [Haml] Don't check for strings returned by ends with block methods. --- lib/haml/template/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/haml/template/plugin.rb b/lib/haml/template/plugin.rb index 36d735ec..828cb1ba 100644 --- a/lib/haml/template/plugin.rb +++ b/lib/haml/template/plugin.rb @@ -45,7 +45,7 @@ module Haml Haml::Util.has?(:instance_method, ActionView::OutputBuffer, :append_if_string=) module Precompiler def push_silent_with_haml_block_deprecation(text, can_suppress = false) - unless can_suppress && block_opened? && + unless can_suppress && block_opened? && !mid_block_keyword?("- #{text}") && text =~ ActionView::Template::Handlers::Erubis::BLOCK_EXPR return push_silent_without_haml_block_deprecation(text, can_suppress) end From 5db91850d7ef5712edbf751d4600b894104b8958 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 16 Mar 2010 16:58:10 -0700 Subject: [PATCH 5/5] [Haml] Make sure block helpers in tests don't return strings. --- test/haml/engine_test.rb | 3 ++- test/haml/helper_test.rb | 9 ++++++++- test/haml/template_test.rb | 5 ++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index 9947d8c5..34aa4b51 100755 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -599,8 +599,9 @@ HAML

foo-end

bar-end

HTML -- "foo-end-bar-end".gsub(/\\w+-end/) do |s| +- ("foo-end-bar-end".gsub(/\\w+-end/) do |s| %p= s +- end; nil) HAML end diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index ef75d609..4754b9c8 100755 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -131,7 +131,14 @@ HAML end def test_capture_haml - assert_equal("\"

13

\\n\"\n", render("- foo = capture_haml(13) do |a|\n %p= a\n= foo.dump")) + assert_equal(<13

\\n" +HTML +- (foo = capture_haml(13) do |a| + %p= a +- end; nil) += foo.dump +HAML end def test_content_tag_block diff --git a/test/haml/template_test.rb b/test/haml/template_test.rb index 3fdf701f..e15bd658 100755 --- a/test/haml/template_test.rb +++ b/test/haml/template_test.rb @@ -211,10 +211,13 @@ baz HTML %p foo - - with_output_buffer do + -# Parenthesis required due to Rails 3.0 deprecation of block helpers + -# that return strings. + - (with_output_buffer do bar = "foo".gsub(/./) do |s| - "flup" + - end; nil) baz HAML end