Skip to content
This repository
Browse code

Tidied and standardised text_helper implementations.

  • Loading branch information...
commit 7e82ebd25eec470b57f8fba3aeafb597561091b6 1 parent 62c6ac2
Jeremy Walker iHiD authored
34 actionpack/lib/action_view/helpers/text_helper.rb
@@ -81,8 +81,7 @@ def safe_concat(string)
81 81 # truncate("<p>Once upon a time in a world far far away</p>")
82 82 # # => "<p>Once upon a time in a wo..."
83 83 def truncate(text, options = {})
84   - options.reverse_merge!(:length => 30)
85   - text.truncate(options.delete(:length), options) if text
  84 + text.truncate(options.fetch(:length, 30), options) if text
86 85 end
87 86
88 87 # Highlights one or more +phrases+ everywhere in +text+ by inserting it into
@@ -102,14 +101,14 @@ def truncate(text, options = {})
102 101 # highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
103 102 # # => You searched for: <a href="search?q=rails">rails</a>
104 103 def highlight(text, phrases, options = {})
105   - options[:highlighter] ||= '<mark>\1</mark>'
106   -
107   - text = sanitize(text) unless options[:sanitize] == false
  104 + highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
  105 +
  106 + text = sanitize(text) if options.fetch(:sanitize, true)
