Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added TextHelper#word_wrap(text, line_length = 80) #1449 [tuxie@dekad…

…ance.se]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1465 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c608ebeade50d3e87e750486e4949e8c3bdf1549 1 parent e59bfc8
@dhh dhh authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added TextHelper#word_wrap(text, line_length = 80) #1449 [tuxie@dekadance.se]
+
* Added a fall-through action for form_remote_tag that'll be used in case Javascript is unavailable #1459 [Scott Barron]. Example:
form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") }
View
5 actionpack/lib/action_view/helpers/text_helper.rb
@@ -65,6 +65,11 @@ def pluralize(count, singular, plural = nil)
end
end
+ # Word wrap long lines to line_width.
+ def word_wrap(text, line_width = 80)
+ text.gsub(/\n/, "\n\n").gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
+ end
+
begin
require "redcloth"
View
4 actionpack/test/template/text_helper_test.rb
@@ -67,6 +67,10 @@ def test_excerpt
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
assert_nil excerpt("This is a beautiful morning", "day")
end
+
+ def test_word_wrap
+ assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
+ end
def test_pluralization
assert_equal("1 count", pluralize(1, "count"))
Please sign in to comment.
Something went wrong with that request. Please try again.