From 90ec863b1d62bade591e841efbb7120bcbe94031 Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 17 May 2012 21:52:49 +0100 Subject: [PATCH] Removed old text_helper apis for highlight, excerpt and word_wrap --- actionpack/CHANGELOG.md | 2 + .../lib/action_view/helpers/text_helper.rb | 38 +------------ actionpack/test/template/text_helper_test.rb | 56 ++++++++----------- 3 files changed, 27 insertions(+), 69 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 8df943139e353..3f2593710e3af 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Removed old text_helper apis for highlight, excerpt and word_wrap *Jeremy Walker* + * Templates without a handler extension now raises a deprecation warning but still defaults to ERb. In future releases, it will simply return the template contents. *Steve Klabnik* diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 67117077dcc30..da8c0d1de6065 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -101,17 +101,7 @@ def truncate(text, options = {}) # # highlight('You searched for: rails', 'rails', :highlighter => '\1') # # => You searched for: rails - # - # You can still use highlight with the old API that accepts the - # +highlighter+ as its optional third parameter: - # - # highlight('You searched for: rails', 'rails', '\1') - # # => You searched for: rails - def highlight(text, phrases, *args) - options = args.extract_options! - unless args.empty? - options[:highlighter] = args[0] - end + def highlight(text, phrases, options = {}) options[:highlighter] ||= '\1' text = sanitize(text) unless options[:sanitize] == false @@ -143,20 +133,8 @@ def highlight(text, phrases, *args) # # excerpt('This is also an example', 'an', :radius => 8, :omission => ' ') # # => is also an example - # - # You can still use excerpt with the old API that accepts the - # +radius+ as its optional third and the +ellipsis+ as its - # optional forth parameter: - # excerpt('This is an example', 'an', 5) # => ...s is an exam... - # excerpt('This is also an example', 'an', 8, ' ') # => is also an example - def excerpt(text, phrase, *args) + def excerpt(text, phrase, options = {}) return unless text && phrase - - options = args.extract_options! - unless args.empty? - options[:radius] = args[0] - options[:omission] = args[1] - end radius = options[:radius] || 100 omission = options[:omission] || "..." @@ -206,17 +184,7 @@ def pluralize(count, singular, plural = nil) # # word_wrap('Once upon a time', :line_width => 1) # # => Once\nupon\na\ntime - # - # You can still use word_wrap with the old API that accepts the - # +line_width+ as its optional second parameter: - # - # word_wrap('Once upon a time', 8) - # # => Once\nupon a\ntime - def word_wrap(text, *args) - options = args.extract_options! - unless args.blank? - options[:line_width] = args[0] - end + def word_wrap(text, options = {}) line_width = options[:line_width] || 80 text.split("\n").collect do |line| diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index e5cb273518392..66fab207109c1 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -110,7 +110,7 @@ def test_highlight assert_equal( "This is a beautiful morning, but also a beautiful day", - highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '\1') + highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '\1') ) assert_equal( @@ -153,14 +153,7 @@ def test_highlight_with_regexp end def test_highlight_with_multiple_phrases_in_one_pass - assert_equal %(wow em), highlight('wow em', %w(wow em), '\1') - end - - def test_highlight_with_options_hash - assert_equal( - "This is a beautiful morning, but also a beautiful day", - highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '\1') - ) + assert_equal %(wow em), highlight('wow em', %w(wow em), :highlighter => '\1') end def test_highlight_with_html @@ -191,40 +184,39 @@ def test_highlight_with_html end def test_excerpt - assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5)) - assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5)) - assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) + assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5)) + assert_equal("This is a...", excerpt("This is a beautiful morning", "this", :radius => 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5)) assert_nil excerpt("This is a beautiful morning", "day") end def test_excerpt_should_not_be_html_safe - assert !excerpt('This is a beautiful! morning', 'beautiful', 5).html_safe? + assert !excerpt('This is a beautiful! morning', 'beautiful', :radius => 5).html_safe? end def test_excerpt_in_borderline_cases - assert_equal("", excerpt("", "", 0)) - assert_equal("a", excerpt("a", "a", 0)) - assert_equal("...b...", excerpt("abc", "b", 0)) - assert_equal("abc", excerpt("abc", "b", 1)) - assert_equal("abc...", excerpt("abcd", "b", 1)) - assert_equal("...abc", excerpt("zabc", "b", 1)) - assert_equal("...abc...", excerpt("zabcd", "b", 1)) - assert_equal("zabcd", excerpt("zabcd", "b", 2)) + assert_equal("", excerpt("", "", :radius => 0)) + assert_equal("a", excerpt("a", "a", :radius => 0)) + assert_equal("...b...", excerpt("abc", "b", :radius => 0)) + assert_equal("abc", excerpt("abc", "b", :radius => 1)) + assert_equal("abc...", excerpt("abcd", "b", :radius => 1)) + assert_equal("...abc", excerpt("zabc", "b", :radius => 1)) + assert_equal("...abc...", excerpt("zabcd", "b", :radius => 1)) + assert_equal("zabcd", excerpt("zabcd", "b", :radius => 2)) # excerpt strips the resulting string before ap-/prepending excerpt_string. # whether this behavior is meaningful when excerpt_string is not to be # appended is questionable. - assert_equal("zabcd", excerpt(" zabcd ", "b", 4)) - assert_equal("...abc...", excerpt("z abc d", "b", 1)) + assert_equal("zabcd", excerpt(" zabcd ", "b", :radius => 4)) + assert_equal("...abc...", excerpt("z abc d", "b", :radius => 1)) end def test_excerpt_with_regex - assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', 5)) - assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5)) + assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', :radius => 5)) end - def test_excerpt_with_options_hash - assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5)) + def test_excerpt_with_omission assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5)) assert_equal( "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]", @@ -234,19 +226,15 @@ def test_excerpt_with_options_hash end def test_excerpt_with_utf8 - assert_equal("...\357\254\203ciency could not be...".force_encoding('UTF-8'), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding('UTF-8'), 'could', 8)) + assert_equal("...\357\254\203ciency could not be...".force_encoding('UTF-8'), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding('UTF-8'), 'could', :radius => 8)) end def test_word_wrap - assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15)) + assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15)) end def test_word_wrap_with_extra_newlines - assert_equal("my very very\nvery long\nstring\n\nwith another\nline", word_wrap("my very very very long string\n\nwith another line", 15)) - end - - def test_word_wrap_with_options_hash - assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15)) + assert_equal("my very very\nvery long\nstring\n\nwith another\nline", word_wrap("my very very very long string\n\nwith another line", :line_width => 15)) end def test_pluralization