Skip to content

Commit 616c91d

Browse files
author
Jeremy Walker
committed
Deprecate old APIs for highlight, excerpt and word_wrap
1 parent 75e8e77 commit 616c91d

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

actionpack/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Rails 3.2.4 (unreleased) ##
22

3+
* Deprecate old APIs for highlight, excerpt and word_wrap *Jeremy Walker*
4+
35
* Deprecate `:disable_with` in favor of `'data-disable-with'` option for `button_to`, `button_tag` and `submit_tag` helpers.
46

57
*Carlos Galdino + Rafael Mendonça França*

actionpack/lib/action_view/helpers/text_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def truncate(text, options = {})
111111
def highlight(text, phrases, *args)
112112
options = args.extract_options!
113113
unless args.empty?
114+
ActiveSupport::Deprecation.warn "Calling highlight with a highlighter as an argument is deprecated. " \
115+
"Please call with :highlighter => '#{args[0]}' instead.", caller
116+
114117
options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>'
115118
end
116119
options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
@@ -156,6 +159,9 @@ def excerpt(text, phrase, *args)
156159

157160
options = args.extract_options!
158161
unless args.empty?
162+
ActiveSupport::Deprecation.warn "Calling excerpt with radius and omission as arguments is deprecated. " \
163+
"Please call with :radius => #{args[0]}#{", :omission => '#{args[1]}'" if args[1]} instead.", caller
164+
159165
options[:radius] = args[0] || 100
160166
options[:omission] = args[1] || "..."
161167
end
@@ -217,6 +223,9 @@ def pluralize(count, singular, plural = nil)
217223
def word_wrap(text, *args)
218224
options = args.extract_options!
219225
unless args.blank?
226+
ActiveSupport::Deprecation.warn "Calling word_wrap with line_width as an argument is deprecated. " \
227+
"Please call with :line_width => #{args[0]} instead.", caller
228+
220229
options[:line_width] = args[0] || 80
221230
end
222231
options.reverse_merge!(:line_width => 80)

actionpack/test/template/text_helper_test.rb

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_highlight
120120

121121
assert_equal(
122122
"This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
123-
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>')
123+
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
124124
)
125125

126126
assert_equal(
@@ -130,6 +130,12 @@ def test_highlight
130130

131131
assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
132132
end
133+
134+
def test_highlight_old_api_is_depcrecated
135+
assert_deprecated("Calling highlight with a highlighter as an argument is deprecated. Please call with :highlighter => '<mark>\\1</mark>' instead.") do
136+
highlight("This is a beautiful morning", "beautiful", '<mark>\1</mark>')
137+
end
138+
end
133139

134140
def test_highlight_should_sanitize_input
135141
assert_equal(
@@ -163,14 +169,7 @@ def test_highlight_with_regexp
163169
end
164170

165171
def test_highlight_with_multiple_phrases_in_one_pass
166-
assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
167-
end
168-
169-
def test_highlight_with_options_hash
170-
assert_equal(
171-
"This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
172-
highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
173-
)
172+
assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), :highlighter => '<em>\1</em>')
174173
end
175174

176175
def test_highlight_with_html
@@ -201,40 +200,48 @@ def test_highlight_with_html
201200
end
202201

203202
def test_excerpt
204-
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5))
205-
assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
206-
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
203+
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
204+
assert_equal("This is a...", excerpt("This is a beautiful morning", "this", :radius => 5))
205+
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5))
207206
assert_nil excerpt("This is a beautiful morning", "day")
208207
end
208+
209+
def test_excerpt_old_api_is_depcrecated
210+
assert_deprecated("Calling excerpt with radius and omission as arguments is deprecated. Please call with :radius => 5 instead.") do
211+
excerpt("This is a beautiful morning", "morning", 5)
212+
end
213+
assert_deprecated("Calling excerpt with radius and omission as arguments is deprecated. Please call with :radius => 5, :omission => 'mor' instead.") do
214+
excerpt("This is a beautiful morning", "morning", 5, "mor")
215+
end
216+
end
209217

