From a9d3b77e494608cedcdad86d7e0c8a07694ffea5 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 16 Jul 2014 19:48:07 +0200 Subject: Added truncate_words method to activesupport strings --- activesupport/CHANGELOG.md | 4 ++++ .../lib/active_support/core_ext/string/filters.rb | 21 +++++++++++++++++++++ activesupport/test/core_ext/string_ext_test.rb | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 214722a0e5..b087d8072b 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -254,4 +254,8 @@ *Jeremy Kemper* +* Add `String#truncate_words` to truncate a string by a number of words. + + *Mohamed Osama* + Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activesupport/CHANGELOG.md) for previous changes. diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index 49c0df6026..2029909486 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -62,4 +62,25 @@ class String "#{self[0, stop]}#{omission}" end + + # Truncates a given +text+ after a given number of wordswords_count: + # + # 'Once upon a time in a world far far away'.truncate_words(4) + # # => "Once upon a time..." + # + # Pass a string or regexp :separator to specify a different separator of words: + # + # 'Once
upon
a
time
in
a
world'.truncate_words(5, separator: '
') + # # => "Once
upon
a
time
in..." + # + # The last characters will be replaced with the :omission string (defaults to "..."): + # + # 'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)') + # # => "And they found that many... (continued)" + def truncate_words(words_count, options = {}) + sep = options[:separator] || /\s+/ + sep = Regexp.escape(sep.to_s) unless Regexp === sep + return dup unless self =~ /^((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/ + $1 + (options[:omission] || '...') + end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 515144245a..7c4089eb73 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -229,6 +229,26 @@ class StringInflectionsTest < ActiveSupport::TestCase assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, :omission => "[...]", :separator => /\s/) end + def test_truncate_words + assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3) + assert_equal "Hello Big...", "Hello Big World!".truncate_words(2) + end + + def test_truncate_words_with_ommission + assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3, :omission => "[...]") + assert_equal "Hello Big[...]", "Hello Big World!".truncate_words(2, :omission => "[...]") + end + + def test_truncate_words_with_separator + assert_equal "Hello
Big
World!...", "Hello
Big
World!
".truncate_words(3, :separator => '
') + assert_equal "Hello
Big
World!", "Hello
Big
World!".truncate_words(3, :separator => '
') + end + + def test_truncate_words_with_separator_and_ommission + assert_equal "Hello
Big
World![...]", "Hello
Big
World!
".truncate_words(3, :omission => "[...]", :separator => '
') + assert_equal "Hello
Big
World!", "Hello
Big
World!".truncate_words(3, :omission => "[...]", :separator => '
') + end + def test_truncate_multibyte assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding(Encoding::UTF_8), "\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(Encoding::UTF_8).truncate(10) -- cgit v1.2.3 From f636652dd52ed36f7438c0436679c987cdfefb82 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 16 Jul 2014 11:54:53 -0700 Subject: extract inner options before delegating to the helper If we extract the options from the user facing method call ASAP, then we can simplify internal logic. --- actionpack/lib/action_dispatch/routing/route_set.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 1ee74e6810..d24751d28d 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -155,8 +155,8 @@ module ActionDispatch @arg_size = @required_parts.size end - def call(t, args) - if args.size == arg_size && !args.last.is_a?(Hash) && optimize_routes_generation?(t) + def call(t, args, inner_options) + if args.size == arg_size && !inner_options && optimize_routes_generation?(t) options = t.url_options.merge @options options[:path] = optimized_helper(args) ActionDispatch::Http::URL.url_for(options) @@ -207,15 +207,19 @@ module ActionDispatch @route = route end - def call(t, args) + def call(t, args, inner_options) controller_options = t.url_options options = controller_options.merge @options - hash = handle_positional_args(controller_options, args, options, @segment_keys) + hash = handle_positional_args(controller_options, + inner_options || {}, + args, + options, + @segment_keys) + t._routes.url_for(hash) end - def handle_positional_args(controller_options, args, result, path_params) - inner_options = args.extract_options! + def handle_positional_args(controller_options, inner_options, args, result, path_params) if args.size > 0 if args.size < path_params.size - 1 # take format into account @@ -251,7 +255,9 @@ module ActionDispatch @module.remove_possible_method name @module.module_eval do define_method(name) do |*args| - helper.call self, args + options = nil + options = args.pop if args.last.is_a? Hash + helper.call self, args, options end end -- cgit v1.2.3