diff options
author | Alexey Gaziev <alex.gaziev@gmail.com> | 2012-04-26 17:20:36 +0400 |
---|---|---|
committer | Alexey Gaziev <alex.gaziev@gmail.com> | 2012-04-29 03:09:44 +0400 |
commit | 1946d7b9229fabb52226f9ff82de72872c748d90 (patch) | |
tree | 9d45b28547accec3c53d177fd0a6b727afb1be18 /activesupport/lib/active_support/core_ext/string/filters.rb | |
parent | 242f4d1ebadd0cff83c10270aecd56752b9befc5 (diff) | |
download | rails-1946d7b9229fabb52226f9ff82de72872c748d90.tar.gz rails-1946d7b9229fabb52226f9ff82de72872c748d90.tar.bz2 rails-1946d7b9229fabb52226f9ff82de72872c748d90.zip |
AS core_ext refactoring
Diffstat (limited to 'activesupport/lib/active_support/core_ext/string/filters.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/filters.rb | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index 1a34e88a87..7fbd140d93 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -35,13 +35,17 @@ class String # # "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)") # # => "And they f... (continued)" - def truncate(length, options = {}) - return self.dup unless self.length > length + def truncate(truncate_at, options = {}) + return dup unless length > truncate_at options[:omission] ||= "..." - length_with_room_for_omission = length - options[:omission].length - stop = options[:separator] ? - (rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission + length_with_room_for_omission = truncate_at - options[:omission].length + stop = \ + if options[:separator] + rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission + else + length_with_room_for_omission + end self[0...stop] + options[:omission] end |