210218
def test_excerpt_should_not_be_html_safe
211-
assert !excerpt('This is a beautiful! morning', 'beautiful', 5).html_safe?
219+
assert !excerpt('This is a beautiful! morning', 'beautiful', :radius => 5).html_safe?
212220
end
213221

214222
def test_excerpt_in_borderline_cases
215-
assert_equal("", excerpt("", "", 0))
216-
assert_equal("a", excerpt("a", "a", 0))
217-
assert_equal("...b...", excerpt("abc", "b", 0))
218-
assert_equal("abc", excerpt("abc", "b", 1))
219-
assert_equal("abc...", excerpt("abcd", "b", 1))
220-
assert_equal("...abc", excerpt("zabc", "b", 1))
221-
assert_equal("...abc...", excerpt("zabcd", "b", 1))
222-
assert_equal("zabcd", excerpt("zabcd", "b", 2))
223+
assert_equal("", excerpt("", "", :radius => 0))
224+
assert_equal("a", excerpt("a", "a", :radius => 0))
225+
assert_equal("...b...", excerpt("abc", "b", :radius => 0))
226+
assert_equal("abc", excerpt("abc", "b", :radius => 1))
227+
assert_equal("abc...", excerpt("abcd", "b", :radius => 1))
228+
assert_equal("...abc", excerpt("zabc", "b", :radius => 1))
229+
assert_equal("...abc...", excerpt("zabcd", "b", :radius => 1))
230+
assert_equal("zabcd", excerpt("zabcd", "b", :radius => 2))
223231

224232
# excerpt strips the resulting string before ap-/prepending excerpt_string.
225233
# whether this behavior is meaningful when excerpt_string is not to be
226234
# appended is questionable.
227-
assert_equal("zabcd", excerpt(" zabcd ", "b", 4))
228-
assert_equal("...abc...", excerpt("z abc d", "b", 1))
235+
assert_equal("zabcd", excerpt(" zabcd ", "b", :radius => 4))
236+
assert_equal("...abc...", excerpt("z abc d", "b", :radius => 1))
229237
end
230238

231239
def test_excerpt_with_regex
232-
assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', 5))
233-
assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5))
240+
assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', :radius => 5))
241+
assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', :radius => 5))
234242
end
235243

236-
def test_excerpt_with_options_hash
237-
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
244+
def test_excerpt_with_omission
238245
assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5))
239246
assert_equal(
240247
"This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]",
@@ -246,30 +253,32 @@ def test_excerpt_with_options_hash
246253
if RUBY_VERSION < '1.9'
247254
def test_excerpt_with_utf8
248255
with_kcode('u') do
249-
assert_equal("...\357\254\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', 8))
256+
assert_equal("...\357\254\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', :line_width => 8))
250257
end
251258
with_kcode('none') do
252-
assert_equal("...\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', 8))
259+
assert_equal("...\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', :line_width => 8))
253260
end
254261
end
255262
else
256263
def test_excerpt_with_utf8
257-
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))
264+
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))
258265
# .mb_chars always returns UTF-8, even in 1.9. This is not great, but it's how it works. Let's work this out.
259266
# assert_equal("...\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped".force_encoding("BINARY"), 'could', 8))
260267
end
261268
end
262269

263270
def test_word_wrap
264-
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
271+
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
265272
end
266-
267-
def test_word_wrap_with_extra_newlines
268-
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))
273+
274+
def test_word_wrap_old_api_is_depcrecated
275+
assert_deprecated("Calling word_wrap with line_width as an argument is deprecated. Please call with :line_width => 15 instead.") do
276+
word_wrap("my very very very long string", 15)
277+
end
269278
end
270279

271-
def test_word_wrap_with_options_hash
272-
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
280+
def test_word_wrap_with_extra_newlines
281+
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))
273282
end
274283

275284
def test_pluralization

0 commit comments

Comments
 (0)