108 107 if text.blank? || phrases.blank?
109 108 text
110 109 else
111 110 match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
112   - text.gsub(/(#{match})(?![^<]*?>)/i, options[:highlighter])
  111 + text.gsub(/(#{match})(?![^<]*?>)/i, highlighter)
113 112 end.html_safe
114 113 end
115 114
@@ -135,8 +134,8 @@ def highlight(text, phrases, options = {})
135 134 # # => <chop> is also an example
136 135 def excerpt(text, phrase, options = {})
137 136 return unless text && phrase
138   - radius = options[:radius] || 100
139   - omission = options[:omission] || "..."
  137 + radius = options.fetch(:radius, 100)
  138 + omission = options.fetch(:omission, "...")
140 139
141 140 phrase = Regexp.escape(phrase)
142 141 return unless found_pos = text =~ /(#{phrase})/i
@@ -152,7 +151,7 @@ def excerpt(text, phrase, options = {})
152 151
153 152 # Attempts to pluralize the +singular+ word unless +count+ is 1. If
154 153 # +plural+ is supplied, it will use that when count is > 1, otherwise
155   - # it will use the Inflector to determine the plural form
  154 + # it will use the Inflector to determine the plural form.
156 155 #
157 156 # pluralize(1, 'person')
158 157 # # => 1 person
@@ -166,7 +165,13 @@ def excerpt(text, phrase, options = {})
166 165 # pluralize(0, 'person')
167 166 # # => 0 people
168 167 def pluralize(count, singular, plural = nil)
169   - "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
  168 + word = if (count == 1 || count =~ /^1(\.0+)?$/)
  169 + singular
  170 + else
  171 + plural || singular.pluralize
  172 + end
  173 +
  174 + "#{count || 0} #{word}"
170 175 end
171 176
172 177 # Wraps the +text+ into lines no longer than +line_width+ width. This method
@@ -185,7 +190,7 @@ def pluralize(count, singular, plural = nil)
185 190 # word_wrap('Once upon a time', :line_width => 1)
186 191 # # => Once\nupon\na\ntime
187 192 def word_wrap(text, options = {})
188   - line_width = options[:line_width] || 80
  193 + line_width = options.fetch(:line_width, 80)
189 194
190 195 text.split("\n").collect do |line|
191 196 line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
@@ -221,9 +226,10 @@ def word_wrap(text, options = {})
221 226 #
222 227 # simple_format("<span>I'm allowed!</span> It's true.", {}, :sanitize => false)
223 228 # # => "<p><span>I'm allowed!</span> It's true.</p>"
224   - def simple_format(text, html_options={}, options={})
225   - text = sanitize(text) unless options[:sanitize] == false
  229 + def simple_format(text, html_options = {}, options = {})
226 230 wrapper_tag = options.fetch(:wrapper_tag, :p)
  231 +
  232 + text = sanitize(text) if options.fetch(:sanitize, true)
227 233 paragraphs = split_paragraphs(text)
228 234
229 235 if paragraphs.empty?
@@ -274,7 +280,7 @@ def simple_format(text, html_options={}, options={})
274 280 # <% end %>
275 281 def cycle(first_value, *values)
276 282 options = values.extract_options!
277   - name = options.fetch(:name, "default")
  283 + name = options.fetch(:name, 'default')
278 284
279 285 values.unshift(first_value)
280 286
42 actionpack/test/template/text_helper_test.rb
@@ -60,6 +60,20 @@ def test_simple_format_should_not_change_the_text_passed
60 60 simple_format(text)
61 61 assert_equal text_clone, text
62 62 end
  63 +
  64 + def test_simple_format_does_not_modify_the_html_options_hash
  65 + options = { :class => "foobar"}
  66 + passed_options = options.dup
  67 + simple_format("some text", passed_options)
  68 + assert_equal options, passed_options
  69 + end
  70 +
  71 + def test_simple_format_does_not_modify_the_options_hash
  72 + options = { :wrapper_tag => :div, :sanitize => false }
  73 + passed_options = options.dup
  74 + simple_format("some text", {}, passed_options)
  75 + assert_equal options, passed_options
  76 + end
63 77
64 78 def test_truncate_should_not_be_html_safe
65 79 assert !truncate("Hello World!", :length => 12).html_safe?
@@ -92,6 +106,13 @@ def test_truncate_multibyte
92 106 assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'),
93 107 truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8'), :length => 10)
94 108 end
  109 +
  110 + def test_truncate_does_not_modify_the_options_hash
  111 + options = { :length => 10 }
  112 + passed_options = options.dup
  113 + truncate("some text", passed_options)
  114 + assert_equal options, passed_options
  115 + end
95 116
96 117 def test_highlight_should_be_html_safe
97 118 assert highlight("This is a beautiful morning", "beautiful").html_safe?
@@ -182,6 +203,13 @@ def test_highlight_with_html
182 203 highlight("<div>abc div</div>", "div", :highlighter => '<b>\1</b>')
183 204 )
184 205 end
  206 +
  207 + def test_highlight_does_not_modify_the_options_hash
  208 + options = { :highlighter => '<b>\1</b>', :sanitize => false }
  209 + passed_options = options.dup
  210 + highlight("<div>abc div</div>", "div", passed_options)
  211 + assert_equal options, passed_options
  212 + end
185 213
186 214 def test_excerpt
187 215 assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
@@ -228,6 +256,13 @@ def test_excerpt_with_omission
228 256 def test_excerpt_with_utf8
229 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', :radius => 8))
230 258 end
  259 +
  260 + def test_excerpt_does_not_modify_the_options_hash
  261 + options = { :omission => "[...]",:radius => 5 }
  262 + passed_options = options.dup
  263 + excerpt("This is a beautiful morning", "beautiful", passed_options)
  264 + assert_equal options, passed_options
  265 + end
231 266
232 267 def test_word_wrap
233 268 assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
@@ -236,6 +271,13 @@ def test_word_wrap
236 271 def test_word_wrap_with_extra_newlines
237 272 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))
238 273 end
  274 +
  275 + def test_word_wrap_does_not_modify_the_options_hash
  276 + options = { :line_width => 15 }
  277 + passed_options = options.dup
  278 + word_wrap("some text", passed_options)
  279 + assert_equal options, passed_options
  280 + end
239 281
240 282 def test_pluralization
241 283 assert_equal("1 count", pluralize(1, "count"))

0 comments on commit 7e82ebd

Please sign in to comment.
Something went wrong with that request. Please try again.