1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #6371 from ihid/remove_old_text_helper_api

Removed old text_helper apis for highlight, excerpt and word_wrap
This commit is contained in:
Rafael Mendonça França 2012-05-17 14:05:31 -07:00
commit b23ac936a6
3 changed files with 27 additions and 69 deletions

View file

@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## 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 * 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* defaults to ERb. In future releases, it will simply return the template contents. *Steve Klabnik*

View file

@ -101,17 +101,7 @@ module ActionView
# #
# highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>') # highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
# # => You searched for: <a href="search?q=rails">rails</a> # # => You searched for: <a href="search?q=rails">rails</a>
# def highlight(text, phrases, options = {})
# You can still use <tt>highlight</tt> with the old API that accepts the
# +highlighter+ as its optional third parameter:
#
# highlight('You searched for: rails', 'rails', '<a href="search?q=\1">\1</a>')
# # => You searched for: <a href="search?q=rails">rails</a>
def highlight(text, phrases, *args)
options = args.extract_options!
unless args.empty?
options[:highlighter] = args[0]
end
options[:highlighter] ||= '<mark>\1</mark>' options[:highlighter] ||= '<mark>\1</mark>'
text = sanitize(text) unless options[:sanitize] == false text = sanitize(text) unless options[:sanitize] == false
@ -143,20 +133,8 @@ module ActionView
# #
# excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ') # excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
# # => <chop> is also an example # # => <chop> is also an example
# def excerpt(text, phrase, options = {})
# You can still use <tt>excerpt</tt> 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, '<chop> ') # => <chop> is also an example
def excerpt(text, phrase, *args)
return unless text && phrase return unless text && phrase
options = args.extract_options!
unless args.empty?
options[:radius] = args[0]
options[:omission] = args[1]
end
radius = options[:radius] || 100 radius = options[:radius] || 100
omission = options[:omission] || "..." omission = options[:omission] || "..."
@ -206,17 +184,7 @@ module ActionView
# #
# word_wrap('Once upon a time', :line_width => 1) # word_wrap('Once upon a time', :line_width => 1)
# # => Once\nupon\na\ntime # # => Once\nupon\na\ntime
# def word_wrap(text, options = {})
# You can still use <tt>word_wrap</tt> 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
line_width = options[:line_width] || 80 line_width = options[:line_width] || 80
text.split("\n").collect do |line| text.split("\n").collect do |line|

View file

@ -110,7 +110,7 @@ class TextHelperTest < ActionView::TestCase
assert_equal( assert_equal(
"This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day", "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>') highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
) )
assert_equal( assert_equal(
@ -153,14 +153,7 @@ class TextHelperTest < ActionView::TestCase
end end
def test_highlight_with_multiple_phrases_in_one_pass def test_highlight_with_multiple_phrases_in_one_pass
assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>') assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), :highlighter => '<em>\1</em>')
end
def test_highlight_with_options_hash
assert_equal(
"This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
)
end end
def test_highlight_with_html def test_highlight_with_html
@ -191,40 +184,39 @@ class TextHelperTest < ActionView::TestCase
end end
def test_excerpt def test_excerpt
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 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", 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", 5)) assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5))
assert_nil excerpt("This is a beautiful morning", "day") assert_nil excerpt("This is a beautiful morning", "day")
end end
def test_excerpt_should_not_be_html_safe 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 end
def test_excerpt_in_borderline_cases def test_excerpt_in_borderline_cases
assert_equal("", excerpt("", "", 0)) assert_equal("", excerpt("", "", :radius => 0))
assert_equal("a", excerpt("a", "a", 0)) assert_equal("a", excerpt("a", "a", :radius => 0))
assert_equal("...b...", excerpt("abc", "b", 0)) assert_equal("...b...", excerpt("abc", "b", :radius => 0))
assert_equal("abc", excerpt("abc", "b", 1)) assert_equal("abc", excerpt("abc", "b", :radius => 1))
assert_equal("abc...", excerpt("abcd", "b", 1)) assert_equal("abc...", excerpt("abcd", "b", :radius => 1))
assert_equal("...abc", excerpt("zabc", "b", 1)) assert_equal("...abc", excerpt("zabc", "b", :radius => 1))
assert_equal("...abc...", excerpt("zabcd", "b", 1)) assert_equal("...abc...", excerpt("zabcd", "b", :radius => 1))
assert_equal("zabcd", excerpt("zabcd", "b", 2)) assert_equal("zabcd", excerpt("zabcd", "b", :radius => 2))
# excerpt strips the resulting string before ap-/prepending excerpt_string. # excerpt strips the resulting string before ap-/prepending excerpt_string.
# whether this behavior is meaningful when excerpt_string is not to be # whether this behavior is meaningful when excerpt_string is not to be
# appended is questionable. # appended is questionable.
assert_equal("zabcd", excerpt(" zabcd ", "b", 4)) assert_equal("zabcd", excerpt(" zabcd ", "b", :radius => 4))
assert_equal("...abc...", excerpt("z abc d", "b", 1)) assert_equal("...abc...", excerpt("z abc d", "b", :radius => 1))
end end
def test_excerpt_with_regex 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', :radius => 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))
end end
def test_excerpt_with_options_hash def test_excerpt_with_omission
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5)) assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5))
assert_equal( assert_equal(
"This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]", "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]",
@ -234,19 +226,15 @@ class TextHelperTest < ActionView::TestCase
end end
def test_excerpt_with_utf8 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 end
def test_word_wrap 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 end
def test_word_wrap_with_extra_newlines 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)) 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_word_wrap_with_options_hash
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
end end
def test_pluralization def test_pluralization