Skip to content

Commit

Permalink
[Haml] Deprecate '- form_for' in Rails 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Mar 16, 2010
1 parent d4d4390 commit 78fdd4b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc-src/HAML_CHANGELOG.md
Expand Up @@ -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).
Expand Down
31 changes: 31 additions & 0 deletions lib/haml/template/plugin.rb
Expand Up @@ -33,6 +33,37 @@ def cache_fragment(block, name = {}, options = nil)
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
Expand Down
22 changes: 22 additions & 0 deletions test/haml/template_test.rb
Expand Up @@ -250,6 +250,28 @@ def test_exceptions_should_work_correctly
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 <<HTML, render(<<HAML, :action_view)
<form action="" method="post">
Title:
<input id="article_title" name="article[title]" size="30" type="text" value="Hello" />
Body:
<input id="article_body" name="article[body]" size="30" type="text" value="World" />
</form>
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
Expand Down
7 changes: 6 additions & 1 deletion test/test_helper.rb
Expand Up @@ -24,7 +24,12 @@ def clean_up_sassc
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
Expand Down

0 comments on commit 78fdd4b

Please sign in to comment.