aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-09-08 09:15:49 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-09-08 09:22:40 -0300
commit7b31a430858c40238dec0a7530937a4b7ee95c7b (patch)
treee4f4243bfe98d6d31b45c96ac90569cd3d7595d4
parentaaa33e6f4948130be24689f9c3609f249556b307 (diff)
downloadrails-7b31a430858c40238dec0a7530937a4b7ee95c7b.tar.gz
rails-7b31a430858c40238dec0a7530937a4b7ee95c7b.tar.bz2
rails-7b31a430858c40238dec0a7530937a4b7ee95c7b.zip
Tidy up excerpt separator logic a bit
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb28
1 files changed, 11 insertions, 17 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 069969a710..527bfe0cab 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -149,10 +149,8 @@ module ActionView
# # => ...a very beautiful...
def excerpt(text, phrase, options = {})
return unless text && phrase
- radius = options.fetch(:radius, 100)
- omission = options.fetch(:omission, "...")
- separator = options.fetch(:separator, "")
+ separator = options.fetch(:separator, "")
phrase = Regexp.escape(phrase)
regex = /#{phrase}/i
@@ -168,11 +166,8 @@ module ActionView
first_part, second_part = text.split(regex, 2)
- options = options.merge(:part_position => :first)
- prefix, first_part = cut_part(first_part, options)
-
- options = options.merge(:part_position => :second)
- postfix, second_part = cut_part(second_part, options)
+ prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
+ postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
prefix + (first_part + separator + phrase + separator + second_part).strip + postfix
end
@@ -420,25 +415,24 @@ module ActionView
end
end
- def cut_part(part, options)
- radius = options.fetch(:radius, 100)
- omission = options.fetch(:omission, "...")
- separator = options.fetch(:separator, "")
- part_position = options.fetch(:part_position)
-
+ def cut_excerpt_part(part_position, part, separator, options)
return "", "" unless part
+ radius = options.fetch(:radius, 100)
+ omission = options.fetch(:omission, "...")
+
part = part.split(separator)
part.delete("")
affix = part.size > radius ? omission : ""
+
part = if part_position == :first
drop_index = [part.length - radius, 0].max
- part.drop(drop_index).join(separator)
+ part.drop(drop_index)
else
- part.first(radius).join(separator)
+ part.first(radius)
end
- return affix, part
+ return affix, part.join(separator)
end
end
end